This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
1|Page
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
2|Page
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
3|Page
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
4|Page
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
5|Page
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
6|Page
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
7|Page
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
8|Page
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
9|Page
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
10 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
11 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
12 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
13 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
14 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
15 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
16 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
17 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
18 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
19 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
20 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
21 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
22 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
23 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
24 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
25 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
26 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
27 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
28 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
29 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
30 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
31 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
32 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
33 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
# Ask user for their name
input("What's your name?")
print("hello, Sarem")
34 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
# Ask user for their name and store the name for future use.
name = input("What's your name?")
# Say hello using their name
print("hello, name")
print("hello, ")
print(name)
# Comments can serve to increase readability of your code
# Comments can also serve to to be sort of a to-do list for yourself.
# Pseudocode is not a formal thing. It's not one specific language. It's just using English
# or your own human language to express your thoughts succinctly, methodically, algorithmically
35 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
36 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
37 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
38 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
39 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
40 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
41 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
42 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
43 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
44 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
Named Parameters
45 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
String Methods
#name = name.strip()
#print(f"hello, {name}")
#Capitalize user's name
#name = name.capitalize()
#Say hello to user
#print(f"hello, {name}")
#name = input("What's your name?")
# Remove whitespace from string and capitalize user's name
#name = name.strip()
# capitalize user's name
#name = name.title()
#print(f"hello, {name}")
# Ask user for their name
#name = input("What's your name?")
# Remove whitespace from string and capitalize user's name
#name = name.strip().title()
# Say hello using their name
#print(f"hello, {name}")
# Ask user for their name
#name = input("What's your name? ").strip().title()
# Say hello using their name
#print(f"hello, {name}")
# Ask user for their name
#name = input("What's your name? ").strip().title()
# Split user's name into first name and last name
#first, last = name.split(" ")
# Say hello to user
#print(f"hello, {first}")
46 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
def hello(to = 'world'):
print("hello,", to)
hello()
name = input("What's your name? ")
hello(name)
hello()
name = input("What's your name? ")
hello(name)
def hello(to = 'world'):
print("hello,", to)
def main():
name = input("What's your name? ")
hello(name)
def hello(to = "World"):
print("hello,", to)
main()
def main():
x = int(input("Enter a number: "))
print(x, "squared is", square(x))
def square(n):
return n * n
main()
47 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
def main():
x = int(input("Enter a number: "))
print(x, "squared is", square(x))
def square(n):
return n ** 2 # n ** n
main()
def main():
x = int(input("Enter a number: "))
print(x, "squared is", square(x))
def square(n):
return pow(n, 2)
main()
Conditionals
x = int(input("What's the first number: "))
y = int(input("What's the second number: "))
if x < y:
print(x, " is less than ", y)
if x > y:
print(x, " is greater than ", y)
if x == y:
print(x, " is equal to ", y)
48 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
x = int(input("What's the first number: "))
y = int(input("What's the second number: "))
if x < y:
print(x, " is less than ", y)
elif x > y:
print(x, " is greater than ", y)
elif x == y:
print(x, " is equal to ", y)
49 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
x = int(input("What's the first number: "))
y = int(input("What's the second number: "))
if x < y:
print(x, " is less than ", y)
elif x > y:
print(x, " is greater than ", y)
else :
print(x, " is equal to ", y)
x= int(input("What's the first number: "))
y= int(input("What's the second number: "))
if x < y or x > y :
print(x, " is not equal to ", y)
else :
print(x, " is equal to ", y)
x = int(input("What's the first number: "))
y = int(input("What's the second number: "))
if x != y:
print(x, " is not equal to ", y)
else :
print(x, " is equal to ", y)
50 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
x= int(input("What's the first number: "))
y= int(input("What's the second number: "))
if x == y:
print(x, " is equal to ", y)
else :
print(x, " is not equal to ", y)
print("meow") i=3 i=3
print("meow") while i != 0: while i != 0:
print("meow") print("meow") print("meow")
i=i–1
Grade Calculator
while True:
score = int(input("Enter the score of the student: "))
if score <= 100 and score > 0:
break
else:
print(score, " is invalid mark!")
51 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
if score > 94 and score <= 100:
print("Grade: A+")
elif score > 84 and score <= 94 :
print("Grade: A")
elif score > 79 and score <= 84 :
print("Grade: A-")
elif score > 74 and score <= 79:
print("Grade: B+")
elif score > 69 and score <= 74 :
print("Grade: B")
elif score > 64 and score <= 69:
print("Grade: B-")
elif score > 59 and score <= 64:
print("Grade: C+")
elif score > 49 and score <= 59 :
print("Grade: C")
elif score > 40 and score <= 49 :
print("Grade: C-")
elif score > 34 and score <= 40 :
print("Grade: D+")
elif score > 29 and score <= 34 :
print("Grade: D")
elif score > 24 and score <= 29 :
print("Grade: D-")
else:
print("Grade: F")
Grade Calculator2
while True:
score = float(input("Enter the score of the student: "))
if score <= 100 and score > 0:
break
else:
print(score, " is invalid mark!")
52 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
if 94 <= score <= 100:
print("Grade: A+")
elif 84 <= score <= 94 :
print("Grade: A")
elif 79 <= score <= 84 :
print("Grade: A-")
elif 74 <= score <= 79:
print("Grade: B+")
elif 69 <= score <= 74 :
print("Grade: B")
elif 64 <= score <= 69:
print("Grade: B-")
elif 59 <= score <= 64:
print("Grade: C+")
elif 49 <= score <= 59 :
print("Grade: C")
elif 40 <= score <= 49 :
print("Grade: C-")
elif 34 <= score <= 40 :
print("Grade: D+")
elif 29 <= score <= 34 :
print("Grade: D")
elif 24 <= score <= 29 :
print("Grade: D-")
else:
print("Grade: F")
Grade Calculator3
while True:
score = int(input("Enter the score of the student: "))
if score <= 100 and score > 0:
break
else:
print(score, " is invalid mark!")
53 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
if score > 94:
print("Grade: A+")
elif score > 84:
print("Grade: A")
elif score > 79:
print("Grade: A-")
elif score > 74:
print("Grade: B+")
elif score > 69:
print("Grade: B")
elif score > 64:
print("Grade: B-")
elif score > 59:
print("Grade: C+")
elif score > 49:
print("Grade: C")
elif score > 40:
print("Grade: C-")
elif score > 34:
print("Grade: D+")
elif score > 29:
print("Grade: D")
elif score > 24:
print("Grade: D-")
else:
print("Grade: F")
Even Odd Checker
while True:
num = int(input("Enter any positive number: "))
if num > 0:
break
else:
print(num, " is not positive!")
if num % 2 == 0:
print(num, " is Even!")
else:
print(num, " is Odd!")
Using Functions for task sharing
54 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
def main():
while True:
num = int(input("Enter any positive number: "))
if num > 0:
break
else:
print(num, " is not positive!")
if is_even(num):
print(num, " is Even!")
else:
print(num, " is Odd!")
def is_even(n):
if n % 2 == 0:
return True
else:
return False
main()
def main():
while True:
num = int(input("Enter any positive number: "))
if num > 0:
break
else:
print(num, " is not positive!")
if is_even(num):
print(num, " is Even!")
else:
print(num, " is Odd!")
def is_even(n):
return True if n % 2 == 0 else False
main()
def main():
while True:
num = int(input("Enter any positive number: "))
if num > 0:
break
55 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
else:
print(num, " is not positive!")
if is_even(num):
print(num, " is Even!")
else:
print(num, " is Odd!")
def is_even(n):
return (n % 2 == 0)
main()
i=1 i=0
while i <= 3: while i < 3:
print("meow") print("meow")
i=i+1 i=i+1
i=0
for i in [0, 1, 2]: for i in range(3):
while i < 3:
print("meow") print("meow")
print("meow")
i += 1
for i in range(1000000): for _ in range(3):
print("meow") print("meow") print("meow" * 3)
print("meow\n" * 3, end = "") 56 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
while True:
print("meow\n" * 3) n = int(input("What's the number? ")
if n > 0:
while True:
break
n = int(input("What's the number? ")
if n > 0:
for _ in range(n):
break
print("meow")
for _ in range(n):
print("meow")
students = ["Eyasu Tilahun","Anteneh Admasu","Hewan Legesse", "Anania Sahile", "Natnael Kefyalew"]
print(students[0])
print(students[1])
print(students[2])
print(students[3])
students = ["Eyasu Tilahun","Anteneh Admasu","Hewan Legesse", "Anania Sahile", "Natnael Kefyalew"]
for student in students:
print(student)
students = ["Eyasu Tilahun","Anteneh Admasu","Hewan Legesse", "Anania Sahile", "Natnael Kefyalew"]
for i in range( len(students) ):
print(students[i])
57 | P a g e
students = ["Eyasu Tilahun","Anteneh Admasu","Hewan Legesse", "Anania Sahile", "Natnael Kefyalew"]
for i in range( len(students) ):
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
planets = {
"Mercury" : "Hot!",
'Venues' : "Rotates Backward",
'Earth' : "Our Home!",
'Mars' : "Red Planet!",
'Jupiter' : "The Biggest!",
'Saturn' : "Has Rings!",
'Uranus' : "Ha Ha!",
'Neptune' : "Blue Planet!",
'Pluto' : "Not a planet!"
print(planets['Earth'])
print(planets['Saturn'])
print(planets['Jupiter'])
print(planets['Venues'])
planets = {
"Mercury" : "Hot!",
'Venues' : "Rotates Backward",
'Earth' : "Our Home!",
'Mars' : "Red Planet!",
'Jupiter' : "The Biggest!",
'Saturn' : "Has Rings!",
'Uranus' : "Ha Ha!",
'Neptune' : "Blue Planet!",
'Pluto' : "Not a planet!"
}
for planet in planets:
print(planet)
58 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
planets = {
"Mercury" : "Hot!",
'Venues' : "Rotates Backward",
'Earth' : "Our Home!",
'Mars' : "Red Planet!",
'Jupiter' : "The Biggest!",
'Saturn' : "Has Rings!",
'Uranus' : "Ha Ha!",
'Neptune' : "Blue Planet!",
'Pluto' : "Not a planet!"
}
for planet in planets:
print(planet, planets[planet], sep = ", ")
students = [
{"name" : "Sarem", "school": "LGM", "Grade": 11, "Age": 15},
{"name" : "Rediet", "school": "Italian State", "Grade": 9, "Age": 13},
{"name" : "Fiker", "school": "Nazereth", "Grade": 11, "Age": 17},
{"name" : "Hana", "school": "Cathedral", "Grade": 11, "Age": 17}
]
for student in students:
print(student['name'],student['school'])
def main(): def main(): def main():
print_column(3) print_column(3) print_row(4)
def print_column(height): def print_column(height): def print_row(width):
for _ in range(height): print("#\n" * height, end = "") print("?" * width)
print("#")
main() main() main()
59 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
def main(): def main():
def main():
print_square(3) print_square(3)
print_square(3)
def print_square(size): def print_square(size):
def print_square(size):
for i in range(size):
for i in range(size):
# For each row in square print_row(size)
for j in range(size):
print("#", end = "") for i in range(size):
def print_row(width):
print()
# For each brick in row print("#" * width)
for j in range(size):
main()
#Print brick main()
print("#", end = "")
print()
main()
Exceptions
Syntax error should be fixed by you. You cannot able to write a program to fix it.
number = int (input("Please enter any valid integer number: "))
print(f"You have entered {number}")
assuming the user has entered non integer value for example ca
def main():
print_square(3)
def print_square(size):
for i in range(size):
print_row(size) 60 | P a g e
def print_row(width):
print("#" * width)
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
try:
num = int
except Value
print("You
print(f"The n
try: while True:
num = int(input("What's the number ")) try:
except ValueError: num = int(input("What's the number "))
print("You have entered non integer value") except ValueError:
else: print("You have entered non integer value")
print(f"The number is {num}")try: else:
num = int(input("What's the number ")) break
except ValueError:
print("You have entered non integer value") print(f"The number is {num}")
else:
print(f"The number is {num}")
def main():
x = get_int()
while True: print(f"The number is {x}")
try:
num = int(input("What's the number ")) def get_int():
break while True:
except ValueError: try:
print("You have entered non integer value") x = int(input("Enter an integer"))
except ValueError:
print(f"The number is {num}") print(x, " is not an integer")
else:
break
return x
main()
61 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
def main():
x = get_int()
print(f"The number is {x}")
def get_int():
while True:
try:
x = int(input("Enter an integer"))
except ValueError:
print(x, " is not an integer")
else:
return x
main()
62 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
def main(): def main():
x = get_int() x = get_int("Enter an integer")
print(f"The number is {x}") print(f"The number is {x}")
def get_int(): def get_int(prompt):
while True: while True:
try: try:
return int(input("Enter an integer")) return int(input(prompt))
except ValueError: except ValueError:
pass pass
main() main()
63 | P a g e
This material is prepared by Tsegaye Amde
@ Microlink Information Technology College
64 | P a g e