CS_Liberia_Python_Basics(Week_2-3)[1]
CS_Liberia_Python_Basics(Week_2-3)[1]
functions
Weeks 1-2: Basics of Python
CS4LIBERIA
www.cs4lib.com
Course Overview
Topics covered in Weeks 3-4:
Conditional Statements (if, elif, else)
Loops (for, while)
Functions (defining and calling
functions)
Modules (importing and using built-
in/custom modules)
Learning objectives:
Control the flow of a program with
conditions and loops
Write reusable functions to simplify
code
Work with Python modules to
enhance functionality
Conditional Statements (if, elif,
else)
Used for decision-making in programs
Exercises:
Exercises:
1. Write a program that checks if a
number is positive, negative, or zero.
2. Create a program that asks for a user's
score and prints the grade (A, B, C, etc.).
3. Build a simple login system that asks for
a username and password.
4. Write a program that checks if a year is
a leap year.
Loops (for and while)
for loop: Used to iterate over sequences
(lists, strings, etc.)
Defining a function:
Calling a function:
Importing modules:
Importing it:
Exercises:
1. Import and use the random module to
generate a random number.
2. Use the datetime module to print
today’s date.
3. Write a program that imports a custom
module with a function to find the
maximum of three numbers.
4. Create a module with functions for basic
math operations and import it into
another script.
Thank You
Questions?
Keep coding and
practicing!
Additional resources:
Python Documentation
W3Schools Python
Codecademy Python
Course
https://realpython.com/