Python Input Output Exercise and Solutions


Python Input Output Exercise

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!

Python Input Output Exercise

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

Given String:

"Logical", "Python", "is", "the", "Best"

Expected Output:

#Logical#Python#is#the#Best

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Code:

print("", "Logical", "Python", "is", "the", "Best", sep = "#")

Explanation:

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

[/bg_collapse]


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

Given number:

num = 123.456789

Expected Output:

123.457

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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

[/bg_collapse]


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

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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]

[/bg_collapse]


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

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Code:

name1, name2, name3 = input("Enter three names : ").split()
print(name1)
print(name2)
print(name3)

Explanation:

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

[/bg_collapse]


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

Given:

Formula of BMI = Kg/m2

Ask user to input the weight and height in meters.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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

[/bg_collapse]


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.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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

[/bg_collapse]


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.")

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Output:

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

[/bg_collapse]


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)

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Output:

7 2

Explanation:

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

[/bg_collapse]


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

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

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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

[/bg_collapse]


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.

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

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

[/bg_collapse]


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

print("1-------------")
print(str(print()) + "Logical")
print("2-------------")
print(str(print("Logical")) + "Python" )
print("3-------------")
print(print("Logical"))
print("4-------------")
print(print("Logical", end = ""))

[bg_collapse view=”button-red” color=”#ffffff” icon=”eye” expand_text=”Show Answer” collapse_text=”Hide Answer”]

Output:

1-------------

NoneLogical
2-------------
Logical
NonePython
3-------------
Logical
None
4-------------
LogicalNone

[/bg_collapse]