0% found this document useful (0 votes)
9 views29 pages

IT G12 Chapter 6

The document is a lesson plan for Grade 12 students at Kegna Academy, focusing on the fundamentals of programming in Python. It covers topics such as program flow controls, conditionals, loops, and syntax, with objectives for students to understand and apply these concepts. The document also includes examples and explanations of conditional statements and iteration in Python.

Uploaded by

obsakenasa2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views29 pages

IT G12 Chapter 6

The document is a lesson plan for Grade 12 students at Kegna Academy, focusing on the fundamentals of programming in Python. It covers topics such as program flow controls, conditionals, loops, and syntax, with objectives for students to understand and apply these concepts. The document also includes examples and explanations of conditional statements and iteration in Python.

Uploaded by

obsakenasa2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

KEGNA ACADEMY

INFORMATION
TECHNOLOGY
GRADE 12th

Chapter 6
FUNDAMENTALS OF
PROGRAMMING
Chapter Content
6.1. Program Flow Controls and Syntax in Python
6.2. Comments in Python
6.3. Python Interpreter
6.4. Testing and Debugging Programs
1 Prepared by Sudi.M (Msc) 05/03/2025
Lesson Objective
At the end of this unit, students will be able
to:
Explain program flow controls
Describe conditionals program flow control
Describe iteration program flow control
Construct program statements using
control statements.
Appreciate Python interpreter using
Integrated Development Environment(IDE)
Analyze program statements in debugging
mode

2 Prepared by Sudi.M(Msc) 05/03/2025


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.
The sequential flow control consists of a
simple list of statements that are to be
executed in the order given.
3 Prepared by Sudi.M(Msc) 05/03/2025
6.1.1 Conditionals Program Flow Controls
In some cases, you may need a program to
choose one out of two or more alternatives,
depending on the input given.
For example, suppose you design a program
that determines whether the given number
is even or odd.
 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
4
called expression in python.
Prepared by Sudi.M(Msc) 05/03/2025

Cont…
A given integer number is either even or odd
(i.e. two possible options), but this can be
decided based on the remainder after
dividing the number by two.
Python provides conditional or branching
statement to implement such solutions.

5 Prepared by Sudi.M(Msc) 05/03/2025


Conditional Expression
Conditional expressions are statements that
use Boolean operators (such as AND, OR,
NOT) or comparison operators (such as >,<.
≠).
Like in mathematics, comparisons of two
values in Python are described with
comparison signs used in mathematics.
Table 6.1 below presents comparison
expressions in Python and Mathematics.

6 Prepared by Sudi.M(Msc) 05/03/2025


Cont…
The comparisons in Table 6.1 are also called
conditional expressions that correspond to the
Boolean values - true or false.
 For example, 3==4 results in false (as 3 is not
equal to 4).
 Note that operators used in python’s
expressions are slightly different from operators
used in Mathematics (e.g. == vs. =, != vs. ≠ ).
Python conditionals can be used in several ways,
most commonly in ‘if statements’ and loop
statements.

7 Prepared by Sudi.M(Msc) 05/03/2025


Conditional or branching statements
Conditionals are statements in a program in which
the program decides at runtime whether some
parts of the code should or should not be executed.
 From the earlier even/odd example, a given integer
number can be even or odd, but this can be
decided based on the remainder after dividing the
number by two.
Therefore, first: Decide whether (num % 2 == 0) is
or is not true.
 If it is true, then the number is even.
 If it is not, then the number is odd.

8 Prepared by Sudi.M(Msc) 05/03/2025


Cont…
The evaluation of the conditions (i.e., num%2==0) is
a test that can be checked to see if it is true or false;
as a result, executing one thing when the condition is
true, or something else when the condition is false.
In Python, a conditional statement is written using
the if keyword.
Like many other programming languages, Python
provides various versions of branching statements
that can be implemented using the if clause: simple
if statement,
if..else statement, and
if..elif…else statement

9 Prepared by Sudi.M(Msc) 05/03/2025


Cont…
if statement: this is the simplest
implementation of a conditional statement
that decides only one part of the program to
be executed if the condition is satisfied or
ignored if the condition fails.
The condition is an expression that is either
true or false.

Notice:The expression (if age>0:) must end with


colon(:). Missing colon at the end of expression is
10
a syntax
Prepared by error.
Sudi.M(Msc) 05/03/2025
if…else statement
if…else statement: The ‘if...else
statement’ provides two alternative
statements or blocks: one following the ‘if
expression’ and another following the ‘else
clause’.
‘if…else’ allows us to specify two options:
one which is executed if a condition is true
(satisfied), and another which is executed if
the condition is false (not satisfied).

11 Prepared by Sudi.M(Msc) 05/03/2025


Cont…

12 Prepared by Sudi.M(Msc) 05/03/2025


Cont….

The ‘if...else statement’ can make a


choice between two alternative actions.
Thus, the even/odd problem discussed
earlier can be accomplished with the
following python statement:

Prepared by Sudi.M(Msc)
Figure 5.8. Disk Defragmenter
13 05/03/2025
Cont…

In the above program, first the expression num % 2


== 0 is evaluated (checked).
If num % 2 is true (condition satisfied), then the
statement following (print (num, “is Even number”)
is executed.
Otherwise, which means the condition num % 2 ==
0 has not satisfied (or false), then the statement
following else (print(num, “ is Odd number”) is
executed
14 Prepared by Sudi.M(Msc) 05/03/2025
Cont…
Syntax in a python program is a set of rules that
defines how a python program is written and
interpreted.
For instance, the expression of an ‘if statement’
must be
followed by a colon (:).
Some programming languages require expressions
to be enclosed in parenthesis, but in python it is
optional.
 So in python, “if num % 2 ==0:” or “if (num % 2 ==
0):”, this complies to Python’s syntax rule.
Another important syntax in python is indentation

15 Prepared by Sudi.M(Msc) 05/03/2025


Cont…
.In Figure 6.3, it is noted that the ‘yes statements’ and ‘no
statements’ must be indented.
Indentation refers to the spaces at the beginning of a code
line. While in other programming languages the indentation
in code is only for readability, the indentation in Python is
very important.
Python uses indentation to indicate a block of code.
 For example, if you ignore the indentation in the example ‘if
statement’ above, and begin the yes statements or the
no_statements without indentation, the code generates an
error.
 Of course, the number of spaces is up to you as a
programmer, but it has to be at least one.

16 Prepared by Sudi.M(Msc) 05/03/2025


Loops or Iteration Program Flow
Controls
Most of real-world problems include some action that
is repeated several number of times.
A portion of program code that repeats a statement
or group of statements in programming is called
loop.
Loops are set of statements that run repeatedly for a
fixed number of times, or until a condition is
satisfied.
Loop statements control a block of code to be
executed iteratively or until a certain condition fails.
 Loops are a useful and frequently used feature in all
modern programming languages.
17 Prepared by Sudi.M(Msc) 05/03/2025
Cont….
Python provides several language features to
make iteration/looping easier.
There are two types of loops that are built
into Python: for loops and while loops.
In the following section, implementation of
‘for loop’ is discussed and then followed by
‘while loop’ in python.

18 Prepared by Sudi.M(Msc) 05/03/2025


for loops
 for loops: The for loop in python is used to iterate
over a sequence. For loop in combination with the
Python’s range() function is used for counting in all
kinds of ways (See for loop with range function at
the end of this section).
 The for loop in python differs a bit from other like C
or Pascal.
 In Python for loop is used to iterate over the items
of any sequence including the python list, string,
tuple, dictionary, etc.
 The general syntax of for loop is as follows.
for variable_name in sequence :
statement_1

19
statement_2
Prepared by Sudi.M(Msc) 05/03/2025

for loop with range()
function
The range() function returns a list of
consecutive integers. The sequence of
numbers starts from 0 by default, and counts
by incrementing 1(by default), and ends at a
specified number.
It is widely used count controlled loops.
In the syntax below, the range() function
takes one, two, or three parameters.
The last two parameters are optional.

20 Prepared by Sudi.M(Msc) 05/03/2025


Cont…

21 Prepared by Sudi.M(Msc) 05/03/2025


Cont…

22 Prepared by Sudi.M(Msc) 05/03/2025


Cont….

23 Prepared by Sudi.M(Msc) 05/03/2025


Cont…

24 Prepared by Sudi.M(Msc) 05/03/2025


break and continue keywords with
while loop
break and continue keywords with while loop:
With the break statement, you can stop the
loop even if the while condition is true.
 It causes the loop to quit even before
reaching the last iteration. The loop in the
program below terminates when the value of
count is 5 (in the 5th iteration).

25 Prepared by Sudi.M(Msc) 05/03/2025


Cont…

26 Prepared by Sudi.M(Msc) 05/03/2025


Comments in Python
Comments are descriptive texts that exist in
program source code and are ignored by a
compiler and interpreter.
Comments are not executable statements or
part of the program.
In python, comments are denoted by the hash
symbol (#). Anything after the # symbol is
ignored by the interpreter (See the above
example program code with the # symbol).
Comments can be given in a single line or
may take multiple lines. In any case, all
comment lines should start with the special
Prepared by Sudi.M(Msc) 05/03/2025
27
character (#).
Cont…

28 Prepared by Sudi.M(Msc) 05/03/2025


29 Prepared by Sudi.M(Msc) 05/03/2025

You might also like