Grade12 April 15/08/2017E.
Unit 6 Fundamental of Programming
Python is a high-level, general-purpose programming language.
-Python is a programming language that is widely used in web applications, software development, data science,
and machine learning (ML).
-It was created by Guido van Rossum, and released in 1991.
6.1. Program Flow Controls and Syntax in Python
A program flow control is the order in which the program’s code executes. In Python, the flow control of the
program is regulated by the implementation of three types of programming language constructs or program logic,
namely sequential, branching, and loop.
6.1.1 Conditionals Program Flow Controls
An even number is a number divisible by two without a remainder, whereas an odd number is divisible by two with
a reminder. We can describe the problem with the following mathematical equation which is called expression in
python.
num % 2 = 0 num is Even, for num any integer number.
• num % 2 ≠ 0 num is Odd, for num any integer number.
Conditional Expression Conditional expressions are statements that use :
The most common of these operators include:
1. == is The “equal to” operator.
2.! = is The “not equal to” operator.
3.< is The “less than” operator.
4.> is the “greater than” operator.
5.<= is The “less than or equal to” operator.
6.>= is The “greater than or equal to” operator.
Example:
a = 5
b = 3
print(a > b) output is true
Example2. <= comparison works in practice
a = 5
b = 3
print(a <= b) output is false
Example3:
i = 10
# Checking if i is greater than 15
if (i > 15):
print("10 is less than 15")
print("I am Not in if")
Conditional Expression : Python conditionals can be used in several ways, most commonly in ‘if statements’ and
loop statements.
Example3: i = 10
# Checking if i is greater than 15
if (i > 15):
print("10 is less than 15")
example4:if(num % 2 == 0) is or is not true, If it is true, then the number is even.
N.B: In Python, a conditional statement is written using the if keyword.
if statement: if statement is the most simple decision-making statement. If the condition
evaluates to True, the block of code inside the if statement is executed.
[Link] will be the output of the following code.
[Link] is the conditional expression version of the following if-else statement?
X=45
if x < 50:
result = True
else:
result = False
7. if 4 * 10 % 12 – 9= -5
4*10=40%12=3 ,3x12=36, remainder is 4.
4-9=-5
b)
- ->100,000 to be received in 5 years at 6% per year
>>> pv_lump_sum(0.06, 5, 1000)
747.2581728660571
[Link] Loops
Import turtle
t = [Link]()
for i in range(4):
[Link](100)
[Link](90)
[Link]()
[Link] Loops
import turtle
t = [Link]()
steps = 0
while steps < 4:
[Link](100)
[Link](90)
steps += 1
[Link]()
[Link] a for Loop With Turtle
[Link]
Assignment 1.
for i in range(5):
import turtle
tfor=j in
[Link]()
range(i):
for i in range(36):
for j in
print(" ", end="")
for j in range(i, 5):
print("* ", end="")
print()
---------------------------------------
ex2.
n=5
print("Pattern 1")
for a1 in range (0,n):
for a2 in range (a1):
print("*", end="")
print()
-------------------------------------------
------------------------------------------------
for a1 in range (n,0,-1):
for a2 in range (a1):
print("*", end="")
print()
2. Write a python expression to describe the following statement.
a. Age is less than 12.
b. Student mark is equal to 95.
3. What is the output of the following expressions (use python IDE to confirm your answer)?
a. 19 != 20 b. age = 22 age ==22
4. What is the difference between height = 2.5 and height == 2.5 in python?
-Indentation refers to the spaces at the beginning of a code line
-Comments - are descriptive texts in program source code that are ignored by compilers and interpreters.
-Repeated execution of a set of statements is called iteration.
6.1.2 Loops or Iteration Program Flow Controls: Loop is used to iterate over a sequence such as a list,
string, tuple, other iterable objects such as range. Loop statements control a block of code to be executed
Iteratively or until a certain condition fails.
-Break - is a keyword which is used with for loop, stop the loop before it has looped through all the items
There are two types of loops in python:
i) For loops - is used to iterate over a sequence.
ii) While loops - runs as long as the condition (expression) evaluates to True and executes theprogram
block statements.
While loop is checked every time at the beginning of the loop and the first time when the expression evaluates to False.
Ex: Print first 10 numbers in loop
ii)While loop: example 1:
for i in range(1, 10):
number = 1
print(i) output is:1 2 3 4 5 6 7 8 9
while number <= 3:
Syntax: for loop with range() print(number)
number = number + 1 (output is 1 2 3)
for num in range(10):
print(num) Example2:
Example2: i=1
Numbers = [1, 2, 3, 4, 5] while i < 6:
for i in numbers: print(i)
# ** exponent operator if i == 4:
square = i ** 2 break
print("Square of:", i, "is:", square) i += 1 (output is 1 2 3 4)
Example3: Range 6.3. Python Interpreter-is special program that executes instruction
written in programming language.
for x in range(9,41,4):
print(x) output is: 9 13 17 21 25 29 33 37 -Python is an interpreted programming language.
Example4: -Compilation is simply a translation that generate bytecode.
for i in range(1, 11): -save your Python code with the extension .py
-Compiled source code is stored in is usually stored in a file with the extension of .pyc
if i % 2 == 0: Python interpreter process
Source codeCompilerBytecodePython virtual machine
print('Even Number:', i)
6.4. Testing and Debugging Programs
else: -The program sometimes fails to run correctly because of a bug.
-A bug is an unexpected problem in your program.
print('Odd Number:', i) -Debugging means, having complete control over the program execution.
Summary questions
1. The statement range(9) generates a sequence of numbers.(0 to 8)
2. program flow [Link],loop and