completed up to exercise 10: using data and time

This commit is contained in:
Uthman Fatih 2026-08-27 16:40:55 +01:00
commit d5ddbfda71
34 changed files with 250 additions and 0 deletions

View File

@ -0,0 +1,10 @@
print(50-5*6)
print((50-5*6)/4)
print(8/5)
print(4*3)
print(12/3)
print(21&6)
print(17//5)
print(5*3+2)
print(5**2)
print(1//2)

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

View File

@ -0,0 +1,11 @@
from decimal import Decimal
import math
# Use Decimal for bcd (binary coded decimal) to avoid floating point errors
radius = Decimal('6.7')
pi = Decimal(str(math.pi))
area = pi * (radius ** 2)
print(f"{area}cm²")

View File

@ -0,0 +1,15 @@
name = input("Please enter your name: ").split(" ")
forename = name[0]
surname = name[-1] # handle the case where a middle name is supplied
age = int(input("How old are you? "))
height = int(input("How tall are you (cm)? "))
def display_info(forename, surname, age, height):
print(f"Hello {forename.capitalize()} {surname.capitalize()}! You are {age} years old and {height}cm tall.")
# uthman fatih to ufatih
print(f"Your username is {forename.lower()[0]}{surname.lower()}")
display_info(forename, surname, age, height)

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 KiB

View File

@ -0,0 +1,5 @@
name = input("Please enter your name: ").split(" ")
for i in range(len(name)):
name[i] = name[i].capitalize()
name = " ".join(name)
print(f"Hello {name}! \nI hope you are doing well.")

21
03 Strings/exercise-02.py Normal file
View File

@ -0,0 +1,21 @@
numbers = input("Please enter two numbers: ")
# if numbers variable containers , split by , else split by space
if "," in numbers:
numbers = numbers.split(",")
else:
numbers = numbers.split(" ")
# exit if len(numbers) != 2
if len(numbers) != 2:
print("Please enter two numbers")
exit()
# convert to int
for i in range(len(numbers)):
numbers[i] = int(numbers[i])
# calculate the average
average = sum(numbers) / len(numbers)
print(f"The average of {numbers[0]} and {numbers[1]} is {average}")

38
03 Strings/exercise-03.py Normal file
View File

@ -0,0 +1,38 @@
# Prompt user to enter a key number between 1 and 26
# output the original string and encrypted string
print(r"""
_________
\_ ___ \_____ ____ ___________ _______
/ \ \/\__ \ _/ __ \ / ___/\__ \\_ __ \
\ \____/ __ \\ ___/ \___ \ / __ \| | \/
\______ (____ /\___ >____ >(____ /__|
\/ \/ \/ \/ \/
""")
original = """
If he had anything confidential to say,
he wrote it in cipher,
that is, by so changing the order of the letters of the alphabet,
that not a word could be made out.
"""
key = int(input("Please enter a key number between 1 and 26: "))
if key < 1 or key > 26:
print("Please enter a key number between 1 and 26")
exit()
encrypted = ""
for i in range(len(original)):
if original[i].isalpha():
if original[i].isupper():
encrypted += chr((ord(original[i].lower()) - ord('a') + key) % 26 + ord('A'))
else:
encrypted += chr((ord(original[i].lower()) - ord('a') + key) % 26 + ord('a'))
else:
encrypted += original[i]
print(f"The original string is: {original}")
print(f"\nThe encrypted string is: {encrypted}")

BIN
03 Strings/task.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 KiB

View File

@ -0,0 +1,18 @@
def add(a, b):
""" add a and b and print the output """
print(a + b) # print the result
x = 17
y = 22
add(x, y)
def multiply_divide(a, b, z):
""" multiply a and b and divide by z """
return(a * b / z) # return the result
x = 6
y = 4
z = 8
print(multiply_divide(x, y, z))

BIN
04 Basic Function/task.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

View File

@ -0,0 +1,9 @@
def multiply_divide(a, b, z):
""" multiply a and b and divide by z """
return(a * b / z) # return the result
x = 15
y = 13
z = 5
print(multiply_divide(x, y, z))

View File

@ -0,0 +1,5 @@
def thanks(inp):
""" print a string and follow it by 'You're welcome' """
print(inp + " You're welcome")
thanks("There are only 10 types of people in the world.")

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

View File

@ -0,0 +1,33 @@
def get_name():
""" ask for a name and return it """
name = input("What is your name? ")
return name
def get_cs_teacher_name():
""" ask for a students cs teacher's name and return it """
name = input("What is your CS teacher's name? ")
return name
def average_homework():
""" get the score out of 10 for the last 4 homework assignments and average them """
test_scores = []
for i in range(4):
score = int(input("What is the score for assignment " + str(i+1) + "? "))
test_scores.append(score)
return sum(test_scores)/4
name = get_name()
teacher_name = get_cs_teacher_name()
average = average_homework()
# if average >= 8: well done {name}, {teacher_name} is very pleased with your effort
# if average >= 6 > 8: A good effort {name}, {teacher_name} thinks you should check your work carefully
# if average >= 5: {name} this is very poor, {teacher_name} has asked you to try harder
if average >= 8:
print(f"Well done {name}, {teacher_name} is very pleased with your effort")
elif average >= 6:
print(f"A good effort {name}, {teacher_name} thinks you should check your work carefully")
elif average >= 5:
print(f"{name} this is very poor, {teacher_name} has asked you to try harder")

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB

View File

@ -0,0 +1,3 @@
print(23!=15)
print(5+3<10)
print(6>10==10<2)

View File

@ -0,0 +1,7 @@
a = 3
b = 8
print(a<b)
print(6 >= a)
print(b > a == False)
print(True !=(a==b))

View File

@ -0,0 +1,6 @@
c = True
d = False
print(c and d)
print(not c or d)
print(c == d and True)

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

View File

@ -0,0 +1,14 @@
running = True
while running:
num = int(input("Enter a number between 1 and 20: "))
if num < 1:
print("Incorrect entry. Number NOT between 1 & 20.")
continue
elif num > 20:
print("Incorrect entry. Number NOT between 1 & 20.")
continue
print("Thank you. Number within correct range.")
running = False

View File

@ -0,0 +1,20 @@
# get names of two football teams and their scores
# 3 points if they win, 1 for draw, 0 for loss
WIN = 3
DRAW = 1
LOSS = 0
team1 = input("Enter the name of the first team: ")
team2 = input("Enter the name of the second team: ")
score1 = int(input("Enter the score of the first team: "))
score2 = int(input("Enter the score of the second team: "))
if score1 > score2:
print(f"{team1} wins with a score of {WIN}")
print(f"{team2} loses with a score of {LOSS}")
elif score1 < score2:
print(f"{team2} wins with a score of {WIN}")
print(f"{team1} loses with a score of {LOSS}")
else:
print(f"The teams tied with a score of {DRAW}")

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 KiB

View File

@ -0,0 +1,10 @@
# enter number between 1 and 12 print times tables
num = int(input("Enter a number between 1 and 12: "))
if num < 1 or num > 12:
print("Incorrect entry. Number NOT between 1 & 12.")
exit()
for i in range(1, 13):
print(f"{num} x {i} = {num * i}")

View File

@ -0,0 +1,23 @@
def calculate_sum():
"""Continuously prompts for numbers using a while loop until 0 is entered."""
total = 0
while True:
num = float(input("Enter a number (or 0 to finish): "))
if num == 0:
break
total += num
return total
def display_total(total_value):
"""Displays the final calculated sum using parameter passing."""
if total_value.is_integer():
print(f"\nThe final total is: {int(total_value)}")
else:
print(f"\nThe final total is: {total_value}")
def main():
final_sum = calculate_sum()
display_total(final_sum)
if __name__ == "__main__":
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

View File

@ -0,0 +1,2 @@
import time

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

BIN
11/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 KiB

BIN
12/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

BIN
13/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

BIN
14/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
15/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

BIN
16/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB