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

Loops in Python: Example

This document discusses loops and functions in Python. It provides examples of for loops, while loops, and defining and calling functions. The key points are: 1. For loops iterate over elements in a range or collection, while while loops repeat as long as a condition is true. 2. Functions are blocks of code that are called by name and can accept parameters and return values. 3. The document provides examples of for loops, while loops, and functions in Python code, and presents lab tasks to practice writing loops and functions.

Uploaded by

ALvi
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)
50 views

Loops in Python: Example

This document discusses loops and functions in Python. It provides examples of for loops, while loops, and defining and calling functions. The key points are: 1. For loops iterate over elements in a range or collection, while while loops repeat as long as a condition is true. 2. Functions are blocks of code that are called by name and can accept parameters and return values. 3. The document provides examples of for loops, while loops, and functions in Python code, and presents lab tasks to practice writing loops and functions.

Uploaded by

ALvi
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/ 5

Loops in Python

Objective
To acquaint the students with looping.

To write a simple program

For Loop

In Python, "for loops" are called iterators.

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

For Loop iterates with number declared in the range.

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

• Code Line 4: Variable x is set to 0


• Code Line 7: While loop checks for condition x<4. The current value of x is 0. Condition is
true. Flow of control enters into while Loop
• Code Line 8: Value of x is printed
• Code Line 9: x is incremented by 1. Flow of control goes back to line 7. Now the value of
x is 1 which is less than 4. The condition is true, and again the while loop is executed.
This continues till x becomes 4, and the while condition becomes false.

LAB TASKS
1. Evaluate the following series
1 1 1 1
1+ + + + ...
2 3 4 N

where N is entered by the user

2.Read a number and display the number of digits in it

3. Read the marks obtained by students in examination. Determine and display sum and
average of all. Terminate the input with -1

4. Display the table of 2 from 1-10

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

A function is a block of code which only runs when it is called.


You can pass data, known as parameters, into a function.
A function can return data as a result.

Creating a Function

In Python a function is defined using the def keyword:

Example
def my_function():
print("Hello from a function")

Calling a Function

To call a function, use the function name followed by parenthesis:

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.

You might also like