Chapter 3: Basic Syntax
Your First Python Program
Type python3 in your terminal to enter interactive mode, then type:
bash
>>> print("Hello, world!")
>>> print("Hello, World!")bash
Hello, world!
Hello, World!Congratulations! You just completed the "Hello World" ritual of programming 🎉
Variables — Naming Your Data
Variables are like labeled storage boxes — the label is the variable name, and what's inside the box is the data.
bash
# Variable assignment
name = "Alice" # String
age = 18 # Integer
height = 175.5 # Float
is_student = True # Boolean
print(f"My name is {name}, I am {age} years old, and I am {height} cm tall")bash
My name is Alice, I am 18 years old, and I am 175.5 cm tall📝 Note: 📝 Naming rules: Can only contain letters, numbers, and underscores
_. Cannot start with a number (2name❌,name2✅). Case-sensitive (Ageandageare different variables). Cannot use Python keywords (likeif,for,class). Recommended: lowercase with underscores (snake_case):my_name.
Data Types
bash
# Basic data types in Python
text = "Hello" # str - string
number = 42 # int - integer
price = 9.99 # float - floating point
is_open = True # bool - boolean
nothing = None # NoneType - null value
# Use type() to check type
print(type(text))
print(type(number))
print(type(price))
print(type(is_open))bash
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>bash
# Type conversion
age_str = "25"
age_int = int(age_str) # String → Integer
age_float = float(age_str) # String → Float
age_back = str(age_int) # Integer → String
print(f"Type conversion example: {age_int} → {age_float} → {age_back}")bash
Type conversion example: 25 → 25.0 → 25Operators
bash
# Arithmetic operators
print(f"Addition: 10 + 3 = {10 + 3}")
print(f"Subtraction: 10 - 3 = {10 - 3}")
print(f"Multiplication: 10 * 3 = {10 * 3}")
print(f"Division: 10 / 3 = {10 / 3}")
print(f"Floor division: 10 // 3 = {10 // 3}")
print(f"Modulo: 10 % 3 = {10 % 3}")
print(f"Exponentiation: 2 ** 10 = {2 ** 10}")
# Comparison operators
print(f"\n5 > 3 → {5 > 3}")
print(f"5 == 5 → {5 == 5}")
print(f"5 != 3 → {5 != 3}")
# Logical operators
print(f"\nTrue and False → {True and False}")
print(f"True or False → {True or False}")
print(f"not True → {not True}")bash
Addition: 10 + 3 = 13
Subtraction: 10 - 3 = 7
Multiplication: 10 * 3 = 30
Division: 10 / 3 = 3.3333333333333335
Floor division: 10 // 3 = 3
Modulo: 10 % 3 = 1
Exponentiation: 2 ** 10 = 1024
5 > 3 → True
5 == 5 → True
5 != 3 → True
True and False → False
True or False → True
not True → FalseInput and Output
bash
# Output: print() function
print("Hello", "World", sep="-", end="!\n")
# Input: input() function (note: return value is always a string)
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Remember to convert type
print(f"Hello, {name}! Next year you'll be {age + 1} years old.")bash
Hello-World!
Enter your name: Bob
Enter your age: 20
Hello, Bob! Next year you'll be 21 years old.💡 Tip: 💡 f-string magic: f-strings introduced in Python 3.6+ are the best way to format strings. Just add
fbefore a string, and you can embed any expression using{}.