Fundamentals of Python programming: variables, data types, input/output, and basic syntax - with practical examples.
1. First Line of Python - Printing Output
The print() function displays text or values on the screen.
print("Hello Python!")
print("Learning step by step")
print("Line A\n")
print("Line B")
Output
Hello Python!
Learning step by step
Line A
Line B
\n creates a new line.
2. Variables - Storing Information
Variables are containers for values. You assign them using =.
student = "Maria"
age = 19
city = "Mombasa"
print(student, "is", age, "years old and lives in", city)
Output
Maria is 19 years old and lives in Mombasa
Variables can be updated or replaced.
points = 40
print("Points before:", points)
points = 55 # replace old value
print("Points after:", points)
points = points + 15
print("Points plus 15:", points)
Output
Points before: 40
Points after: 55
Points plus 15: 70
3. Style Rules Readable Code
Use descriptive variable names and keep track of updates.
name = "James Mwangi"
age = 30
track = "Web Development"
sessions_left = 5
print(name, "is", age, "years old.", "Track:", track, "Sessions left:", sessions_left)
sessions_left = sessions_left - 1
print("Sessions left:", sessions_left)
Output
James Mwangi is 30 years old. Track: Web Development Sessions left: 5
Sessions left: 4
4. Data Types - Kinds of Values
Python has built-in data types:
str → text (strings, always in quotes)
int → whole numbers
float → decimal numbers
bool → logical values (True/False)
title = "Python Basics" # str
year = 2026 # int
price = 99.99 # float
is_published = False # bool
print(type(title))
print(type(year))
print(type(price))
print(type(is_published))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
Strings that look like numbers ("10") are still text, not integers.
5. Input - Getting User Data
The input() function lets users type values. By default, it returns a string.
name = input("Enter your name: ")
print("Hello,", name)
print("Welcome to Python Basics")
Output
Enter your name: Kelvin
Hello, Kelvin
Welcome to Python Basics
6. Convert input to numbers using int() or float().
age = int(input("Enter your age: "))
print("You are", age, "years old")
print("Next year you will be", age + 1)
Output
Enter your age: 21
You are 21 years old
Next year you will be 22
7. f-Strings - Formatted Output
f-strings make it easy to embed variables inside text.
name = "Sophia"
age = 22
city = "Kisumu"
balance = 3200.50
print(f"Name: {name} Age: {age} City: {city} Balance: {balance}")
print(f"Next year {name} will be {age + 1} years old")
Output
Name: Sophia Age: 22 City: Kisumu Balance: 3200.5
Next year Sophia will be 23 years old
Practical Examples - Applying the Basics
Example 1: Simple Calculator with VAT
price = int(input("Enter the price (Ksh): "))
quantity = int(input("Enter quantity: "))
subtotal = price * quantity
vat = subtotal * 0.16
total = subtotal + vat
print(f"Item price: Ksh {price}")
print(f"Quantity: {quantity}")
print(f"Subtotal: Ksh {subtotal}")
print(f"VAT (16%): Ksh {vat}")
print(f"Total: Ksh {total}")
Output
Enter the price (Ksh): 200
Enter quantity: 3
Item price: Ksh 200
Quantity: 3
Subtotal: Ksh 600
VAT (16%): Ksh 96.0
Total: Ksh 696.0
Example 2: Bus Ticket Booking
print("=" * 35)
print(" BUS TICKET BOOKING")
print("=" * 35)
passenger_name = input("Enter passenger name: ")
route = input("Enter route: ")
fare = int(input("Enter fare (Ksh): "))
booking_fee = 15
print("=" * 35)
print(" TICKETING")
print("=" * 35)
print(f"Passenger : {passenger_name}")
print(f"Route : {route}")
print(f"Fare : Ksh{fare}")
print(f"Booking fee : Ksh{booking_fee}")
print("-" * 35)
print(f"Total : Ksh{fare + booking_fee}")
print("=" * 35)
print(" THANK YOU FOR BOOKING")
print("=" * 35)
Output
Enter passenger name: Brian
Enter route: Nakuru
Enter fare (Ksh): 120
Passenger : Brian
Route : Nakuru
Fare : Ksh120
Booking fee : Ksh15
Total : Ksh135
THANK YOU FOR BOOKING
Example 3: School Grade Calculator
print("=" * 38)
print(" SCHOOL GRADE CALCULATOR")
print("=" * 38)
student_name = input("Enter Student name: ")
subject = input("Enter subject: ")
print(f"Enter 3 test scores for {student_name}:")
test_1 = int(input(" Test 1: "))
test_2 = int(input(" Test 2: "))
test_3 = int(input(" Test 3: "))
average = (test_1 + test_2 + test_3) / 3
print("=" * 38)
print(f" REPORT: {student_name}")
print("=" * 38)
print(f"Subject: {subject}")
print(f"Average Score: {average}")
Output
Enter Student name: Lydia
Enter subject: English
Enter 3 test scores for Lydia:
Test 1: 65
Test 2: 70
Test 3: 80
REPORT: Lydia
Subject: English
Average Score: 71.666...
Conclusion
print() displays output.
Variables store values that can be updated.
Python has built-in data types (str, int, float, bool).
input() gets user input, and you can convert it to numbers.
f-strings make output formatting easier.
Top comments (0)