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

Python Programming Using Simple Statements and Expressions

This document provides 36 Python programming exercises ranging from basic calculations and string manipulation to more complex problems involving mathematical formulas and user input/output. The exercises cover a wide range of fundamental Python concepts including variables, expressions, conditionals, functions, modules and more.

Uploaded by

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

Python Programming Using Simple Statements and Expressions

This document provides 36 Python programming exercises ranging from basic calculations and string manipulation to more complex problems involving mathematical formulas and user input/output. The exercises cover a wide range of fundamental Python concepts including variables, expressions, conditionals, functions, modules and more.

Uploaded by

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

Python programming using simple statements and expressions

Practice the following programs using the Python interpreter as a calculator,


A. For a variable ‘name’ assign an appropriate value, and evaluate the statement
print(“Your name is”, name).
B. How many seconds are there in 42 minutes 42 seconds?
C. How many miles are there in 10 kilometers? Hint: there are 1.61 kilometers in a mile.
D. The volume of a sphere with radius r is . What is the volume of a sphere with
radius 5?
E. Suppose the cover price of a book is $24.95, but bookstores get a 40% discount.
Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the
total wholesale cost for 60 copies?
F. Divide an integer with zero and note down the output.

Practice the following programs using the Python’s IDE (script mode),
1) Write a Python program that prints (displays) your name, address, and telephone
number.

2) First request the user’s first name, and then request their last name. The program
should then display,
Hello first name last name
Welcome to Python!
For the first name and last name entered

3) Assign x = 8 and y = 2. Evaluate the following expressions and write the output,
a) x + y * 3
b) (x + y) * 3
c) x ** y
d) x % y
e) x / 12.0
f) x // 6

4) Evaluate the following expressions and note down the result


a) 11 % 3
b) 11 % -3
c) -11 % 3
d) -11 % -3

5) Print the following statements and note the output 42 + 2 and “42” + “2”

6) Write a program to print the Unicode encoded value of the given character and also
print the character of the given Unicode value.
7) Write a Python program that prompts the user to enter an upper or lower case letter
and displays the corresponding Unicode encoding.

8) Assume the variable named n holds a positive integer value. How do you determine
the last digit of n? (Hint, what does the % operator do?)

9) Earth takes a period of revolution of 31558150 seconds. Write a program to convert


these seconds into number of days, hours, and minutes.

10) Write a Python program to find the average of given 3 values.

11) Mass of earth is (5.97 × 1024) kg and mass of moon is (7.35 × 1022) kg. Write a
program to calculate the total mass of the two?

12) The distance between the Sun and the Earth is 1.496 × 1011 km and distance between
the Earth and the Moon is 3.84 × 108 m. During solar eclipse the Moon comes in
between the Earth and the Sun. Write a program to calculate the distance between the
Moon and the Sun at that particular time?

13) Write a Python program that allows the user to enter any integer value, and displays
the value of 2 raised to that power. Your program should function as shown below.
What power of two? 10
Two to the power of 10 is 1024

14) Write a Python program that allows the user to enter any integer base and integer
exponent, and displays the value of the base raised to that exponent. Your program
should function as shown below.
What base? 10
What power of 10? 4
10 to the power of 4 is 10000

15) Write a Python program that allows the user to enter a four-digit binary number and
displays its value in base 10.Each binary digit should be entered one per line, starting
with the leftmost digit, as shown below.
Enter leftmost digit: 1
Enter the next digit: 0
Enter the next digit: 0
Enter the next digit: 1
The value is 9

16) Write three Python statements. The first should read a number from the user that
represents a temperature in Fahrenheit, placing the value into a variable named f. The
second statement should convert the value into Celsius, placing the result into a
variable named c. The third statement should print the values of f and c with a
descriptive notation. [oc=5/9(of-32)]

17) The kinetic energy of a moving object is given by the formula KE=(1/2)mv2 , where
m is the object’s mass and v is its velocity. An object’s momentum is its mass
multiplied by its velocity. Write a program that accepts an object’s mass (in
kilograms) and velocity (in meters per second) as inputs and then outputs its kinetic
energy as well as its momentum.

18) You can calculate the surface area of a cube if you know the length of an edge. Write
a program that takes the length of an edge (an integer) as input and prints the cube’s
surface area as output.

19) Five Star Video rents new videos for $3.00 a night and oldies for $2.00 a night. Write
a program that the clerks at Five Star Video can use to calculate the total charge for a
customer’s video rentals. The program should prompt the user for the number of each
type of video and output the total cost

20) Write a program that calculates and prints the number of seconds in a year. (Year=365
days)

21) Light travels at 3 * 108 meters per second. A light-year is the distance a light beam
travels in one year. Write a program that calculates and displays the value of a light-
year. [Light year = speed of light * number of seconds in a year]

22) Write a series of Python statements that will first ask the user to type today’s
exchange rate between US Dollars and Euros (Ex. 1 United States Dollar equals
0.89 Euro). Next, ask the user to type a value in Dollars. Finally, print the equivalent
value in Euros.

23) An employee’s total weekly pay equals the hourly wage multiplied by the total
number of regular hours plus any overtime pay. Overtime pay equals the total
overtime hours multiplied by 1.5 times the hourly wage. Write a program that takes as
inputs the hourly wage, total regular hours, and total overtime hours and displays an
employee’s total weekly pay.

24) Translate each of the following mathematical expressions into an equivalent Python
representation and evaluate the expression.
a) (3 + 4) (7)
b) n (n – 1) / 2
c) sqrt(a * a + b * b)

25) Write a program to read two complex numbers from the user and find their sum,
difference and product.
26) Write a series of Python statements that will read a number from the user that
represents the radius of a circle. Then use a print statement to show the circles
diameter, circumference and area. You can import the math module and use the
constant math.pi to represent the constant π.

27) The math module includes a pow function that raises a number to a given power. The
first argument is the number, and the second argument is the exponent. Write a code
segment that imports this function and calls it to print the values 82 and 54.

28) Write a Python program to find the area of a triangle whose sides are a, b, c using the
formula where

29) Write a program that takes the radius(r) of a sphere (a floating-point number) as input
and outputs the sphere’s diameter (2r), circumference, surface area (4πr2), and
volume .

30) Let x = 4.66. Write the values of the following expressions:


a) round(x)
b) int(x)

31) Write a simple Python program that prompts the user for a certain number of cities for
the Traveling Salesman problem, and displays the total number of possible routes that
can be taken. Your program should function as shown below.
How many cities? 10
For 10 cities, there are 3628800 possible routes

32) Write a Python statement that reads a numeric value from the user, placing it into a
variable named x. Then write a print statement that will print the values x, x 2, x3and
x4. Use tab characters to separate the four resulting values. Write a second print
statement, but use newline characters instead of tab stops.

33) Write a Python program that prompts the user for two integer values and displays the
result of the first number divided by the second, with exactly two decimal places
displayed.

34) Write a Python program that prompts the user for two floating-point values and
displays the result of the first number divided by the second, with exactly six decimal
places displayed.
35) Write a Python program that prompts the user for two floating-point values and
displays the result of the first number divided by the second, with exactly six decimal
places displayed in scientific notation.

36) Write a Python program that allows the user to enter two integer values, and displays
the results when each of the following arithmetic operators is applied. For example, if
the user enters the values 7 and 5, the output would be,
a) 7 + 5 = 12
b) 7 - 5 = 2
c) 7 *5 =35
d) 7 /5 = 1.40
e) 7 //5 =1
f) 7 %5 =2
g) 7 **5 = 16,807
All floating-point results should be displayed with two decimal places of accuracy. In
addition, all values should be displayed with commas where appropriate.

You might also like