0% found this document useful (0 votes)
4 views3 pages

Lab05 (33817547)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Lab05 (33817547)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

TURIN POLYTECHNIC UNIVERSITY IN TASHKENT

COURSE OF COMPUTER SCIENCE


LABORATORY PRACTICE n. 5

Exercise 1:
Write down a Python program to:
• read a positive integer number n.
• read n integer numbers from the keyboard.
• compute and display the average of the n values introduced by the user.

Example: let n = 5 and assume that the following 5 numbers are introduced: 2 4 6 8 10. Then, the
program must display the value 6.4 (as resulting from the computation: (2+4+6+8+10)/5).

Exercise 2:
Write down a Python program which:
• reads a positive integer number n.
• computes and displays the nth partial sum of the harmonic series: H(n) = 1 + 1/2 + … + 1/n

Example: assume that value 3 is introduced for n. Then, the program must compute the result of the
sum 1+1/2+1/3, showing a value of 1.83.

Exercise 3:
Write a Python program to use the loop to find the factorial of a given number. The factorial means
multiplying all numbers from the chosen number down to 1.

Example: the factorial of 5 is 120.

Exercise 4:

Write a Python program which can calculate the sum of the numbers which are divisible by 15,
between 100 and 300 (both included).

Exercise 5:

Write down a Python program able to:


• read a sequence of integer numbers terminated by introducing a zero.
• compute the sum of all the positive and, separately, all the negative numbers among the ones
which have been introduced.
• display the two values obtained in this way.

Example: assume that the following sequence of numbers is introduced: -1 2 4 -3 5 6 1 -8 0. Then,


the program has to print out the values 18 (= 2+4+5+6+1) and -12 (= -1 + -3 + -8).
Exercise 6:
Write down a Python program to print a table reporting the decimal value of the ASCII code used
for representing each letter (both small and capital) in the English alphabet. The table must be
composed of 26 rows and 4 columns, where a small letter, its ASCII code, the corresponding capital
letter and its ASCII code are specified for each row.
To solve this problem, properly use an iterative statement.

Example: the following (few) lines give the idea of what the program should display.
‘a’ 97 ‘A’ 65
‘b’ 98 ‘B’ 66
‘c’ 99 ‘C’ 67
‘d’ 100 ‘D’ 68
...
‘z’ 122 ‘Z’ 90

Exercise 7:
Write a Python program to construct the following pattern using a nested for loop.

Exercise 8:
Write down a Python program which:
• reads a positive integer number n.
• reads n integer values and:
o displays the message “ascending sequence” if every number of the sequence
(beyond the first one) is larger than the previous one.
o displays the message “descending sequence” if every number of the sequence
(beyond the first one) is smaller than the previous one.
o displays the message “neither ascending nor descending sequence”
if none of the above conditions is satisfied.

Example: assume n=10 and the following 10 numbers introduced: -2 5 7 13 18 24 40 56 90 137.


Then, the program must print out the message: “ascending sequence”.

Exercise 9:
Write down a set of Python programs (one per figure) able to:
• read a positive integer number n.
• display the geometric figures (with “side” equal to n) as detailed in the following examples.
Example: assume n=4. Then, the figures to be printed are the following ones:

**** **** **** *+++ * *


*** **** * * -*++ **
** **** * * --*+ **
* **** **** ---* * *

Example: assume n=5. Then, the figures to be printed are the following ones:

***** ***** ***** *++++ * *


**** ***** * * -*+++ * *
*** ***** * * --*++ *
** ***** * * ---*+ * *
* ***** ***** ----* * *

Exercise 10:
Write a Python program which repeatedly reads numbers until the user enters stop. Once stop is
entered, print out the total, count, and average of the numbers. If the user enters anything other than
a number, detect their mistake using try and except, and print an error message, and skip to the
next number.

Sample Output:
Enter a number: 15
Enter a number: 10
Enter a number: 20
Enter a number: bad input
You entered invalid input
Enter a number: 10
Enter a number: stop
Sum: 55 ** Number of inputs: 4 ** Average: 13.75

You might also like