Professional Documents
Culture Documents
Python Programming
Using Problem Solving Approach
Reema Thareja
A control statement is a statement that determines the control flow of a set of instructions, i.e., it decides the
sequence in which the instructions in a program are to be executed.
Example:
Example:
Example:
Example:
Example:
Examples:
10
Examples:
11
12
Example:
13
Example:
14
15
16
Unlike C and C++, in Python you can have the else statement associated with a loop statements. If the else statement
is used with a for loop, the else statement is executed when the loop has completed iterating. But when used with the
while loop, the else statement is executed when the condition becomes false.
Examples:
17
9. Write a script that calculates the squares and cubes of the numbers from 0 to 5. Print the resulting values in table format, as shown
below. Use the tab escape sequence to achieve the three-column output.
10. Assuming that you begin with $1000 investment in savings scheme, calculate and display how much money you’ll have after 10, 20
and 30 years. Use the following formula for determining these amounts:
where p is the original amount invested (i.e., the principal of $1000), r is the annual rate of return (7%), n is the number of years (10, 20
or 30) and a is the amount on deposit at the end of the nth year.
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 19
HOMEWORK
1) Write a Python program to sum the series: 1 2 / 1 + 22 / 2 + 32 / 3 + … + n2 / n.
2) Write a Python program to sum the series: - X + X 2 – X3 + X4 +…
The number of rows should be read from user and the number of stars should be adjusted accordingly.