Punctuators/ Comments/Data Types

M3-R5.1 · Chapter 3: Introduction to Python · 6 min read

Punctuators

In Python, punctuators are symbols that help define the structure and syntax of the language. They are not used as operators, but rather to group, organize, or separate code. Here's a list of common punctuators in Python and their purposes:

  • : Colon: Used in defining functions, loops, classes, and conditional statements.
  • , Comma: Used to separate elements in lists, tuples, dictionaries, and function arguments.
  • . Period (Dot): Access attributes or methods of an object or module (e.g., object.method()).
  • () Parentheses: Used for function calls, grouping expressions, and creating tuples.
  • [] Square brackets: Used for list indexing, slicing, and comprehensions.
  • {} Curly braces: Used to define dictionaries and sets.
  • = Assignment operator: Assign values to variables.
  • -> Arrow: Used to indicate return types of functions (e.g., def func() -> int:).
  • ; Semicolon: Allows multiple statements on a single line, though its usage is rare in Python.

Python's punctuators help maintain its clean and readable syntax.

Expressions

  • It is any legal combination of symbols that represents a value.
  • An expression represents something, which python evaluates and which then produces a value
  • Example: 15, a+10, a+3, b>5

Statements

  • A statement is a programming instruction that does something i.e some action takes place 
  • Example: print (“hello Python”)

Comments in Python

  • A comment is a line of text in Python code that is ignored by the interpreter.
  •  It is used to explain code, document functionality, and increase readability.
  •  Comments do not affect the execution of the program.
  • Comments are the additional readable information, which is read by the programmers but ignored by Python interpreter
  • Add a # symbol in the beginning of every physical line part of the multiline comments

Types of Comments in Python :    Python supports two types of comments:

  1. Single-line comments
  2. Multi-line comments (or block comments)

1. Single-Line Comments

  • Single-line comments start with a # symbol.
  • The interpreter ignores everything after # on that line.

Example:    print("Hello, World!")  # This prints "Hello, World!"

2. Multi-Line Comments (Block Comments)

  • Python does not have built-in multi-line comment syntax like /* */ in C/C++.
  • However, you can use:
  • Multiple # symbols (preferred method) so Add a # symbol in the beginning of every physical line part of the multiline comments

Note: Comments enclosed in triple quotes (""") or triple apostrophe ('") are called docstrings.

A. Using Multiple # Symbols

#Multi-line comments are useful for detailed additional information.

# related to the program in question.

# It helps clarify certain important things.

B. Using Triple Quotes (""" """ or ''' ''')

  • Triple quotes are mainly used for docstrings.
  •  However, they can also be used for multi-line comments.

 “ “ “ Multi-line comments are useful for detailed additional

                                Information related to the program in question.

                                It helps clarify certain important things

“ “ “

NOTE :

    • Triple quotes (""" """ or ''' ''') are not ignored by the interpreter but stored as docstrings if they appear at the beginning of a function or class.
    • Comments enclosed in triple quotes (""") or triple apostrophe ('") are called docstrings

Python’s Built-in Data types : 

Python provides several built-in data types to store and manipulate different kinds of data efficiently. These data types can be categorized broadly into the following types:

1. Numeric Types

  • int: Represents integers (whole numbers). Example: x = 10
  • float: Represents floating-point numbers (decimal values). Example: y = 3.14
  • complex: Represents complex numbers with real and imaginary parts. Example: z = 2 + 3j

2. Sequence Types

  • str: A sequence of characters (text). Example: text = "Hello, Python"
  • list: An ordered, mutable sequence of items. Example: fruits = ["apple", "banana", "cherry"]
  • tuple: An ordered, immutable sequence of items. Example: coordinates = (10, 20, 30)
  • range: Represents a sequence of numbers. Example: nums = range(5) (produces numbers 0 to 4)

3. Set Types

  • set: An unordered collection of unique elements. Example: unique_items = {1, 2, 3}
  • frozenset: An immutable version of a set. Example: frozen = frozenset([1, 2, 3])

4. Mapping Type

  • dict: Represents key-value pairs. Example: person = {"name": "Naitik", "age": 25}

5. Boolean Type

  • bool: Represents logical values: True or False. Example: is_valid = True

6. Binary Types

  • bytes: Immutable sequence of bytes. Example: b = b"Hello"
  • bytearray: Mutable sequence of bytes. Example: ba = bytearray([65, 66, 67])
  • memoryview: A memory view object. Example: mv = memoryview(b"Hello")

7. None Type

  • NoneType: Represents the absence of a value. Example: value = None