0% found this document useful (0 votes)
10 views5 pages

63e5e10169243QCR Lab 03

The document is a lab outline for QCR Lab 01 at Namal University, focusing on Python programming concepts such as loops and functions. It covers while and for loops, including break and continue statements, as well as function declaration, calling, arguments, and return statements. Additionally, it provides exercises for practicing function implementation in Python.
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)
10 views5 pages

63e5e10169243QCR Lab 03

The document is a lab outline for QCR Lab 01 at Namal University, focusing on Python programming concepts such as loops and functions. It covers while and for loops, including break and continue statements, as well as function declaration, calling, arguments, and return statements. Additionally, it provides exercises for practicing function implementation in Python.
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

QCR Lab 01

Namal University
2023

Lab Instructor: Tooba Tehreem


Course Instructor: Adnana Bashir

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)

# function call with two values add numbers(5, 4)

# 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.)

“Your time is limited, so don’t waste it living someone else’s life!”

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy