Introduction to Programming Class Notes
Page 1: Basics of Python Syntax
Python is a high-level, interpreted programming language known for its readability and versatility.
Unlike compiled languages, Python executes code line-by-line, making debugging easier for
beginners. Variables in Python are dynamically typed, meaning you don’t need to declare data types
explicitly. For example, name = "Alice" assigns a string, while age = 25 assigns an integer. Basic data
types include strings (text), integers (whole numbers), floats (decimals), and booleans (True/False).
Input and output functions like print() and input() allow interaction. A simple "Hello World" program
would be:
print("Hello, World!")
user_name = input("Enter your name: ")
print("Welcome,", user_name)
Common errors include syntax errors (e.g., missing colons or parentheses) and runtime errors (e.g.,
dividing by zero). Always check indentation, as Python uses whitespace to define code blocks.
Page 2: Control Structures
Control structures direct the flow of a program. Conditional statements like if, elif, and else
execute code based on Boolean conditions. For example:
temperature = 30
if temperature > 25:
print("It's a hot day.")
else:
print("Cool weather!")
Loops repeat actions: for loops iterate over sequences (e.g., lists), while while loops run until
a condition is false. Use break to exit a loop early or continue to skip iterations. Logical
operators (and, or, not) combine conditions.
A practical exercise is building a number-guessing game:
import random
target = random.randint(1, 10)
guess = 0
while guess != target:
guess = int(input("Guess a number (1-10): "))
print("Correct!")
Page 3: Functions and Modules
Functions encapsulate reusable code. Define them with def, followed by parameters:
def add_numbers(a, b):
return a + b
Parameters are variables in the function definition; arguments are the actual values passed.
Modules like math (for sqrt, sin) or random (for randint, choice) extend functionality. Import
them with import module_name.
Variable scope determines accessibility: variables inside a function are local, while those
declared outside are global. For example:
x = 10 # Global
def test():
y = 5 # Local
print(x + y)
test()
Practice by creating a calculator that uses functions for addition, subtraction, etc.