Python Statements and Functions: 1 Lab Overview
Python Statements and Functions: 1 Lab Overview
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.
• If condition statements
• if condition statements else statements
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.
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 )
2
1 # Structure of While Statement
2 while Condition :
3 statements
4 change condition
Change value of x and conditions and then see what will you get, if you
wanted to see different cases.
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.
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) )
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 ?
4 Home Work
Do the following, send me your solutions before Thursday, if you want to
be given home work marks.
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.