06
Loops
Prepared by : Abdelaziz Shaheen
For-Loop
An iterable is an object in Python that can return its members one at a time.
Prepared by : Abdelaziz Shaheen
For-Loop
Prepared by : Abdelaziz Shaheen
While-Loop
Prepared by : Abdelaziz Shaheen
Infinite-Loops
Sometimes you want your software to keep running. In such cases, an infinite loop can be of help. Let’s create such an infinite
loop in Python:
Why is this an infinite loop? Remember that while takes an expression and keeps repeating the code as long as that expression
evaluates to True. Since the very simple expression ‘True‘ is always True, this loop never stops.
Prepared by : Abdelaziz Shaheen
list comprehensions
A Python list comprehension is a language construct. It’s used to create a Python list based on an existing list. Sounds a little
vague, but after a few examples, that ‘ah-ha!’ moment will follow, trust me.
Examples:
Prepared by : Abdelaziz Shaheen
Nested list comprehension
If expression can be any valid Python expression, it can also be another list comprehension. This can be useful when you want to
create a matrix:
Prepared by : Abdelaziz Shaheen
LAB 4
1. Write a Python program to
implement the FizzBuzz game. Print
numbers from 1 to 100, but for
multiples of 3, print "Fizz"
instead of the number, and for the
multiples of 5, print "Buzz." For
numbers that are multiples of both
3 and 5, print "FizzBuzz."
2. Write a Python program to find
the smallest and largest digit in a
number entered by the user. For
example, if the input is 37492, the
smallest digit is 2, and the
largest digit is 9.
Prepared by : Abdelaziz Shaheen
07
Functions
Prepared by : Abdelaziz Shaheen
Functions
A Python function can be defined once and used many times. So, it aids in code reuse: you don’t want to write the same code
more than once.
Functions
Parameters:
Functions accept a parameter, and you’ll see how this works in
a moment. The big advantage here, is that you can alter the
function’s behavior by changing the parameter.
Parameters
Return values:
A function can return a value. This value is often the result of
some calculation or operation. In fact, a Python function can
even return multiple values.
Return
Values
Prepared by : Abdelaziz Shaheen
Built-in Python Functions
Prepared by : Abdelaziz Shaheen
Creating a Python function
It’s just a few lines, but a lot is going on. Let’s dissect this:
• First, we see the keyword def, which is Python’s keyword to
define a function.
• Next comes our function name, say_hi.
• Then we encounter two parentheses, (), which indicate that this
function does not accept any parameters (unlike print and len).
• We end the line with a colon (:)
• And finally, we bump into a feature that sets Python apart from
many other programming languages: indentation.
Prepared by : Abdelaziz Shaheen
Creating a Python function
Our function now accepts a value, which gets assigned to the
variable name. We call such variables the parameter, while the
actual value we provide (‘Erik’) is called the argument.
Parameters and arguments:
A Python function can have parameters. The values we pass
through these parameters are called arguments.
Python Function with Multiple arguments
Prepared by : Abdelaziz Shaheen
Creating a Python function
So far, our function only printed something and returned nothing.
What makes functions a lot more usable is the ability to return a
value. Let’s see an example of how a Python function can return a
value:
Empty return statement
Prepared by : Abdelaziz Shaheen
Variable scope
The variable name only exists inside our function. We say that the variable’s scope name is limited to the function say_hi,
meaning it doesn’t exist outside of it.
Scope: The visibility of a variable is called scope. The scope defines which parts of your program can see and use a variable.
Prepared by : Abdelaziz Shaheen
Default values and named
parameters
A compelling Python feature is the ability to provide default values for the parameters:
Prepared by : Abdelaziz Shaheen
LAB 5
1. Write a Python function
is_prime(n) that determines whether
a given positive integer n is
prime. Then, create a function
sum_of_primes(limit) that
calculates the sum of all prime
numbers less than or equal to the
given limit.
Prepared by : Abdelaziz Shaheen