You are on page 1of 6

Python Statements and Functions

Department of Computer Science


University of Zakho
Third Year Students
Compiler Design (Practical Part)

October 12, 2020

1 Lab Overview
In this lab, students will be taught how to write statements and functions
in Python. Statements consist of a combination of expressions and other
statements. Functions are used to make program more readable.

2 Python Statements
Python has different statements to implement programs based on different
loops and conditions. This includes condition and loop controls statements.

1. Condition Control Statements: These include If statements, which has


the following structures:

• If condition statements
• if condition statements else statements

2. Loops Control Statements: This includes for, while statements, which


have the following structures:

• For x in group statements


• While condition do statements
• else statement can be used with For and While statements

3. Break and Continue Statements: These statements are used to break


or continue given statements based on a specific condition.

The result of conditions must be True or False. If condition is true, then


statements will be executed, otherwise else statements will selected. In this
lab, it will be shown how python statements can be implemented, based on
different operations.

1
Start this lab by writing a new python file with name Lab3_statements.py.
Add code from each section as you go through the lab and see corresponding
result. If you have any comments about given results let me know. You can
change code in any sections as much as you like, ask if you need help.

2.1 Structure of Statements in Python


2.1.1 If statements
This statement has two structures:
1 # 1
2 if conditions :
3 statements
4
5 # 2
6 if conditions :
7 statements
8 else :
9 statements

Add this code to see how If statement can be written.


1 # Example using If statement
2 x = 5
3 y = 6
4 # Printing max between x and y
5 if x > y :
6 print ( x )
7 else :
8 print ( y )

You can change conditions and see results.

2.1.2 For Loop Statement


This statement has the following structure:
1 for item in group :
2 statements

In order to write for loop, add the following code into Lab3_statements.py.
1 # Example using for statement
2 # Printing numbers from 0 to 10
3 for x in range (10) :
4 print ( x )

Again, you can change range and see results.

2.1.3 While Loop Statement


This statement has the following structures:

2
1 # Structure of While Statement
2 while Condition :
3 statements
4 change condition

Add this code to add while statements


1 # Example using while statement
2 # Printing numbers from 0 to 10 using while
3 x = 0
4 while x < 11:
5 print ( x ) # statement
6 x = x +1 # change condition

Change value of x and conditions and then see what will you get, if you
wanted to see different cases.

2.1.4 Continue and Break Statements


These statements could be used to keep given statements running or to stop
these statements. Add the following code to see how these two statements
can be used in python.
1 # Example using for break
2 # Printing numbers from 0 to 10 using if and break .
3 for x in range (10) :
4 if x < 6:
5 print ( x )
6 else :
7 break
8 # example using continue .
9 var = 10
10 while var > 0:
11 var = var -1
12 if var == 5:
13 continue
14 print ’ Current variable value : ’ , var

2.2 Loop Statements for Collections


We can print items from collections using loops statements. For example,
print all items in list l and tupe t. use the following code.
1 # Example using for collections ( lists and tuples )
2 l = range (10) # list of 10 numbers
3 t = range (20) # tuple of 20 numbers
4 for i in l :
5 print ( i )
6 for j in t :
7 print ( j )

3
3 Functions in Python
As in the many other language, python supports function to create a pro-
gram. Functions are used to re use the same code in different places. This
lets program be more clear. In this case, you must be careful when writing
functions and included statements. Make sure each statements belong to
the block as expected.
Start this lab by writing a new python file with name Lab3_functions.py.
Add code given in each section into this file and then see corresponding re-
sults. As usual, you are allowed to change given code as much as you like,
if you get any issues let me know.

3.1 How Functions are Declared in Python

1 # Structure of functions in Python


2 def FunctionName ( parameters ) :
3 statements

To write a simple function in python, add this code into file Lab3_functions.pyand
then run it and see result.
1 # Example to use function to add two numbers a and b
2 def sum (a , b ) :
3 return a + b
4
5 # main function which will use any functions declared above
6 print ( sum (4 ,5) )
7 print ( sum (7 ,3) )

3.2 Parameters and Calling Functions


In python, functions are used to accept parameters. This means that pa-
rameters can be used in different ways. Functions could have no parameters,
one or more parameters and default parameters. Default parameters must
be the last parameters. This example shows how parameters in functions
can be used. Add this code into Lab3_functions.py and see results.
1 # Function to return add of a and b
2 def sum (a , b ) :
3 return a + b
4 # Function with ( no parameters )
5 def f1 () :
6 print ( " No parameters " )
7 # Function with ( one default parameter )
8 def f2 ( a =8) :
9 print ( " one default parameter " + str ( a ) )
10 # Function with ( one default parameter and one non default )
11 def f3 (b , a =8) :
12 print ( " one default parameter and another parameter " + str ( a )
+ " and " + str ( b ) )
13

4
14 # this is how you can call above functions ( Main function )
15 print ( sum (3 ,9) )
16 f1 ()
17 f2 (3)
18 f2 ()
19 f3 (3 ,4)
20 f3 (7)

Change parameters in the main function and see if you get any issues.
Look at the default parameters how they are declared and how they can be
used. For example, function f2 has one default parameters (a=8), if you
pass any number, for instance, f2(9), then a will be the new number (a=9),
if no parameters is passed f2() then a will be 8.
Function f3 has two parameters (one default and one non-default). So,
if you pass two parameters in the main function, for example f3(3,4), these
two new numbers will be used in the function f3. In this case, a = 3 and b
= 4. However, if you pass one parameters, for example f3(7) then this new
number will be the non-default parameter. In this case, b = 7 and a = 8).
If you pass no parameters to f3, or pass more than 2 parameters, then error
must be shown, why ?

3.3 Functions and Recursion


Recursion is a code where no loop statements are used. Functions call
them selves to do what they aimed to do it. Recursion functions have two
conditions, stop and continue conditions. Function is recursively repeated
until stop condition is satisfied. For example, the following function stops
when a = 0.
Add this function to Lab3_functions.py and see result.
1 # Example to use recursion function to print number between 0
and 20
2 def foo ( a ) :
3 if a == 0:
4 print ( a ) # this is a stop condition
5 else :
6 foo (a -1) # This statement repeat function until stop
condition is executed
7 print ( a )

4 Home Work
Do the following, send me your solutions before Thursday, if you want to
be given home work marks.

1. Change function foo to print numbers from 20 to 0.

2. Write a function to find ab (first using loops and then using recursion)

5
3. Write for loop and if statements to print all even numbers between 0
and 20.

4. Write a new function to print all odd numbers between 1 and 30, using
while, if and continue statements.

5. Write a function to find sum of all numbers between 5 and 20.

You might also like