Python Syntax
Master the fundamental rules and structure of Python programming
๐ Understanding Python Syntax
Python syntax refers to the set of rules that defines how a Python program will be written and interpreted. Python's syntax is designed to be readable and straightforward, making it an excellent choice for beginners and experienced programmers alike.
variable_name = "Value"
Basic Syntax Rules
Case Sensitive
Python distinguishes between uppercase and lowercase letters
name = "Alice"
Name = "Bob"
print(name) # Alice
print(Name) # Bob
Indentation Matters
Python uses indentation to define code blocks
if 5 > 2:
print("Five is greater than two!")
print("This is also indented")
No Semicolons Required
Each statement typically goes on its own line
x = 5
y = 10
z = x + y
print(z)
Dynamic Typing
Variables don't need explicit type declarations
x = 5 # integer
x = "Hello" # now it's a string
x = 3.14 # now it's a float
๐น Case Sensitivity
Python distinguishes between uppercase and lowercase letters
x = 5
X = 10
print(x) # 5
print(X) # 10
๐น Indentation
Python uses indentation to define code blocks
Indentation is one of Python's most distinctive features. It's not just for readabilityโit's part of the syntax!
if 5 > 2:
print("Greater")
Note: Indentation is required. No braces `{}` used.
โ ๏ธ Common Indentation Errors
# This will cause an IndentationError
if 5 > 2:
print("This line should be indented!")
# This will also cause an error - inconsistent indentation
if 5 > 2:
print("Four spaces")
print("Eight spaces - inconsistent!")
๐น No Semicolons
Each statement typically goes on its own line
x = 5
y = 10
print(x + y)
๐น Dynamic Typing
Variables don't need explicit type declarations
a = 10 # int
a = "text" # now string
๐นVariables and Naming Conventions
Python has specific rules for naming variables and follows conventions that make code more readable.
โ Valid Variable Names
name = "Alice"
age = 25
user_name = "alice123"
firstName = "Alice" # camelCase
first_name = "Alice" # snake_case (preferred)
_private = "hidden"
PI = 3.14159 # constants in UPPERCASE
โ Invalid Variable Names
# These will cause SyntaxError
2name = "Alice" # starts with number
first-name = "Alice" # contains hyphen
class = "Python" # reserved keyword
my name = "Alice" # contains space
๐น Naming Conventions
- Variables/Functions: snake_case
- Constants: UPPER_CASE
- Classes: PascalCase
- Private: _underscore
๐น Statements vs Expressions
Instructions that perform actions
Expressions that return values
# Assignment statement
x = 5
# Print statement
print("Hello, World!")
# If statement
if x > 0:
print("Positive number")
# Function definition statement
def greet():
print("Hello!")
๐น Line Continuation
Sometimes you need to split long lines of code for better readability.
total = (1 + 2 +
3 + 4) # Using parentheses
text = "This is a very " \
"long string" # Using backslash
๐น String Concatenation
Python allows you to concatenate strings in multiple ways.
greeting = "Hello" + " " + "World"
# Multi-line string concatenation
message = ("Hello "
"World "
"Python")
๐น Multiple Assignments
Python allows you to assign multiple variables in a single line or across multiple lines.
# Single line assignment
x, y, z = 1, 2, 3
# Multi-line assignment
a, b, c = (1,
2,
3)
๐น Multiple Statements (not recommended)
While not recommended for readability, you can put multiple statements on one line using semicolons.
x = 1; y = 2; print(x + y)
๐น Practice Example
name = "Alice"
age = 22
print("Name:", name)
print("Age:", age)