unit 6 programming language
unit 6 programming language
More than a hundred programming languages have been introduced since the
introduction of the computer.
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
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.
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 >,<. ≠).
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
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
The elif is a keyword in Python to say “if the previous condition(s) are not true,
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.
1. Write the output of the above program ( in Figure 6.14) in the space
provided.
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.
• 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.
• 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