Punctuation in Python Explained

Posted by Afsal on 13-Dec-2024

Python’s syntax is simple and elegant, yet every punctuation mark plays a critical role. Understanding these symbols can transform your Python skills, from writing clean code to mastering advanced techniques. Let’s dive into each punctuation mark, its purpose, and examples!

Parentheses ()

  • Group expressions.
  • Call functions.
  • Create tuples.

code

# Function call
print("Hello, World!")  

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

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

Advanced Use:

Parentheses can make generator expressions concise when used in functions:

code

>>> generator = (x**2 for x in range(10))
>>> generator
<generator object <genexpr> at 0x7ce6ce4a1460>
>>> 

Brackets []

  • Define lists.
  • Access list elements using indexing or slicing.

code

# List creation
my_list = [1, 2, 3]  

# Indexing
first_item = my_list[0]  

# Slicing
sub_list = my_list[1:3]  

Advanced Use:

List comprehensions:

code

squares = [x**2 for x in range(5)]

Braces {}

  • Define dictionaries and sets.

code

# Dictionary
my_dict = {"name": "Python", "age": 30}  

# Set
my_set = {1, 2, 3}

Advanced Use:

Dynamic expression evaluation in f-strings:

code

>>> name = "Python"
>>> print(f"Hello, {name.upper()}!")
Hello, PYTHON!
>>> 

Colon :

  • Define blocks (e.g., functions, loops, conditionals).
  • Slicing.

code

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

# Slicing
my_slice = my_list[1:3]

Advanced Use:

Annotations for type hints:

code

def add(x: int, y: int) -> int:
    return x + y

Semicolon ;

  • Separate multiple statements on a single line.

code

a = 1; b = 2; c = 3

Comma ,

  • Separate elements in tuples, lists, and function arguments.

code

# Tuple
my_tuple = (1, 2, 3)  

# Function arguments
print(1, 2, 3)  

Trailing commas can lead to generation of tuple

>>> a = 1,
>>> a
(1,)
>>> type(a)
<class 'tuple'>
>>> 

Period .

  • Access attributes or methods of objects.

code

# Method call

my_string = "Hello"
print(my_string.upper())

Ellipsis ...

  • Placeholder for unimplemented code.

 code

def future_function():
    ...

Note: This is equivalent to pass keyword

At @

  • Used in decorators.

code

@staticmethod
def my_method():
    print("Hello!")  

Advanced use

@ operator is used for matrix operations

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
result = A @ B
print(result)

Hash #

  • Add comments to your code.

code

# This is a comment

Backslash \

  • Escape special characters or continue code on a new line.

code

# Line continuation
long_string = "This is a long string " \
              "that spans multiple lines."


# Escaping
print("He said, \"Hello!\"")  

Underscore _

  • Used in variable names or as a throwaway variable.

code

_, value = (1, 2)
print(value)  # Output: 2

var_ = 1

Asterisk * and **

  • Perform multiplication.
  • Unpack arguments.

code

# Multiplication
result = 2 * 3  


# Unpacking
a, *b = [1, 2, 3, 4]

I hope every puctuation is covered please share your suggestions with afsal@parseltongue.co.in