63e5e10169243QCR Lab 03
63e5e10169243QCR Lab 03
Namal University
2023
Outline
• Loops
• Functions
• Functions Practice
Loops
Python has two primitive loop commands:
while loops
for loops
While Loop
With the while loop we can execute a set of statements as long as a condition
is true.
i=1
while i < 3:
print(i)
i += 1
Break Statement
With the break statement we can stop the loop even if the while condition is
true.
i=1
while i < 6:
print(i)
if i == 3:
1
break
i += 1
Continue Statement
With the continue statement we can stop the current iteration, and continue
with the next.
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
For Loop
fruits = [”apple”, ”banana”, ”cherry”]
for x in fruits:
print(x)
Looping Through String
Loop through the letters in the word ”yellow”
for x in ”yellow”:
print(x)
Break Statement
Exit the loop when x is ”banana”:
fruits = [”apple”, ”banana”, ”cherry”]
for x in fruits:
print(x)
if x == ”banana”:
break
Continue Statement
Do not Print apple
fruits = [”apple”, ”banana”, ”cherry”]
for x in fruits:
if x == ”apple”:
continue
print(x)
2
Range Function
for x in range(8):
print(x)
for x in range(2, 6):
print(x)
for x in range(2, 30, 3):
print(x)
Functions
A function is a block of code that performs a specific task.
Function Declaration
def function(arguments):
# function body
return
def - keyword used to declare a function
function name - any name given to the function
arguments - any value passed to function
return (optional) - returns value from a function
def greet():
print(’Hello Mathematicians ;)’)
Here, we have created a function named greet(). It simply prints the text Hello
Mathematicians ;).
Function Calling
we can call the greet() function in Python.
greet()
def greet():
print(’Hello Mathematicians ;)’)
# call the function
greet()
When the function is called, the control of the program goes to the function
definition.
All codes inside the function are executed.
The control of the program jumps to the next statement after the function call.
Function Arguments
# function with two arguments
def add numbers(num1, num2):
3
sum = num1 + num2
print(”Sum: ”,sum)
# Output: Sum: 9
In the above example, we have created a function named add numbers() with
arguments: num1 and num2.
We can also call the function by mentioning the argument name as:
add numbers(num1 = 5, num2 = 4)
Return Statement
A Python function may or may not return a value. If we want our function to
return some value to a function call, we use the return statement.
def add numbers():
...
return sum
The return statement also denotes that the function has ended. Any code after
return is not executed.
4
Functions Practice
Exercise 1Write a Python function to sum all the numbers in a list.
Exercise 2Write a Python program to reverse a string.
Sample String : ”1234abcd”
Expected Output : ”dcba4321”
Exercise 3Write a Python function that takes a list and returns a new list with
unique elements of the first list.
Sample List : [1,2,3,3,3,3,4,5]
Unique List : [1, 2, 3, 4, 5]
Exercise 4 Write a Python program to print the even numbers from a given
list.
Sample List : [1, 2, 3, 4, 5, 6, 7, 8, 9]
Expected Result : [2, 4, 6, 8]
Exercise 5 Write a Python function that checks whether a passed string is
palindrome or not.
Note: A palindrome is a word, phrase, or sequence that reads the same back-
ward as forward, e.g., madam or nurses run.
Exercise 6 Define a function that accepts roll number and returns whether the
student is present or absent.
Exercise 7 Define a function in python that accepts 3 values and returns the
maximum of three numbers.
Exercise 8 Define a function which counts vowels and consonant in a word.
Exercise 9 Define a function that accepts lowercase words and returns upper-
case words.
Exercise 10 Write a Python program to make a chain of function decorators
(bold, italic, underline etc.)