Class Notes: Fundamentals of Python
Programming
1 Introduction to Python
Python is a high-level, interpreted programming language known for its read-
ability and versatility. It is widely used in web development, data science, au-
tomation, and more.
2 Basic Syntax
Python uses indentation to define code blocks, unlike other languages that use
braces or keywords.
2.1 Variables and Data Types
Variables in Python are dynamically typed, meaning you don’t need to declare
their type.
# Examples of variables
name = ”Alice” # String
age = 20 # Integer
height = 5.7 # Float
is_student = True # Boolean
2.2 Basic Operations
• Arithmetic: +, −, ∗, /, //, %, ∗∗
• Comparison: ==, ! =, <, >, <=, >=
• Logical: and, or, not
# Arithmetic example
x = 10
y = 3
print(x + y) # Output: 13
print(x // y) # Output: 3 (floor division)
1
3 Control Structures
Control structures manage the flow of a program.
3.1 Conditionals
age = 18
if age >= 18:
print(”Adult”)
else:
print(”Minor”)
3.2 Loops
• For Loop: Iterates over a sequence.
• While Loop: Repeats as long as a condition is true.
# For loop example
for i in range(5):
print(i) # Outputs: 0, 1, 2, 3, 4
# While loop example
count = 0
while count < 5:
print(count)
count += 1
4 Functions
Functions are defined using the def keyword.
def greet(name):
return f”Hello,␣{name}!”
print(greet(”Alice”)) # Output: Hello, Alice!
5 Lists and Dictionaries
5.1 Lists
Lists are ordered, mutable collections.
fruits = [”apple”, ”banana”, ”cherry”]
fruits.append(”orange”) # Add item
print(fruits[1]) # Output: banana
2
5.2 Dictionaries
Dictionaries store key-value pairs.
student = {”name”: ”Alice”, ”age”: 20}
print(student[”name”]) # Output: Alice
student[”grade”] = ”A” # Add key-value pair
6 Key Points to Remember
• Python is dynamically typed and uses indentation for code blocks.
• Basic data types include integers, floats, strings, and booleans.
• Control structures like conditionals and loops manage program flow.
• Functions, lists, and dictionaries are fundamental for organizing code and
data.
Homework: Write a Python program to calculate the factorial of a number using
a loop.