Skip to content
Gallery
Python Fundamentals and Analytics
Share
Explore
Milestone -2

Chapter -5 : Practice Questions & Materials

Positive or Negative numbers

In Python, when we use a numerical data-type converter is used to convert a string to an integer or float, the Python interpreter keeps into account the sign of the number if it is mentioned in the string. Here is a sample line of code that can be executed in Python:
var1 = int("-123")
var2 = int("122")
print(var2<var1)
In the above code, two variables are created (i.e., var1 and var2), and we have used a comparison operator and printed the comparison result to the console. The output of the above code will be "False", as we know that -123<122, and in Python this rule stays true because the sign is not ignored when we use data-type converter.
Task
Write a program that takes a random number (integer/float) N as input, and performs the following operations :
Print "Positive number" if N is greater than zero.
Print "Negative number" if N is less than zero.
Print "It is zero" if N is zero.
Input Format
Single line input, as a random number (integer/float)
Output Format
Single line output, either "Positive number" or "Negative number" or "It is zero"
Sample Input 1
4
Sample Output 1
Positive number
Sample Input 2
-6
Sample Output 2
Negative number
Sample Input 3
0
Sample Output 3
It is zero

Even or Odd

Task
Write a program that takes the input of a random integer, and prints if the number is even number or odd number to the console.
Input Format
Single line input of a random integer
Output Format
Single line output of either "Even" or "Odd"
Sample Input 1
4
Sample Output 1
Even
Sample Input 2
3
Sample Output 2
Odd

Print the string with the longest length

Task
Write a program that takes two random strings as input and prints out the longest of the input strings with its respective length to the console.
Input Format
The first line contains a random string
The second line contains a random string
Output Format
Single-line output, of the longest string and its length
Sample Input 1
I love coding
I love python
Sample Output 1
I love Python : 13
Sample Input 2
Hi, I am Peter
Hi
Sample Output 2
Hi, I am Peter : 14

Print the 2nd largest number out of 3 numbers

Task
Write a program that takes 3 random numbers (integer/float) as input and prints the 2nd largest number to the console.
Input Format
The first line contains a random integer/float
The second line contains a random integer/float
The third line contains a random integer/float
Output Format
Single line output, of a single number (integer/float)
Sample Input 1
1
2
3
Sample Output 1
2
Sample Input 2
-12
-125
1
Sample Output 2
-12

Print the Numbers

Task
Write a program that takes the input of a random number N (integer/float), and performs the following operations:
If N is less than or equal to zero, then print "Not in range".
If N is greater than zero, then print all the numbers from 1 to N.
Input Format
Single-line input, as a random number (integer/float)
Output Format
Single line output, either "Not in range" or numbers from 1 to N
Sample Input 1
5
Sample Output 1
1 2 3 4 5
Sample Input 2
-125
Sample Output 2
Not in range

Read a List

Task
Write a program to read a number N (integer/float), then read the next N input lines, and append them to a list. Print the final list to the console. If the number is less than or equal to zero, then print
Write a program that takes a random number (integer/float) N as input, and performs the following operations :
If N is less than or equal to zero, then print "Value not in range"
Else read the next N lines and append them to a list
Input Format
The first line contains a random number (integer/float)
The next N line contains random strings
Output Format
Single-line output, the final list or "Value not in range"
Sample Input 1 ​5 1 2 45 14 04
Sample Output 1 ​['1', '2', '45', '14', '04']

Read using While Loop

Task
Write a program that reads a value V, and then starts to read further values and adds them to a List until the initial value V is added again. Don't add the first V and last V to the list. Print the list to the console.
Input Format
N numbers of lines input, with a random string
Output Format
Single line output, as a list.
Sample Input 1
56 23 346 457 234 436 689 68 80 25 567 56
Sample Output 1
['23', '346', '457', '234', '436', '689', '68', '80', '25', '567']
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.