2 Chapter 2 - Introduction to Python Programming - Sessions 2, 3, and 4 - Exercises(1)(2)
2 Chapter 2 - Introduction to Python Programming - Sessions 2, 3, and 4 - Exercises(1)(2)
Programming -
Exercises
Chapter 2
Exercise 1
Write a Python program that accepts the user's first and last name and
prints them in reverse order with a space between them.
Exercise 1 solution
Name: Simon
Age: 19
Address: Bangalore, Karnataka, India
Exercise 2 solution
name = input("Enter your name: ")
age = input("Enter your age: ")
address = input("Enter your address: ")
Sample Output:
Input the radius of the circle : 1.1
The area of the circle with radius 1.1 is: 3.8013271108436504
The third line print concatenates the string and the value of the radius and area using
the + operator and prints the final string.
Tip: Python str() function is used to convert an object to its string representation.
Exercise 4
Write a Python program to get the volume of a sphere with radius six. The volume of
the sphere is : V = 4/3 × π × r3
Exercise 4 solution
pi = 3.1415926535897931
r = 6.0
V = 4.0 / 3.0 * pi * r **3
print('The volume of the sphere is: ', V)
Sample Output:
The volume of the sphere is: 904.77868423386
Exercise 5
Write a Python program that determines whether a given number (accepted from the
user) is even or odd, and prints an appropriate message to the user.
Exercise 5 solution
num = int(input("Enter a number: "))
mod = num % 2
if mod > 0:
print("This is an odd number.")
else:
print("This is an even number.")
Sample Output:
Enter a number: 5
This is an odd number.
Exercise 6
Write a Python program to convert height in centimeters to inches.
1 inch = 2.54 centimeters
Exercise 6 solution
# Input height in centimeters
cm = float(input("Enter height in centimeters: "))