0% found this document useful (0 votes)
3 views11 pages

Module1 1

Uploaded by

Chisale Mwale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Module1 1

Uploaded by

Chisale Mwale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Programming Fundamentals

Training Module

Trainer: George Mugala


Department of Electrical/Electronics
Copperbelt University

Course Code: CS2111-Python


Duration: 40 Hours
Credit Hours: 3

December 16, 2025


Python Programming Fundamentals George Mugala - Copperbelt University

Contents

Course Overview 2

1 Introduction to Python 3
1.1 What is Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Python Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Python Basics 3
2.1 Your First Python Program . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Variables and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 Basic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3.2 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . 4

3 Control Structures 4
3.1 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.2 Looping Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

4 Data Structures 5
4.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4.2 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.4 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

5 Functions 7

6 File Handling 8

7 Error Handling 8

8 Practical Exercises 8
8.1 Exercise 1: Student Grade Calculator . . . . . . . . . . . . . . . . . . . . 8
8.2 Exercise 2: Library Management System . . . . . . . . . . . . . . . . . . 9

9 Assessment 9
9.1 Continuous Assessment (40%) . . . . . . . . . . . . . . . . . . . . . . . . 9
9.2 Final Examination (60%) . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

10 Resources 9
10.1 Recommended Textbooks . . . . . . . . . . . . . . . . . . . . . . . . . . 9
10.2 Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
10.3 Software Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Contact Information 10

1
Python Programming Fundamentals George Mugala - Copperbelt University

Course Overview
This module introduces students to the fundamentals of Python programming language.
Python is a versatile, high-level programming language known for its simplicity and
readability. This course covers basic syntax, data structures, control flow, functions, and
file handling.

Learning Outcomes
Upon completion of this module, students will be able to:

ˆ Understand Python syntax and semantics

ˆ Write Python programs using fundamental programming constructs

ˆ Implement data structures (lists, tuples, dictionaries, sets)

ˆ Create and use functions and modules

ˆ Handle files and exceptions

ˆ Solve basic computational problems using Python

Prerequisites
ˆ Basic computer literacy

ˆ Understanding of mathematical concepts

ˆ No prior programming experience required

2
Python Programming Fundamentals George Mugala - Copperbelt University

1 Introduction to Python
1.1 What is Python?
Python is an interpreted, high-level, general-purpose programming language created by
Guido van Rossum and first released in 1991. Its design philosophy emphasizes code
readability with its notable use of significant whitespace.

1.2 Python Features


ˆ Easy to learn and use

ˆ Interpreted language

ˆ Cross-platform compatibility

ˆ Large standard library

ˆ Supports multiple programming paradigms

ˆ Open source and community-driven

1.3 Installing Python


1. Download Python from [Link]

2. Verify installation: python --version

3. Install an IDE (PyCharm, VS Code, or Jupyter Notebook)

2 Python Basics
2.1 Your First Python Program

1 # This is a comment
2 print ( " Hello , Copperbelt University ! " )
3 print ( " Welcome to Python Programming " )

Listing 1: Hello World Program

2.2 Variables and Data Types

1 # Variable declaration
2 student_name = " John Banda "
3 age = 20
4 height = 1.75
5 is_student = True
6
7 # Display variables
8 print ( " Name : " , student_name )
9 print ( " Age : " , age )
10 print ( " Type of age : " , type ( age ) )
11 print ( " Height : " , height )

3
Python Programming Fundamentals George Mugala - Copperbelt University

12 print ( " Is Student : " , is_student )


13
14 # Multiple assignment
15 x , y , z = 10 , 20 , 30
16 print ( f " x ={ x } , y ={ y } , z ={ z } " )

Listing 2: Variables and Data Types

2.3 Basic Operators


2.3.1 Arithmetic Operators

1 a = 10
2 b = 3
3

4 print ( " Addition : " , a + b ) # 13


5 print ( " Subtraction : " , a - b ) # 7
6 print ( " Multiplication : " , a * b ) # 30
7 print ( " Division : " , a / b ) # 3.333...
8 print ( " Floor Division : " , a // b ) # 3
9 print ( " Modulus : " , a % b ) # 1
10 print ( " Exponent : " , a ** b ) # 1000

2.3.2 Comparison Operators

1 x = 10
2 y = 20
3
4 print ( " x == y : " , x == y ) # False
5 print ( " x != y : " , x != y ) # True
6 print ( " x > y:", x > y) # False
7 print ( " x < y:", x < y) # True
8 print ( " x >= y : " , x >= y ) # False
9 print ( " x <= y : " , x <= y ) # True

3 Control Structures
3.1 Conditional Statements

1 # Simple if statement
2 marks = 75
3
4 if marks >= 75:
5 print ( " Grade : Distinction " )
6 elif marks >= 60:
7 print ( " Grade : Credit " )
8 elif marks >= 50:
9 print ( " Grade : Pass " )
10 else :
11 print ( " Grade : Fail " )
12
13 # Nested if statement
14 age = 18

4
Python Programming Fundamentals George Mugala - Copperbelt University

15 has_id = True
16
17 if age >= 18:
18 if has_id :
19 print ( " You can vote ! " )
20 else :
21 print ( " You need ID to vote " )
22 else :
23 print ( " Too young to vote " )

Listing 3: If-Elif-Else Statements

3.2 Looping Structures

1 # For loop example


2 print ( " Counting 1 to 5: " )
3 for i in range (1 , 6) :
4 print ( i )
5
6 # While loop example
7 count = 1
8 print ( " \ nCounting with while loop : " )
9 while count <= 5:
10 print ( count )
11 count += 1
12
13 # Loop control statements
14 print ( " \ nBreak and Continue : " )
15 for i in range (1 , 11) :
16 if i == 5:
17 continue # Skip 5
18 if i == 8:
19 break # Stop at 8
20 print ( i )

Listing 4: For and While Loops

4 Data Structures
4.1 Lists

1 # Creating lists
2 students = [ " John " , " Mary " , " Peter " , " Alice " ]
3 marks = [85 , 92 , 78 , 95]
4 mixed_list = [1 , " hello " , 3.14 , True ]
5

6 # List operations
7 print ( " Original list : " , students )
8 print ( " First student : " , students [0])
9 print ( " Last student : " , students [ -1])
10 print ( " Slice : " , students [1:3])
11
12 # List methods
13 students . append ( " David " )
14 students . insert (2 , " Grace " )

5
Python Programming Fundamentals George Mugala - Copperbelt University

15 students . remove ( " Peter " )


16 students . sort ()
17
18 print ( " Sorted list : " , students )
19 print ( " List length : " , len ( students ) )
20
21 # List comprehension
22 squares = [ x **2 for x in range (1 , 6) ]
23 print ( " Squares : " , squares )

Listing 5: List Operations

4.2 Tuples

1 # Tuples are immutable


2 student_record = ( " John Banda " , 20 , " Computer Science " )
3 coordinates = (10.5 , 20.3)
4

5 print ( " Student Name : " , student_record [0])


6 print ( " Age : " , student_record [1])
7
8 # Tuple unpacking
9 name , age , course = student_record
10 print ( f " { name } is { age } years old studying { course } " )

Listing 6: Tuple Examples

4.3 Dictionaries

1 # Creating dictionaries
2 student = {
3 " name " : " Mary Phiri " ,
4 " age " : 21 ,
5 " course " : " Information Technology " ,
6 " year " : 2 ,
7 " grades " : { " math " : 85 , " programming " : 92}
8 }
9
10 # Accessing values
11 print ( " Student Name : " , student [ " name " ])
12 print ( " Course : " , student . get ( " course " , " Not specified " ) )
13
14 # Adding / updating
15 student [ " email " ] = " mary . phiri@students . cbu . ac . zm "
16 student [ " age " ] = 22
17
18 # Iterating through dictionary
19 print ( " \ nStudent Information : " )
20 for key , value in student . items () :
21 print ( f " { key }: { value } " )

Listing 7: Dictionary Operations

6
Python Programming Fundamentals George Mugala - Copperbelt University

4.4 Sets

1 # Creating sets
2 set1 = {1 , 2 , 3 , 4 , 5}
3 set2 = {4 , 5 , 6 , 7 , 8}
4
5 print ( " Set 1: " , set1 )
6 print ( " Set 2: " , set2 )
7
8 # Set operations
9 print ( " Union : " , set1 | set2 )
10 print ( " Intersection : " , set1 & set2 )
11 print ( " Difference ( set1 - set2 ) : " , set1 - set2 )
12 print ( " Symmetric Difference : " , set1 ^ set2 )

Listing 8: Set Operations

5 Functions

1 # Basic function
2 def greet ( name ) :
3 " " " This function greets the person " " "
4 return f " Hello , { name }! "
5

6 # Function with multiple parameters


7 def calculate_grade ( marks ) :
8 if marks >= 75:
9 return " Distinction "
10 elif marks >= 60:
11 return " Credit "
12 elif marks >= 50:
13 return " Pass "
14 else :
15 return " Fail "
16
17 # Function with default arguments
18 def register_student ( name , age , course = " Computer Science " ) :
19 return {
20 " name " : name ,
21 " age " : age ,
22 " course " : course
23 }
24
25 # Using functions
26 print ( greet ( " George " ) )
27 print ( " Grade : " , calculate_grade (82) )
28 student1 = register_student ( " John Banda " , 20)
29 print ( " Registered Student : " , student1 )
30
31 # Lambda function
32 square = lambda x : x * x
33 print ( " Square of 5: " , square (5) )

Listing 9: Function Examples

7
Python Programming Fundamentals George Mugala - Copperbelt University

6 File Handling

1 # Writing to a file
2 with open ( " students . txt " , " w " ) as file :
3 file . write ( " John Banda , Computer Science , Year 2\ n " )
4 file . write ( " Mary Phiri , Information Technology , Year 3\ n " )
5 file . write ( " Peter Mwamba , Electrical Engineering , Year 1\ n " )
6
7 # Reading from a file
8 print ( " Student Records : " )
9 with open ( " students . txt " , " r " ) as file :
10 for line in file :
11 print ( line . strip () )
12
13 # Appending to a file
14 with open ( " students . txt " , " a " ) as file :
15 file . write ( " Alice Zulu , Mechanical Engineering , Year 4\ n " )

Listing 10: File Operations

7 Error Handling

1 try :
2 # Attempt to open non - existent file
3 with open ( " nonexistent . txt " , " r " ) as file :
4 content = file . read ()
5 except Fil eNotF oundEr ror :
6 print ( " Error : File not found ! " )
7 except PermissionError :
8 print ( " Error : Permission denied ! " )
9 except Exception as e :
10 print ( f " An error occurred : { e } " )
11 else :
12 print ( " File read successfully ! " )
13 finally :
14 print ( " This always executes " )

Listing 11: Exception Handling

8 Practical Exercises
8.1 Exercise 1: Student Grade Calculator
Write a Python program that:

1. Takes input for 5 subjects

2. Calculates total and average marks

3. Determines grade based on average

4. Displays results in a formatted table

8
Python Programming Fundamentals George Mugala - Copperbelt University

8.2 Exercise 2: Library Management System


Create a simple library system that:
ˆ Stores book information (title, author, ISBN, availability)
ˆ Allows adding, removing, and searching books
ˆ Tracks borrowed books
ˆ Saves data to a file

9 Assessment
9.1 Continuous Assessment (40%)
ˆ Weekly quizzes (10%)
ˆ Programming assignments (20%)
ˆ Mid-term project (10%)

9.2 Final Examination (60%)


ˆ Theory questions (20%)
ˆ Practical programming test (40%)

10 Resources
10.1 Recommended Textbooks
1. ”Python Crash Course” by Eric Matthes
2. ”Automate the Boring Stuff with Python” by Al Sweigart
3. ”Learning Python” by Mark Lutz

10.2 Online Resources


ˆ Official Python Documentation: [Link]
ˆ W3Schools Python Tutorial: [Link]
ˆ Codecademy Python Course: [Link]
ˆ Python for Everybody: [Link]

10.3 Software Requirements


ˆ Python 3.8 or higher
ˆ Visual Studio Code with Python extension
ˆ Jupyter Notebook
ˆ Git for version control

9
Python Programming Fundamentals George Mugala - Copperbelt University

Contact Information
Trainer: George Mugala
Department: Electrical/Electronics
Institution: Copperbelt University
Email: [Link]@[Link]
Office Hours: Monday-Friday, 10:00-16:00
Location: Electrical/Electronics department,

”Programming isn’t about what you know; it’s about


what you can figure out.”

10

You might also like