Day 02 (Module # 2B)
Day 02 (Module # 2B)
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
Compound logical operators require Boolean inputs and give a Boolean answer.
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
a = 2
a = a * 3
a = 2
a *= 3
Expressions:
length = 5
breadth = 2
Output:
$ python expression.py
Area is 10
Perimeter is 14
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
Control Flow
The Python interpreter executes statements in your code from the top to the bottom of
the file, in sequential order. That is unless, of course, we employ some time of control
flow statements to break this normal sequential flow.
The if statement
The if statement is used to check a condition: if the condition is true, we run a block of
statements (called the if-block), else we process another block of statements (called
the else-block). The else clause is optional.
number = 23
guess = int(input('Enter an integer : '))
if guess == number:
# New block starts here
print('Congratulations, you guessed it.')
print('(but you do not win any prizes!)')
# New block ends here
elif guess < number:
# Another block
print('No, it is a little higher than that')
# You can do whatever you want in a block ...
else:
print('No, it is a little lower than that')
# you must have guessed > number to reach here
print('Done')
# This last statement is always executed,
# after the if statement is executed.
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
Output:
$ python if.py
Enter an integer : 50
No, it is a little lower than that
Done
$ python if.py
Enter an integer : 22
No, it is a little higher than that
Done
$ python if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
number = 23
running = True
while running:
guess = int(input('Enter an integer : '))
if guess == number:
print('Congratulations, you guessed it.')
# this causes the while loop to stop
running = False
else:
print('No, it is a little lower than that.')
else:
print('The while loop is over.')
# Do anything else you want to do here
print('Done')
Output:
$ python while.py
Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
The for..in statement is another looping statement which iterates over a sequence of
objects i.e. go through each item in a sequence. A sequence is just an ordered
collection of items.
Output:
1
2
3
4
The for loop is over
range() returns a sequence of numbers starting from the first number and up to
the second number. For example, range(1,5) gives the sequence [1, 2, 3, 4].
Note that range() generates only one number at a time, if you want the full list
of numbers, call list()on the range(), for example, list(range(5)) will result in [0,
1, 2, 3, 4]. Lists are explained in the data structure.
The for loop then iterates over this range - for i in range(1,5) is equivalent to for
i in [1, 2, 3, 4]
Remember that the else part is optional. When included, it is always executed
once after the for loop is over unless a break statement is encountered.
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
An important note is that if you break out of a for or while loop, any corresponding
loop else block is not executed.
Example:
while True:
s = input('Enter something : ')
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')
The length of the input string can be found out using the built-in len function.
Output:
$ python break.py
Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something : use Python!
Length of the string is 11
Enter something : quit
Done
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
The continue statement is used to tell Python to skip the rest of the statements in the
current loop block and to continue to the next iteration of the loop.
while True:
s = input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
print('Too small')
continue
print('Input is of sufficient length')
# Do other kinds of processing here...
Output:
$ python continue.py
Enter something : a
Too small
Enter something : 12
Too small
Enter something : abc
Input is of sufficient length
Enter something : quit
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
Functions:
Functions are reusable pieces of programs. They allow you to give a name to a block
of statements, allowing you to run that block using the specified name anywhere in
your program and any number of times. This is known as calling the function.
We have already used many built-in functions such as len() and range().
Functions are defined using the def keyword. After this keyword comes
an identifier name for the function, followed by a pair of parentheses which may
enclose some names of variables, and by the final colon that ends the line.
Next follows the block of statements that are part of this function.
def say_hello():
# block belonging to the function
print('hello world')
# End of function
Output:
$ python function1.py
hello world
hello world
Function Parameters:
A function can take parameters, which are values you supply to the function so that
the function can do something utilising those values.
Parameters are specified within the pair of parentheses in the function definition,
separated by commas. When we call the function, we supply the values in the same
way.
Note the terminology used - the names given in the function definition are
called parameters whereas the values you supply in the function call are
called arguments.
thingsRoam Academy Contact: +92-308-1222240 academy.thingsroam.com
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
Example:
def print_max(a, b):
if a > b:
print(a, 'is maximum')
elif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')
x = 5
y = 7
Output:
$ python function_param.py
4 is maximum
7 is maximum
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
Local Variables
When you declare variables inside a function definition, they are not related in any
way to other variables with the same names used outside the function - i.e. variable
names are local to the function. This is called the scope of the variable.
All variables have the scope of the block they are declared in starting from the point
of definition of the name.
Example:
x = 50
def func(x):
print('x is', x)
x = 2
print('Changed local x to', x)
func(x)
print('x is still', x)
Output:
$ python function_local.py
x is 50
Changed local x to 2
x is still 50
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
If you want to assign a value to a name defined at the top level of the program (i.e.
not inside any kind of scope such as functions or classes), then you have to tell
Python that the name is not local, but it is global. We do this using
the global statement. It is impossible to assign a value to a variable defined outside a
function without the global statement.
Example:
x = 50
def func():
global x
print('x is', x)
x = 2
print('Changed global x to', x)
func()
print('Value of x is', x)
Output:
$ python function_global.py
x is 50
Changed global x to 2
Value of x is 2
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
Keyword Arguments
If you have some functions with many parameters and you want to specify only some
of them, then you can give values for such parameters by naming them - this is
called keyword arguments - we use the name (keyword) instead of the position (which
we have been using all along) to specify the arguments to the function.
There are two advantages - one, using the function is easier since we do not need to
worry about the order of the arguments. Two, we can give values to only those
parameters to which we want to, provided that the other parameters have default
argument values.
Example:
func(3, 7)
func(25, c=24)
func(c=50, a=100)
Output:
$ python function_keyword.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
Email: [email protected]
Module # 2B Prepared by:
Python Programming M. Wasiq Pervez
The return statement is used to return from a function i.e. break out of the function.
We can optionally return a value from the function as well.
Example:
def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y
print(maximum(2, 3))
Output:
$ python function_return.py
3
Email: [email protected]