The Foundations
All the notes. All the examples. All in one place. The challenges prove you understand it. Solve them all and you mastered The Foundations.
▶📝 Cheatsheet
1. Print
Before you do anything, you need to see output.
print("Hello World")Printing numbers
print(25)
print(3.99)Printing multiple values
print("Name:", "Ahmed")
print("Age:", 25)New lines and tabs
print("Hello\nWorld") # prints on two lines
print("Name:\tAhmed") # adds a tab space2. Variables
A variable stores a value so you can use it later.
name = "Hodan"
print(name) # HodanData types
city = "Oslo" # str (text)
age = 22 # int (whole number)
price = 9.99 # float (decimal)
paid = True # bool (True or False)Change variable
age = 22
age = 23
print(age) # 23Naming rules
first_name = "Hodan" # yes
1st_name = "Hodan" # no - can't start with a numberConcatenation
greeting = "Hello " + name
print(greeting) # Hello Hodanf-strings: the better way
print(f"Welcome {name}, you are {age} years old")3. User Input
Your program can ask the user for information.
name = input("What is your name? ")
print(f"Hello {name}")Storing multiple inputs
name = input("Name: ")
city = input("City: ")
print(f"{name} lives in {city}")Input is always a string. Even if the user types a number.
age = input("Enter your age: ")
print(type(age)) # <class 'str'>Type casting: convert it to the type you need
age = int(input("Enter your age: ")) # whole number
price = float(input("Enter price: ")) # decimal4. Math
Basic operators
10 + 3 # 13 (addition)
10 - 3 # 7 (subtraction)
10 * 3 # 30 (multiplication)
10 / 3 # 3.333 (division)Floor division and modulo
10 // 3 # 3 (division without decimals)
10 % 3 # 1 (remainder)Rounding
price = 10 / 3
print(round(price, 2)) # 3.33Order of operations
total = 10 + 5 * 2 # 20, not 30
total = (10 + 5) * 2 # 305. Text & Strings
Length
name = "Abdifatah"
print(len(name)) # 9Uppercase and lowercase
name = "ahmed"
print(name.upper()) # AHMED
print(name.lower()) # ahmed
print(name.capitalize()) # AhmedReplace
message = "I love Java"
print(message.replace("Java", "Python")) # I love PythonStrip whitespace
city = " Oslo "
print(city.strip()) # OsloSlicing
name = "Abdifatah"
print(name[0:4]) # Abdi
print(name[0]) # A
print(name[-1]) # hFind and count
text = "banana"
print(text.find("n")) # 2
print(text.count("a")) # 3Summary
| Category | Key Ideas |
|---|---|
| print(), \n, \t | |
| Variables | name = value, str, int, float, bool |
| Input | input(), int(), float() |
| Math | +, -, *, /, //, %, round() |
| Strings | .upper(), .lower(), .replace(), .strip(), len(), slicing |
▶🧩 Challenges
Challenge 1: Grocery Run
Sahra has $200. She buys rice for $25, chicken for $40, bread for $8, and milk for $6. Print the total, budget, and change.
Total: $79
Budget: $200
Change: $121Challenge 2: Gym Membership
Mahad wants to join a gym. The fee is $50 per month. He has $320. How many full months can he pay for and how much is left?
Months: 6
Remaining: $20Challenge 3: Flight Booking
Ahmed is booking a flight for $500. The airline adds a 10% booking fee. Print the fee and the total.
Flight: $500
Fee: $50
Total: $550Challenge 4: Greeting Card
Ask the user for their name and their friend's name. Print a greeting card. The sender's name should be in uppercase.
===================
GREETING CARD
===================
To: Faadumo
From: AHMED
Message: Wishing you the best!
===================Challenge 5: Name Tag
Ask the user for their full name. Print it in uppercase and lowercase. Count the total letters without the space. Show the first and last letter.
Upper: ABDIFATAH BASHI
Lower: abdifatah bashi
Letters: 14
First: A
Last: iChallenge 6: Pizza Split
You and your friends ordered pizza. Ask for the total bill and how many people are splitting. Print what each person pays, rounded to 2 decimals.
Total bill: $85
Friends: 6
Each pays: $14.17Challenge 7: Username Generator
Ask the user for their first name and birth year. Create a username from the first 4 letters of their name in lowercase plus their birth year.
Name: Abdifatah
Year: 1998
Username: abdi1998Challenge 8: Coffee Receipt
Ask the user for their name and how many coffees they want. Each coffee is $5. Print a receipt with their name in uppercase and the total.
===================
COFFEE RECEIPT
===================
Customer: HAMDA
Coffees: 3
Total: $15
===================Challenge 9: Contact Card
Ask for a name, phone number, and email. Print a contact card. Show the name capitalized and how many characters the email has.
===================
CONTACT CARD
===================
Name: Ahmed Yusuf
Phone: 92345678
Email: ahmed@email.com (14 characters)
===================Challenge 10: Travel Profile
Ask the user for their name, destination, and number of days. Print a travel card with their name in uppercase, destination capitalized, and a message about the trip.
===================
TRAVEL PROFILE
===================
Traveler: HODAN
Destination: Istanbul
Days: 7
Note: Hodan is spending 7 days in Istanbul. Bon voyage!
===================