Python Syntax and Variables

Python's syntax is designed for readability. Whitespace (indentation) is meaningful — it replaces the braces {} used by other languages.

Indentation

Python
# Python uses indentation (4 spaces) instead of braces
def greet(name):
    if name:
        print(f"Hello, {name}!")
    else:
        print("Hello, stranger!")

# This is NOT just style — it's syntax!
# Wrong (IndentationError):
# if True:
# print("hello")  # IndentationError!

# Correct:
if True:
    print("hello")

Variables and Naming

Python
# Variable naming rules:
# 1. Must start with a letter or underscore
# 2. Can contain letters, numbers, underscores
# 3. Case-sensitive
# 4. Cannot be a Python keyword

# Good naming (snake_case — Python convention)
user_name = "Alice"
max_retry_count = 3
is_active = True

# Bad naming
# userName = "Alice"   # camelCase — not Pythonic
# MAX_RETRIES = 3      # SCREAMING_SNAKE — only for constants
# 2nd_place = "Bob"    # Can't start with a number

# Python keywords (cannot use as variable names)
import keyword
print(keyword.kwlist)
# ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await',
#  'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except',
#  'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
#  'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return',
#  'try', 'while', 'with', 'yield']

Numbers

Python
# Integers (unlimited size!)
big_number = 10**100  # Googol — Python handles this natively!
hex_num = 0xFF         # 255
oct_num = 0o17         # 15
bin_num = 0b1010       # 10

# Floats
pi = 3.14159
scientific = 1.5e10    # 15000000000.0

# Arithmetic
print(10 + 3)    # 13  (addition)
print(10 - 3)    # 7   (subtraction)
print(10 * 3)    # 30  (multiplication)
print(10 / 3)    # 3.333... (true division — always float)
print(10 // 3)   # 3   (floor division)
print(10 % 3)    # 1   (modulo/remainder)
print(10 ** 3)   # 1000 (exponentiation)

# Math module
import math
print(math.sqrt(16))     # 4.0
print(math.ceil(3.2))    # 4
print(math.floor(3.8))   # 3
print(math.pi)           # 3.141592653589793

# Decimal (for precise financial calculations)
from decimal import Decimal
price = Decimal('19.99')
tax = Decimal('0.08')
total = price * tax
print(total)  # Decimal('1.5992') — exact, not floating point error

Booleans

Python
# Booleans are a subclass of integers!
print(True + True)    # 2
print(True * 10)      # 10
print(False + 1)      # 1

# Comparison operators (return booleans)
print(5 == 5)    # True  (equal)
print(5 != 3)    # True  (not equal)
print(5 > 3)     # True  (greater than)
print(5 < 3)     # False (less than)
print(5 >= 5)    # True  (greater or equal)
print(5 <= 3)    # False (less or equal)

# Logical operators
print(True and False)   # False (both must be True)
print(True or False)    # True  (at least one must be True)
print(not True)         # False (inverts)

# Chained comparisons (Pythonic!)
age = 25
print(18 <= age <= 65)  # True (same as: 18 <= age and age <= 65)

Input and Output

Python
# Getting user input (always returns a string!)
name = input("What is your name? ")
print(f"Hello, {name}!")

# Convert input to other types
age = int(input("How old are you? "))
height = float(input("Your height in meters: "))

# Print options
print("Hello", "World", sep=", ")   # Hello, World
print("Hello", end=" ")             # No newline at end
print("World")                      # Hello World (on same line)

Knowledge Check

4 questions — test your understanding

1

What is the output of: print(10 / 3)?

2

Which naming convention does Python prefer for variables?

3

What does input() always return in Python?

4

What is the output of: print(10 // 3)?