Python
About Lesson

Punctuators in Python are symbols that have special meanings in the language. They are used to structure code, separate statements, and group expressions. Here’s a simple tutorial on the most common punctuators in Python along with examples.

 

1. Parentheses ( )

Used for grouping expressions and function calls.

# Grouping expressions
result = (2 + 3) * 4 # 5 * 4 = 20

# Function calls
print("Hello, World!") # prints Hello, World!

 

2. Brackets [ ]

Used to define lists and access elements.

# Defining a list
my_list = [1, 2, 3, 4, 5]

# Accessing elements
print(my_list[2]) # prints 3

 

3. Braces { }

Used to define dictionaries and sets.

# Defining a dictionary
my_dict = {"name": "Alice", "age": 25}

# Accessing dictionary values
print(my_dict["name"]) # prints Alice

# Defining a set
my_set = {1, 2, 3, 4, 5}

 

4. Colon :

Used in function definitions, loops, conditional statements, and dictionary key-value separation.

# Function definition
def greet(name):
print(f"Hello, {name}!")

# Conditional statement
age = 20
if age >= 18:
print("You are an adult.")

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

# Dictionary key-value separation
my_dict = {"name": "Alice", "age": 25}

 

5. Comma ,

Used to separate items in lists, tuples, function arguments, and dictionary items.

# List items
my_list = [1, 2, 3, 4, 5]

# Tuple items
my_tuple = (1, 2, 3)

# Function arguments
def add(a, b):
return a + b

# Dictionary items
my_dict = {"name": "Alice", "age": 25}

 

6. Dot .

Used to access attributes and methods of objects.

# Accessing methods
my_str = "hello"
print(my_str.upper()) # prints HELLO

 

7. Assignment =

Used to assign values to variables.

# Assignment
x = 10
y = 20
z = x + y # z is now 30
 

8. Equality == and Inequality !=

Used to compare values.

# Equality
x = 10
print(x == 10) # prints True

# Inequality
y = 20
print(x != y) # prints True

 

9. Plus + and Minus -

Used for addition and subtraction.

# Addition
a = 5 + 3 # a is 8

# Subtraction
b = 10 - 2 # b is 8

 

10. Asterisk * and Slash /

Used for multiplication and division.

# Multiplication
c = 4 * 2 # c is 8

# Division
d = 20 / 4 # d is 5.0

 

11. Indentation

Python uses indentation to define blocks of code.

# Function definition
def say_hello():
print("Hello!")

# Conditional statement
if True:
print("This is true.")

 

Understanding and correctly using punctuators is essential for writing clear and correct Python code. This tutorial covered some of the most commonly used punctuators with examples to illustrate their usage.