0% found this document useful (0 votes)
4 views7 pages

Module-1 (Chapter 2)

This document covers the basics of writing clean and readable code in Python, focusing on comments, docstrings, and data types. It explains how to use single-line and multi-line comments, the purpose of docstrings, and the primary data types in Python, including numeric, compound, boolean, dictionary, set, and mapping types. Best practices for writing comments and docstrings are also discussed to ensure maintainable and understandable code.

Uploaded by

ashwinidumbre789
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Module-1 (Chapter 2)

This document covers the basics of writing clean and readable code in Python, focusing on comments, docstrings, and data types. It explains how to use single-line and multi-line comments, the purpose of docstrings, and the primary data types in Python, including numeric, compound, boolean, dictionary, set, and mapping types. Best practices for writing comments and docstrings are also discussed to ensure maintainable and understandable code.

Uploaded by

ashwinidumbre789
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MODULE-1

CHAPTER-2: DATA TYPES, VARIABLES AND OTHER BASIC ELEMENTS

❖ WRITING CLEAN AND READABLE CODE IN PYTHON

COMMENTS:

1. Single-Line Comments:

In Python, you can write a single-line comment by starting the line with the
# symbol.

# This is a single-line comment explaining the next line of code


print("Hello, world!")

Everything after the # on that line is ignored by Python. Use single-line comments to make brief
notes about what your code is doing, why certain decisions were made, or to highlight potential
areas for future improvement.

2. Multi-Line Comments:

While Python does not have a specific syntax for multi-line comments like some other
programming languages, you can use a series of single-line comments.

#This is a multi-line comment

# explaining that the next few lines

# of code initialize the main application

initialize_app()

Alternatively, you can use a trick with multi-line strings, which is not an official comment but can
be used to achieve a similar effect.

“””

This is a multi-line string,

but it can also be used as a multi-line comment

to explain complex logic in your code.


“””
DOCSTRINGS: THE OFFICIAL DOCUMENTATION FOR YOUR CODE

While comments are for your internal notes, docstrings (documentation strings) serve as the
official documentation for your code. They describe the purpose and usage of modules, classes,
methods, and functions. Docstrings are placed within triple quotes """ or " and are positioned as
the first statement within a module, class, or function.

MODULE DOCSTRINGS:

At the beginning of a Python module, you can include a docstring to describe the module's
purpose and usage.
“””
This module provides utility functions for data processing.

It includes functions for data cleaning, transformation, and visualization.\


“””

#Module code follows

FUNCTION AND METHOD DOCSTRINGS:

For functions and methods, docstrings should explain what the function does, its parameters,
and its return value.

def add(a, b):


“””

Add two numbers and return the result.

Parameters:

a (int or float): The first number to add.

b (int or float): The second number to add.

Returns:

int or float: The sum of the two numbers.

return a + b
CLASS DOCSTRINGS:
For classes, docstrings provide an overview of the class's purpose and can describe its
attributes and methods.

class MyClass:
"""
A simple example class.

Attributes:

attribute1 (type): Description of attribute1.

attribute2 (type): Description of attribute2.

Methods:

method1(): Description of method1.

method2(param): Description of method2 with parameters.

"""

def _init(self, attribute1, attribute2):

self.attribute1 = attribute1

self.attribute2 = attribute2

def method1(self):
"""
Perform an action for method1.
"""
pass
def method2(self, param):
"""
Perform an action for method2 with a parameter.
Parameters:
param (type): Description of the parameter.
"""
pass

In Python, pass is a placeholder statement that does nothing. It's used when a statement is
syntactically required but you don't want any code to run yet—for example, in empty functions,
classes, or loops during development.
❖ BEST PRACTICES FOR WRITING COMMENTS AND DOCSTRINGS

1. Be Clear and Concise:

Write comments and docstrings that are easy to understand. Avoid redundant or over
explanations.

2. Update Comments Regularly:

Keep your comments and docstrings up-to-date as your code evolves. Outdated comments can
be misleading.

3. Use Comments to Explain Why, Not What:

Focus on explaining the reasoning behind your code rather than what the code is doing. The
code itself should be clear about what it does.

4. Follow Conventions:

Adhere to established conventions for writing docstrings, such as those outlined in PEP 257
(Python Docstring Conventions).

By incorporating comments and docstrings effectively, you ensure that your code is not only
functional but also maintainable and understandable for others who might read or modify it in
the future.

❖ UNDERSTANDING DATA TYPES IN PYTHON

In Python, data types are the classification of data items. They determine the operations that
can be performed on the data and the structure in which the data will be stored. Let us dive into
the primary data types in Python, including Numeric, Compound, Boolean, Dictionary, Sets, and
Mapping types.

1. Numeric Data Types:

Numeric data types are used to store numbers. Python supports several types of numeric data.

i) Integers (int):

Integers are whole numbers, both positive and negative, without a fractional part.

x = 42

y = -7
ii) Floating-Point Numbers (float):

Floats represent real numbers with a fractional part, indicated by a decimal point.
pi = 3.14159
g = 9.81

iii) Complex Numbers (complex) :

Complex numbers have a real part and an imaginary part, represented as a bj, where a is the
real part and b is the imaginary part.

z=3+4)

2. Compound Data Types:

Compound data types can hold multiple values. The most commonly used compound types are
lists and tuples.

1) Lists:

Lists are ordered collections of items, which can be of different types Lists are mutable,
meaning their contents can be changed.

fruits = ["apple", "banana", "cherry"]

numbers = [1, 2, 3, 4, 5]

ii) Tuples:

Tuples are similar to lists but are immutable, meaning their contents cannot be changed once
defined.

point = (10, 20)


dimensions (1920, 1080)

3. Boolean Data Type:

The boolean data type represents one of two values: True or False. Booleans are often used in
conditional statements and logical operations.

is_sunny = True
is_raining = False
4. Dictionary Data Type:

Dictionaries are collections of key-value pairs. Each key is unique, and the values can be of any
data type. Dictionaries are mutable.

student = {"name": "Alice", "age": 23, "grades": [88, 92, 79]}

5. Set Data Type:

Sets are unordered collections of unique items. Sets are useful for membership testing and
eliminating duplicate entries.

unique_numbers = {1, 2, 3, 4, 5}

6. Mapping Data Type:

In Python, the most common mapping type is the dictionary. Mappings store objects by key,
allowing for fast retrieval. While dictionaries are the
[4:38 pm, 8/7/2025] Ashwini: primary mapping type, understanding them is crucial due to their
frequent use in Python programming.

phone_book ("John": "555-1234", "Jane": "555-5678", "Jake": "555-8765")

Numeric Data Types Example:

a=5 # Integer
b = 2.5 #Float
c = 1 + 2j # Complex

# Operations

sum_ab = a + b
product_ac = a * c

Compound Data Types Example:

# List
colors = ["red", "green", "blue"]
[Link]("yellow")

# Tuple
coordinates = (10, 20)
Boolean Data Type Example:

is_equal =( a ==b) # False


is_greater = (a > b) # True

Dictionary Data Type Example:

person = {"name": "Bob", "age": 30)


person["age" y = 31

Set Data Type Example:

animals = {"cat", "dog", "bird")


[Link]("fish")

Conclusion:

Understanding these data types is fundamental to mastering Python. Each type has its specific
use cases and advantages, enabling you to handle and manipulate data effectively. As you
progress, you will discover the versatility and power of Python's data types in solving complex
programming challenges.

You might also like