What is Python?
Python is a high-level, interpreted programming language known for its clean syntax and readability. Created by Guido van Rossum in 1991, Python has become one of the most popular languages in the world, used for web development, data science, AI, automation, and more.
The Python Philosophy (The Zen of Python)
import this
# Beautiful is better than ugly.
# Explicit is better than implicit.
# Simple is better than complex.
# Readability counts.
Installing Python
Download Python from python.org. Verify installation:
python --version # or python3 --version
# Python 3.12.0
Your First Program
print("Hello, World!")
Run it:
python hello.py
# Hello, World!
Python Interactive Shell (REPL)
>>> 2 + 3
5
>>> "Hello" + " " + "World"
'Hello World'
>>> type(42)
<class 'int'>
Comments
# This is a single-line comment
"""
This is a
multi-line comment (docstring)
"""
x = 5 # inline comment
Python Indentation
Python uses indentation (spaces/tabs) to define code blocks โ no curly braces!
if True:
print("This is indented") # 4 spaces
print("Still in the block")
print("Outside the block")
Exercises
Exercise 1: Hello World Variations
Write Python code that prints each of the following on separate lines:
- Your name
- Your favorite programming language
- The result of 100 divided by 4
Solution:
print("Alice")
print("Python")
print(100 / 4) # 25.0
Exercise 2: Python as a Calculator
Use the Python shell to compute: (15 + 25) * 3 - 10 / 2
Solution:
result = (15 + 25) * 3 - 10 / 2
print(result) # 115.0
Exercise 3: Fix the Indentation
The following code has an indentation error. Fix it:
# Broken:
if True:
print("Hello")
# Fixed:
if True:
print("Hello")