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" 
        
Clean
Readable
Simple
Structure
Elegant
Design

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)

๐Ÿง  Test Your Knowledge

What is the standard indentation size in Python according to PEP 8?

Which of the following is a valid Python variable name?

What happens if you have inconsistent indentation in Python?