0% found this document useful (0 votes)
3 views

unit 6 programming language

The document outlines learning outcomes related to program flow controls in Python, including conditionals and iteration. It explains the syntax and semantics of programming languages, the types of flow control constructs, and provides examples of conditional statements and loops. Additionally, it discusses debugging, the role of the Python interpreter, and includes various activities for practical application of the concepts learned.

Uploaded by

negasabeshada
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

unit 6 programming language

The document outlines learning outcomes related to program flow controls in Python, including conditionals and iteration. It explains the syntax and semantics of programming languages, the types of flow control constructs, and provides examples of conditional statements and loops. Additionally, it discusses debugging, the role of the Python interpreter, and includes various activities for practical application of the concepts learned.

Uploaded by

negasabeshada
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Learning Outcomes

 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
Cont’d
 A software program is written using a programming language.

 More than a hundred programming languages have been introduced since the
introduction of the computer.

 Programming languages are designed with a specific purpose.

 Like human languages, programming languages have their own specific set of
rules called syntax, which define how to write instructions in that language.
Cont’d

 Syntax is set of rules that govern the writing of instructions/statements in that


specific programming language, whereas semantics is meaning attached to
language construct.

 For example, in Python the syntax to print some text the command is
‘print(“text”)’ or ‘print(‘text’)’.
Program Flow Controls and Syntax in Python
• three types of programming language constructs
• Sequential: e.g. before going to school such as: wake up early, got to toilet, wash your face,
eat breakfast, brush your teeth.

• Branching (decision) (conditional): e.g. Determining student rankings is example of


conditional logic

• Loop (iteration) e.g. Counting a sequence of numbers from 0 to 100

The sequential flow control consists of a simple list of statements that are to be executed in
the order given.

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.
Conditionals Program Flow Controls
Conditional Expression
• Conditional expressions are statements that use Boolean operators
(such as AND, OR, NOT) or comparison operators (such as >,<. ≠).

‘=’ is the assignment operator


Activity 6.2 Work in group
1. Write the python equivalent expression for the following mathematical
equation and determine its output.
a. 21 ≠ 21
b. 97 ≥ 99
c. Price = 135
2. Write a python expression to describe the following statement.
a. Age is less than 12.
b. Student mark is equal to 95.
c. Weight less than or equal to 45.5.
3. What is the output of the following expressions (use python IDE to confirm
your answer)?
a. 19 != 20
b. age = 22 age ==22
c. ‘zenash’ == ‘zehara’
4. What is the difference between height = 2.5 and height == 2.5 in python?
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.
• 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.
• if…else statement: The ‘if... ‘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).
• if...elif…else statement: Assume a problem that could have many options to choose
from or require several conditions. For example, you want to develop a program that
will print ‘Excellent, Very Good, Good, Satisfactory, or Fail’ based on the student mark.
Activity 6.3

Based on the example presented under the simple if statement above:

a. What is the output if you write age 15?

b. What is the output if you write age 0? Discuss in pair.

c. What is the output of if the second print statement is un indented?

Discuss in pair.
‘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).

Eg.1

e.g.2 num = int(input("enter integer number"))


if num % 2==0: Output
enter integer number19
print(num, " is even ")
19is odd
else: enter integer number20
print(num, " is odd") 20 is even
Activity 6.5

1. Based on the code in Figure 6.4,


a. What is the output if the age is 20?
b. What if the number 0 is stored in age?
c. When do you get the output “You are children”?
2. A group activity. Write a Python program that read two numbers from
the keyboard and compute division of the first number by the second.
Apply appropriate if statement to check that the second number is different from
zero, otherwise print division by zero is not allowed. Do in pair
if...elif…else statement: Assume a problem that could have many options to

choose from or require several conditions. For example, you want to develop a

program that will print ‘Excellent, Very Good, Good, Satisfactory, or Fail’ based

on the student.t mark.

The elif is a keyword in Python to say “if the previous condition(s) are not true,

then try this condition”.

The else keyword catches anything that is not caught by all the preceding
conditions.
e.g.1
age=int(input("enter age: "))
if (age<13):
enter age: 11
print("you are child") you are child
elif (age<18): enter age: 50
print("you are adolescent") you are adult
elif (age<60): enter age: 80
print("you are adult") you are old
else:
print("you are old")
Activity 6.6

1. Replace price =49.5 by the message = “Hello Student”, and the variable
price in all expressions by the message in Figure 6.8 above. Then,
discuss the output of the code.
2. Use appropriate conditional statements to write a Python program that
solves the following problem:
The program should prompt the user to enter her/his average mark in the
last or the current semester and then print excellent, very good, good,
satisfactory or fail based on the evaluation of the mark entered.
What necessary to write the program code?
3. Write a Python program that calculates the four arithmetic operations
(i.e. +, *, - and /).
The program asks the user to enter one of the four arithmetic symbols.
Then:
• It asks the user to enter two integer numbers.
• Based on the operator and numbers entered by the user, calculate
and display the result.
Loops or Iteration Program Flow Controls

• Loops are set of statements that run repeatedly for a fixed number of times, or
until a condition is satisfied.

• for loops: The for loop in python is used to iterate over a sequence.

• while Statement: The while statement in Python supports repeated execution of


a statement
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.

range(x, y): This generates a sequence of numbers from x to y excluding y, incrementing by 1.


output
5
6
7
8
>>>
range(x, y, z): This generates a sequence of numbers from x to y excluding y,
incrementing by z.
e.g.
Activity 6.10

1. Write the output of the above program ( in Figure 6.14) in the space

provided.

2. Write a for loop that counts from 51 to 70.


Activity 6.11

1. Write a program that generates a sequence of numbers from 10 to 100,

incrementing by 10.

2. Write a for loop to generate 1, 4, 7, 10, 13, 16, 19, and 22.
Comments in Python

• Comments are descriptive texts that exist in program source code and are
ignored by a compiler and interpreter.
• comments are denoted by the hash symbol (#).
• Comments are not executable statements
• Using comments, a program can make code more readable for other
• developers
• comments can serve as notes to yourself or reminders.
• Commenting part of code are that you do not want to execute while compiling or
running a program.
Python Interpreter
• The interpreter is the program that is responsible for executing each line of
statements sequentially and runs the Python code or script which refers to a
simple program stored in a plain text file.

• Python is an interpreted programming language,


• compilation is a step in Python interpreter processes.

• A breakpoint is a line of code that you have identified as a place where the
interpreter should pause while running your code
Testing and Debugging Programs
• Debugging means, having complete control over the program execution.

• A bug is an unexpected problem in your program.


• Bugs can appear in many forms, and some are more difficult to fix than others.

• Some bugs are tricky enough that you will not be able to catch them by just reading
through your program.

• A breakpoint is a line of code that you have selected as a place where the
interpreter should pause while running your code.
Homework

 Unit summary

 Key terms

 Review questions

You might also like