Python2 vs Python3
Python3 is the current version of Python. It is not backward-compatible with Python2, which is no longer maintained.
Key Differences
# Print is a function in Python3
print("Hello") # Python3
# print "Hello" # Python2 (syntax error in 3)
# Integer division
print(5 / 2) # 2.5 in Python3 (was 2 in Python2)
print(5 // 2) # 2 (floor division)
# Unicode strings
name = "Alice" # Unicode by default in Python3
Modern Python3 Features
# f-strings (Python 3.6+)
name = "Alice"
print(f"Hello, {name}!")
# Walrus operator (Python 3.8+)
if (n := len("hello")) > 3:
print(f"Length is {n}")
# Match statement (Python 3.10+)
match command:
case "start":
print("Starting")
case "stop":
print("Stopping")
case _:
print("Unknown")
Type Hints (Python 3.5+)
def greet(name: str) -> str:
return f"Hello, {name}!"
age: int = 25
names: list = ["Alice", "Bob"]
Summary
- Python3 print() is a function, not a statement
- Division / returns float, use // for integer division
- f-strings provide easy string formatting
- match/case added in Python 3.10
YouTip