completed up to exercise 10: using data and time
10
01 Calculate using Python/exercise-01.py
Normal 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)
|
||||
BIN
01 Calculate using Python/task.png
Normal file
|
After Width: | Height: | Size: 61 KiB |
11
02 Variables and Assignment/exercise-02.py
Normal 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²")
|
||||
15
02 Variables and Assignment/exercise-03.py
Normal 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)
|
||||
BIN
02 Variables and Assignment/task.png
Normal file
|
After Width: | Height: | Size: 157 KiB |
5
03 Strings/exercise-01.py
Normal 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
@ -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
@ -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
|
After Width: | Height: | Size: 133 KiB |
18
04 Basic Function/BasicFunction1.py
Normal 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
|
After Width: | Height: | Size: 136 KiB |
9
05 Extend Basic Function/exercise-01.py
Normal 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))
|
||||
5
05 Extend Basic Function/exercise-02.py
Normal 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.")
|
||||
BIN
05 Extend Basic Function/task.png
Normal file
|
After Width: | Height: | Size: 78 KiB |
33
06 Returning Values/exercise-01.py
Normal 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")
|
||||
BIN
06 Returning Values/task.png
Normal file
|
After Width: | Height: | Size: 135 KiB |
3
07 Relational and Logical Operators/exercise-01.py
Normal file
@ -0,0 +1,3 @@
|
||||
print(23!=15)
|
||||
print(5+3<10)
|
||||
print(6>10==10<2)
|
||||
7
07 Relational and Logical Operators/exercise-02.py
Normal file
@ -0,0 +1,7 @@
|
||||
a = 3
|
||||
b = 8
|
||||
|
||||
print(a<b)
|
||||
print(6 >= a)
|
||||
print(b > a == False)
|
||||
print(True !=(a==b))
|
||||
6
07 Relational and Logical Operators/exercise-03.py
Normal file
@ -0,0 +1,6 @@
|
||||
c = True
|
||||
d = False
|
||||
|
||||
print(c and d)
|
||||
print(not c or d)
|
||||
print(c == d and True)
|
||||
BIN
07 Relational and Logical Operators/task.png
Normal file
|
After Width: | Height: | Size: 86 KiB |
14
08 IF&ELIF Statements/exercise-01.py
Normal 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
|
||||
|
||||
20
08 IF&ELIF Statements/exercise-02.py
Normal 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}")
|
||||
BIN
08 IF&ELIF Statements/task.png
Normal file
|
After Width: | Height: | Size: 182 KiB |
10
09 While and For Loops/exercise-01.py
Normal 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}")
|
||||
23
09 While and For Loops/exercise-02.py
Normal 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()
|
||||
BIN
09 While and For Loops/task.png
Normal file
|
After Width: | Height: | Size: 63 KiB |
2
10 Using Data and Time/exercise-01.py
Normal file
@ -0,0 +1,2 @@
|
||||
import time
|
||||
|
||||
BIN
10 Using Data and Time/task.png
Normal file
|
After Width: | Height: | Size: 62 KiB |
BIN
11/image.png
Normal file
|
After Width: | Height: | Size: 125 KiB |
BIN
12/image.png
Normal file
|
After Width: | Height: | Size: 70 KiB |
BIN
13/image.png
Normal file
|
After Width: | Height: | Size: 111 KiB |
BIN
14/image.png
Normal file
|
After Width: | Height: | Size: 36 KiB |
BIN
15/image.png
Normal file
|
After Width: | Height: | Size: 34 KiB |
BIN
16/image.png
Normal file
|
After Width: | Height: | Size: 57 KiB |