Explore the world of input and output with our hands-on Python input output exercise blog! Whether you’re new to programming or looking to enhance your skills, our exercises cover the basics of user input, formatting output, and understanding the nuances of Python’s print function. Start coding with confidence and bring your Python skills to the next level!

Question 1 : Display 5 different strings “Logical”, “Python”, “is”, “the”, “Best” as #Logical#Python#is#the#Best

The sep argument is used to specify the separator in the print() function., by default it is space. The sep=”#” argument specifies that the strings should be separated by the “#” character instead of the default space.

Output:

#Logical#Python#is#the#Best

Question 2 : Display the given float number to three decimal places.

Given number:

num = 123.456789

Expected Output:

123.457

Code:

num = 123.456789
print("%.3f" %num)

Explanation:

Here, we are formatting the number to the three decimal places. The %f format specifier is used for floating-point numbers, and .3 specifies the precision (number of decimal places).

Output:

123.457

Question 3 : Accept three integers from the user and save them as a list.

Code:

num_list = []
for _ in range(3):
num = int(input("Enter the number : "))
num_list.append(num)
print("Final List =",num_list)

Explanation:

Here, the for loop iterates three times, prompting the user to enter a number in each iteration. The int(input()) combination is used to convert the user input (which is initially a string) to an integer. Each integer is then appended to the num_list using the append function.

Output:

Enter the number : 12
Enter the number : 23
Enter the number : 45
Final List = [12, 23, 45]

Question 4 : Accept three names as a single input from the user.

The split() function is called on the input, which by default splits the input string using whitespace as the delimiter.

Note: If the user enters more or fewer than three names, or if there are extra spaces between names, it may cause an error. The number of names entered must match the number of variables on the left side of the assignment.

Output:

Enter three names : Bill Steve Elon
Bill
Steve
Elon

Question 5 : Write a program to calculate BMI of the person.

Given:

Formula of BMI = Kg/m^{2}

Ask user to input the weight and height in meters.

Code:

weight = float(input("Please enter weight in Kg:"))
height = float(input("Please enter height in meter:"))
bmi = weight/(height*height)
print("Your BMI =", bmi)

Output:

Please enter weight in Kg:70
Please enter height in meter:1.74
Your BMI = 23.120623596247853

Question 6 : Ask user to input three numbers and swap them in the way that 1st becomes 2nd, 2nd becomes 3rd and 3rd becomes 1st.

Code:

first = int(input("Enter first numbers : "))
second = int(input("Enter second numbers : "))
third = int(input("Enter third numbers : "))
first, second, third = second, third, first
print("Final numbers are =", first, second, third)

Output:

Enter first numbers : 10
Enter second numbers : 20
Enter third numbers : 30
Final numbers are = 20 30 10

Question 7 : What is the output of the following code?

name = "Tim"
age = 53
print("Your name is,",name, end="")
print(", and you are",age, "years old.")

Output:

Your name is, Tim, and you are 53 years old.

Question 8 : What is the output of the following code?

a, b = 5,1
a, b, a = a + 2, b + 1, a + 2
print(a, b)

Output:

7 2

Explanation:

a = a + 2, which is 5 + 2 and becomes 7.

b = b + 1, which is 1 + 1 and becomes 2.

a = a + 2, which is 5 + 2 and becomes 7. Note: Here, the initial value of ‘a’ is still 5, not 7.

Question 9 : Identify the invalid identifier names with the reason.

1st_class
One$
Roll_no.
Roll no
False
roll-no

Output:

1st_class = Cannot start with a digit One$ = $ is a special character which is not allowed Roll_no. = Identifier cannot have a dot. Roll no = Cannot contain space False = Cannot be a keyword roll-no = Cannot contain hyphen

Question 10 : Ask user to input three numbers and change values in the way that 1st remains 1st, 2nd becomes 1st + 2nd, 3rd is the sum of all. Make sure this has to be done in the one line.

first = int(input("Enter first numbers : "))
second = int(input("Enter second numbers : "))
third = int(input("Enter third numbers : "))
first, second, third = first, first + second, first + second + third
print("Final numbers are =", first, second, third)

Output:

Enter first numbers : 2
Enter second numbers : 3
Enter third numbers : 4
Final numbers are = 2 5 9

Question 11 : What is the output of the following code?