Python Syntax & Basic Operations
Learn the foundational syntax, variables, operators, and basic input/output in Python.
The Zen of Python
Before diving into code, it's essential to understand Python's philosophy. Python emphasizes readability and simplicity.
You can see the guiding principles by running import this in a Python interpreter.
Basic Syntax
Python uses whitespace (indentation) to define code blocks, unlike languages that use curly braces {}.
This makes Python code visually clean and consistent.
# This is a comment
print("Hello, Python!")
if True:
print("Indentation matters!")
print("Hello, Python!")
if True:
print("Indentation matters!")
Variables & Data Types
Python is dynamically typed, meaning you don't need to declare the type of a variable when you create one.
name = "Alice" # String
age = 30 # Integer
pi = 3.14159 # Float
is_learning = True # Boolean
age = 30 # Integer
pi = 3.14159 # Float
is_learning = True # Boolean
Basic Operations
Python supports standard arithmetic, comparison, and logical operators.
# Arithmetic
total = 10 + 5 * 2
remainder = 10 % 3
power = 2 ** 3 # 8
# Comparison
is_greater = 10 > 5
is_equal = (10 == 10)
total = 10 + 5 * 2
remainder = 10 % 3
power = 2 ** 3 # 8
# Comparison
is_greater = 10 > 5
is_equal = (10 == 10)
String Formatting (f-strings)
Modern Python (3.6+) uses f-strings for clean and readable string interpolation.
user = "Pranav"
msg = f"Welcome, {user}! Today is {20 + 5} degrees."
print(msg)
msg = f"Welcome, {user}! Today is {20 + 5} degrees."
print(msg)
โ Practice (10 minutes)
- Open your terminal and run
python3. - Create variables for
first_nameandlast_name. - Use an f-string to print
"My name is [first_name] [last_name]". - Calculate the area of a circle with
radius = 5(Area = ฯ * rยฒ).