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

Exercise For Python

1 2 3 4 5 6 7

Uploaded by

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

Exercise For Python

1 2 3 4 5 6 7

Uploaded by

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

MEK1300 – Programming 1

Fall 2020
Programming With Numbers and Strings

1. What are the values of the following expressions? In each line, assume that
x = 2.5
y = -1.5
m = 18
n=4

a. x + n * y - (x + n) * y
b. m // n + m % n
c. 5*x-n/5
d. 1 - (1 - (1 - (1 - (1 - n))))
e. sqrt (sqrt (n))

2. What are the values of the following expressions, assuming that n is 17 and m is 18?
a. n // 10 + n % 10
b. n % 2 + m % 2
c. (m + n) // 2
d. (m + n) / 2.0
e. int (0.5 * (m + n))
f. int (round (0.5 * (m + n)))

3. What are the values of the following expressions? In each line, assume that
s = "Hello"
t = "World"

a. len (s) + len (t)


b. s[1] + s[2]
c. s[len (s) // 2]
d. s+t
e. t+s
f. s*2

4. Write a program that prompts the user for two integers and then prints
a. The sum
b. The difference
c. The product
d. The average
e. The distance (absolute value of the difference)
f. The maximum
g. The minimum
5. Properly format the outputs in Exercise 4 as follows.

Sample output:

Enter number 1: 20
Enter number 2: 25

Sum = 45
Difference = -5
Product = 500
Average = 22.5
Distance = 5
Maximum = 25
Minimum = 20

6. Write a program that asks the user for the lengths of the sides of a rectangle. Then print the area and
perimeter of the rectangle.

Area = a * b
b
Perimeter = 2 (a + b)

7. Write a program that initializes a string variable and prints the first two characters, followed by three
periods, and then the last two characters. For example, if the string is initialized to "Mississippi", then print
Mi...pi.

8. Write a program that reads a five-digit positive integer and breaks it into a sequence of individual digits.
For example, the input 16384 is displayed as

16384

You might also like