Skip to content

Basic Syntax

Python is designed to be simple, readable, and expressive. Its syntax is intentionally minimal compared to many other programming languages, which makes it a popular choice for beginners and professionals alike.

The print() function is used to display output to the console.

print("Hello, World!")

Output:

Hello, World!

This is usually the first program people write when learning Python.


Variables store data. In Python, you do not need to declare the type of a variable.

name = "Alice"
age = 25
height = 5.6

Python automatically determines the variable type.


Some commonly used built-in data types include:

integer_number = 10 # int
decimal_number = 3.14 # float
text = "Python" # string
is_active = True # boolean

Python is dynamically typed, meaning variable types are determined at runtime.


Comments are used to explain code and are ignored during execution.

Single-line comment:

# This is a comment

Multi-line comment (commonly done with triple quotes):

"""
This is a
multi-line comment
"""

Unlike many languages that use braces {}, Python uses indentation to define code blocks.

if age > 18:
print("You are an adult")

Improper indentation will cause an error.


Python uses if, elif, and else for decision-making.

score = 85
if score >= 90:
print("Grade A")
elif score >= 75:
print("Grade B")
else:
print("Grade C")

Used to iterate over a sequence.

for i in range(5):
print(i)

Output:

0
1
2
3
4

Executes as long as a condition is true.

count = 0
while count < 3:
print(count)
count += 1

Functions allow code reuse.

def greet(name):
return "Hello " + name
message = greet("Alice")
print(message)

Python has many built-in modules that extend functionality.

import math
print(math.sqrt(16))

Output:

4.0

Python’s syntax emphasizes readability and simplicity. Core features like dynamic typing, indentation-based blocks, and built-in libraries allow developers to write powerful programs with relatively little code.