0% found this document useful (0 votes)
13 views

2 Chapter 2 - Introduction to Python Programming - Sessions 2, 3, and 4 - Exercises(1)(2)

Uploaded by

milo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

2 Chapter 2 - Introduction to Python Programming - Sessions 2, 3, and 4 - Exercises(1)(2)

Uploaded by

milo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to Python

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

fname = input("Input your First Name : ")


lname = input("Input your Last Name : ")
print("Hello " + lname + " " + fname)
Exercise 2
Write a Python program that reads and displays your name, age, and
address on three different lines.

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

print("Name:", name, "\n Age:", age, "\n address:", address)


Exercise 3
Write a Python program that calculates the area of a circle based on
the radius entered by the user. In geometry, the area enclosed by a
circle of radius r is π*r^2.
Exercise 3 solution
from math import pi
r = float(input("Input the radius of the circle : "))
print("The area of the circle with radius " + str(r) + " is: " + str(pi * r *
*2))

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

# 1 inch = 2.54 centimeters


inches = cm / 2.54

# Display the result


print("The height in inches is", inches)
Exercise 7
Write a Python program to swap two variables. Swapping two variables
refers to mutually exchanging the values of the variables.
Exercise 7 solution
# Input two variables
a = input("Enter the value of variable 'a': ")
b = input("Enter the value of variable 'b': ")

# Print the values before swapping


print("Before swapping:", (a, b))

# Swap the values using a temporary variable


temp = a
a = b
b = temp

# Print the values after swapping Sample Output:


print("After swapping:", (a, b)) Enter the value of variable 'a': 10
Enter the value of variable 'b': 20
Before swapping: ('10', '20')
After swapping: ('20', '10')
Exercise 8
Write a Python to check whether a number is divisible by another number.
Accept two integer values from the user.
Exercise 8 solution
# Input two integer values from the user
number = int(input("Enter the first number: "))
divisor = int(input("Enter the second number (divisor):
"))

# Check if the first number is divisible by the second


if number% divisor == 0:

print(number, "is divisible by", divisor)


else:
print(number, "is not divisible by", divisor)
Exercise 9
Write a Python program to determine the highest and lowest values among three
numbers.
Exercise 9 solution
# Input three numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Find the maximum


max_num = num1
if num2 > max_num:
max_num = num2
if num3 > max_num:
max_num = num3

# Find the minimum


min_num = num1
if num2 < min_num:
min_num = num2
if num3 < min_num :
min_num = num3

# Print the maximum and minimum


print("Maximum number:", max_num)
print("Minimum number:", min_num)
Exercise 10
Write a Python program to calculate the square, cube, and quadratic root of a
given number.
Exercise 10 solution
# Input a number from the user
number = float(input("Enter a number: "))

# Calculate the square


square = number * number

# Calculate the cube


cube = number * number * number

# Calculate the quadratic


quadratic = number ** 4

# Print the results


print("The square of:", number, "is", square)
print("The cube of:", number, "is", cube)
print("The quadratic of:", number, "is", quadratic)

You might also like