Week 2 Notes: Python Fundamentals
Course: Object-Oriented Programming Using Python
Level: Pre-Diploma
Duration: 3 Hours
Learning Outcomes
By the end of this lesson, learners should be able to:
1. Declare and use variables in Python.
2. Identify and use different data types.
3. Accept input from users.
4. Perform calculations using operators.
5. Apply conditional statements to make decisions.
6. Use loops to repeat tasks.
7. Create and use functions.
8. Develop simple Python programs that solve real-world problems.
Introduction
In Week 1, we learned how to write simple Python programs using the print() function.
This week, we will learn how to:
Store information in memory
Accept user input
Perform calculations
Make decisions
Repeat tasks
Organize code using functions
These concepts form the foundation of programming and will prepare us for Object-Oriented
Programming in Week 3.
1. Variables
What is a Variable?
A variable is a named storage location used to hold data in memory.
Think of a variable as a labeled container that stores information.
Example:
Name = John
Age = 20
Course = ICT
In Python:
name = "John"
age = 20
course = "ICT"
Rules for Naming Variables
Valid Variable Names
student_name = "John"
age = 20
course1 = "ICT"
total_marks = 450
Invalid Variable Names
1name = "John"
student-name = "John"
class = "ICT"
Why?
Cannot start with a number.
Cannot contain special symbols like (-).
Cannot use Python keywords.
Displaying Variables
name = "John"
age = 20
print(name)
print(age)
Output:
John
20
Example Program
student_name = "Mary"
age = 19
course = "Business Management"
institution = "ABC College"
print("Student Information")
print("---------------------")
print("Name:", student_name)
print("Age:", age)
print("Course:", course)
print("Institution:", institution)
Output:
Student Information
---------------------
Name: Mary
Age: 19
Course: Business Management
Institution: ABC College
2. Data Types
What is a Data Type?
A data type specifies the kind of value stored in a variable.
Common Python Data Types:
Data Type Description Example
str Text/String "John"
int Whole Number 20
float Decimal Number 75.5
bool True/False True
String Data Type
Strings contain text enclosed in quotation marks.
name = "John"
course = "ICT"
print(name)
print(course)
Integer Data Type
Stores whole numbers.
age = 18
marks = 450
print(age)
print(marks)
Float Data Type
Stores decimal numbers.
height = 1.75
price = 1500.50
print(height)
print(price)
Boolean Data Type
Stores either True or False.
is_registered = True
fees_cleared = False
print(is_registered)
print(fees_cleared)
Checking Data Types
Use the type() function.
name = "John"
age = 20
height = 1.75
print(type(name))
print(type(age))
print(type(height))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
3. User Input
What is User Input?
User input allows users to enter data while a program is running.
Python uses the input() function.
Example 1
name = input("Enter your name: ")
print("Welcome", name)
Sample Output:
Enter your name: John
Welcome John
Example 2
course = input("Enter your course: ")
institution = input("Enter institution: ")
print("Course:", course)
print("Institution:", institution)
Example 3: Student Registration
print("STUDENT REGISTRATION")
name = input("Enter student name: ")
age = input("Enter age: ")
course = input("Enter course: ")
print("\nREGISTRATION DETAILS")
print("--------------------")
print("Name:", name)
print("Age:", age)
print("Course:", course)
Output:
STUDENT REGISTRATION
Enter student name: Mary
Enter age: 19
Enter course: ICT
REGISTRATION DETAILS
--------------------
Name: Mary
Age: 19
Course: ICT
4. Operators
What are Operators?
Operators perform calculations and comparisons.
Arithmetic Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
// Floor Division
** Power
Example Program
num1 = 20
num2 = 10
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)
Output:
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2.0
Student Fee Calculator
tuition_fee = 25000
registration_fee = 2000
library_fee = 1500
total_fee = tuition_fee + registration_fee + library_fee
print("Fee Structure")
print("----------------")
print("Tuition Fee:", tuition_fee)
print("Registration Fee:", registration_fee)
print("Library Fee:", library_fee)
print("Total Fee:", total_fee)
5. Conditional Statements
What is a Conditional Statement?
A conditional statement allows a program to make decisions.
Python uses:
if
elif
else
Example 1
age = 20
if age >= 18:
print("You are an adult")
Example 2
marks = 75
if marks >= 50:
print("Pass")
else:
print("Fail")
Example 3: Student Grade Checker
marks = int(input("Enter marks: "))
if marks >= 80:
print("Grade A")
elif marks >= 70:
print("Grade B")
elif marks >= 60:
print("Grade C")
elif marks >= 50:
print("Grade D")
else:
print("Fail")
Example 4: Voting Eligibility
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
6. Loops
What is a Loop?
A loop repeats a block of code multiple times.
Types of loops:
1. For Loop
2. While Loop
For Loop
Example:
for number in range(1, 6):
print(number)
Output:
Display Student Numbers
for student in range(1, 11):
print("Student", student)
Output:
Student 1
Student 2
Student 3
...
Student 10
Multiplication Table
number = int(input("Enter a number: "))
for i in range(1, 13):
print(number, "x", i, "=", number * i)
Sample Output:
5x1=5
5 x 2 = 10
5 x 3 = 15
...
5 x 12 = 60
While Loop
Example:
count = 1
while count <= 5:
print(count)
count += 1
Output:
Countdown Program
count = 10
while count >= 1:
print(count)
count -= 1
print("Blast Off!")
7. Functions
What is a Function?
A function is a block of code that performs a specific task.
Functions help avoid repetition.
Creating a Function
def greet():
print("Welcome to Python Programming")
greet()
Output:
Welcome to Python Programming
Function with Parameters
def greet(name):
print("Welcome", name)
greet("John")
greet("Mary")
Output:
Welcome John
Welcome Mary
Function Returning a Value
def add_numbers(num1, num2):
total = num1 + num2
return total
result = add_numbers(10, 20)
print("Total =", result)
Output:
Total = 30
Practical Activity 1: Student Grade Calculator
name = input("Enter student name: ")
marks1 = int(input("Enter CAT marks: "))
marks2 = int(input("Enter Exam marks: "))
total = marks1 + marks2
print("\nRESULTS")
print("Student:", name)
print("Total Marks:", total)
if total >= 50:
print("Status: PASS")
else:
print("Status: FAIL")
Practical Activity 2: Simple Login Program
username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin" and password == "1234":
print("Login Successful")
else:
print("Invalid Username or Password")
Practical Activity 3: Student Registration System
def register_student():
print("STUDENT REGISTRATION SYSTEM")
print("----------------------------")
name = input("Enter student name: ")
age = input("Enter age: ")
course = input("Enter course: ")
print("\nREGISTRATION SUCCESSFUL")
print("------------------------")
print("Name:", name)
print("Age:", age)
print("Course:", course)
register_student()
Common Errors
Syntax Error
print("Hello"
Indentation Error
if age > 18:
print("Adult")
Name Error
print(student_name)
When student_name has not been defined.
Exercises
1. Create a program that stores and displays your personal information.
2. Create a calculator that performs addition, subtraction, multiplication, and division.
3. Create a grading system using if-elif-else.
4. Create a multiplication table generator.
5. Create a function that calculates the area of a rectangle.
6. Create a login system that checks username and password.
7. Create a student registration system using functions.
Lesson Summary
In this lesson, we learned:
✓ Variables
✓ Data Types
✓ User Input
✓ Arithmetic Operators
✓ Conditional Statements
✓ For Loops
✓ While Loops
✓ Functions
These concepts are essential because they form the foundation upon which we will build our
first Object-Oriented Python program in Week 3: Introduction to OOP, Classes, and Objects.