Python Basics

Python Syntax

Python syntax basics

Python Syntax

Python syntax is designed to be readable and straightforward. Unlike many other programming languages, Python uses indentation to define code blocks instead of curly braces.

Indentation

Python uses indentation (whitespace) to indicate blocks of code. The number of spaces is up to you, but it must be consistent throughout the block.

if 5 > 2:
    print("Five is greater than two!")

If you skip the indentation, Python will give you an error:

if 5 > 2:
print("Five is greater than two!")
# This will cause an IndentationError

Comments

Comments start with a # and Python will ignore them:

# This is a comment
print("Hello, World!")  # This is also a comment

Variables

Python has no command for declaring a variable. A variable is created the moment you first assign a value to it:

x = 5
y = "Hello, World!"
print(x)
print(y)

Python is Case Sensitive

Variable names are case-sensitive:

a = 4
A = "Sally"
# A and a are different variables