Loops in Python: Example
Loops in Python: Example
Objective
To acquaint the students with looping.
For Loop
Just like while loop, "For Loop" is also used to repeat the program.
But unlike while loop which depends on condition true or false. "For Loop" depends on the
elements it has to iterate.
Example:
#
#Example file for working with loops
#
x=0
#Define a for loop
for x in range(2,7):
print(x)
Output
2
3
4
5
6
While Loop
While loop does the exactly same thing what "if statement" does, but instead of running the
code block once, they jump back to the point where it began the code and repeats the whole
process again.
Syntax
while expression
Statement
Example:
#
#Example file for working with loops
#
x=0
#define a while loop
while(x <4):
print(x)
x = x+1
Output
0
1
2
3
LAB TASKS
1. Evaluate the following series
1 1 1 1
1+ + + + ...
2 3 4 N
3. Read the marks obtained by students in examination. Determine and display sum and
average of all. Terminate the input with -1
POST LAB
5. Display the table of a number entered by the user. The range will also be given by the
user
6. Read the starting value and the ending value . Display the product of the numbers in
range
Functions in Python
Creating a Function
Example
def my_function():
print("Hello from a function")
Calling a Function
Example
def my_function():
print("Hello from a function")
my_function()
LAB TASKS
Q1) Write a void function named swap that takes two integers x and y as arguments and swap
the values of both in such a way that value of y is assigned to x and value to x is assigned
to y(e.g x=8, y=7 the swap function should make x=7 and y=8). The function prints the
values of x and y after swapping. In main(), define two integer variable with same names x
and y, get their values from the user, print their values ,now call swap function from the
main and again print the values of x and y? Are x and y in swap function values are same
as in main, if not mention the reason in comments.
Q2) A palindrome is a number or a text phrase that reads the same backwards as forwards. For
example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554
and 11611. Write a function that take in a five-digit integer as input and return 1 if the
number is a palindrome otherwise return 0. (Hint: Use the division and modulus operators
to separate the number into its individual digits.)
Q3) Repeat the above but this time the function takes in a five-digit integer and variable result
of integer type passed by reference and returns nothing, In main program print the result
obtained.
Q4) Re-implement the swap function with both integers passed by reference this time.
POST LAB
Q5) Write a function called computeCircle() that is passed three float argument (two by
reference and one by value).
void computeCircle(float& a, float& c, float r)
This function calculate the area and circumference c of the circle with given radius r and
assign it to a and c respectively. Write a main() program to exercise this function.
Q6) Re-write the above program/function that is passed three float arguments (two by
reference and one by const reference ) void computeCircle(float& a, float &c, const
float&r). Try to change the value of r , and display the errors thrown by compiler.