Year 1 Computer Programming - Lecture 2
Year 1 Computer Programming - Lecture 2
(Based on Gaddis, T., Starting out with Python 3e, Pearson Education)
– Output formatting
– Decision structures
output balance
We are writing a program to calculate and display the gross pay for an
hourly paid employee.
And = 2
and12 = “And”
val 1 = 5
Val_1 = 5
1num = 12
_num = 7
– Sequences
- String : used to represent textual data, e.g. “Hello”, “welcome” etc.
– Example
Example
– Example
a=int(input("Enter your age "))
name = "Alex"
int(name)
Example
age=10
print(“He is %d years old" %age)
He is 10 years old
The % symbol can be used to define field width and decimal precisions as
well.
_float = 11.56542
Example, print ("a float %14.2f" % _float )
a float 11.57
These small functions can then be executed in the desired order to perform
the overall task.
Some library functions built into Python interpreter. To use, just call the
function
Modules: files that stores functions of the standard library
– Help organize library functions not built into the interpreter
– Copied to computer when you install Python
To call a function stored in a module, need to write an import statement
– Written at the top of the program
– Format: import module_name
– Example
import math
math.pow(2, 2)
4.0
Python syntax:
if condition:
Statement
Statement
def find_grade():
score = int(input("Enter Score : "))
if score >= 40:
print("Your grade is Pass")
CMP 4266, School of CS & DT, Birmingham City University.
Decision Structure - The if-else Statement
– Syntax: if condition:
statements
else:
other statements
– if clause and else clause must be aligned
– Statements must be consistently indented
CMP 4266, School of CS & DT, Birmingham City University.
Decision Structure - The if-elif-else Statement
def find_grade():
score = int(input("Enter Score : "))
if score >= 90:
print("Your grade is A")
elif score >= 80:
print("Your grade is B")
elif score >= 70:
print("Your grade is C")
elif score >= 60:
print("Your grade is D")
else:
print("Your grade is F")
a > b and c < a or b > c : what is the truth value if a=10, b=13 and
c=8.
So we have
a > b and (c < a or b > c) : what is the truth value if a=10, b=13
and c=8.
So we have
not (a == b or c < a and b < c): what is the truth value if a=10,
b=13 and c=8.
So we have