COMMENTS
Purpose:
- Comments are used to add remarks or notes in the source code.
- They are not executed by the interpreter but are included to make the source code easier for humans to understand.
- Comments document the meaning, purpose, input, and output requirements of the source code, aiding in understanding its functionality.
Usage:
- Comments are particularly important in large and complex software projects, especially when multiple programmers are involved.
- They help programmers, including those working in teams or maintaining code written by others, to understand how the program functions and how to use it effectively.
Syntax:
- In Python, comments start with the
# (hash sign) character. - Everything following the
# until the end of the line is treated as a comment by the interpreter, which ignores it during execution.
Example (5.1):
- Demonstrates the usage of comments in Python code.
- Comments are used to explain the purpose of variables
amount and totalMarks, providing context for their usage.
Example Program (5-4):
- Illustrates a Python program to find the sum of two numbers.
- Comments are added to describe the purpose of variables
num1 and num2, as well as the calculation of the result.
EVERYTHING IS AN OBJECT
Object Representation:
- In Python, every value or data item, whether numeric, string, or other types, is treated as an object.
- Objects can be assigned to variables or passed to functions as arguments.
Unique Identity:
- Each object in Python is assigned a unique identity (ID), which remains constant throughout the object’s lifetime.
- This ID serves as a reference akin to the memory address of the object.
Function id():
- The
id() function in Python returns the identity of an object, which is essentially its unique identifier.
Example (5.2):
- Demonstrates the usage of the
id() function in Python. - Variable
num1 is assigned the value 20, and id(num1) returns its identity. - Variable
num2 is assigned the expression 30 - 10, and id(num2) returns the same identity as num1, indicating that both variables refer to the same object, which is 20.
DATA TYPES
Definition:
- Every value in Python belongs to a specific data type, which identifies the type of data a variable can hold and the operations that can be performed on it.
Numeric Data Types:
- Numeric data types store numerical values and are classified into three types:
int, float, and complex. int: Represents integer numbers, such as -12, -3, 0, 125, 2.float: Represents real or floating-point numbers, such as -2.04, 4.0, 14.23.complex: Represents complex numbers, such as 3 + 4j, 2 - 2j.
Boolean Data Type (bool):
- A subtype of integer consisting of two constants:
True and False. True represents a non-zero, non-null, and non-empty value, while False represents the value zero.
Example (5.3):
- Demonstrates the usage of the
type() function in Python to determine the data type of variables. - Variables are assigned values of different data types (
int, bool, float, complex), and the type() function is used to identify their respective data types.
Complex Data Structures:
- While simple data types like integers, floats, and booleans hold single values, Python provides data types like tuples, lists, dictionaries, and sets to hold collections of data.
- These data types are useful for storing and manipulating larger sets of information, such as lists of names, phone numbers, or artifacts.
Sequence
- Sequence Data Types:
- A Python sequence is an ordered collection of items, where each item is indexed by an integer.
- There are three main types of sequence data types in Python: Strings, Lists, and Tuples.
String (A):
- A string is a group of characters, including alphabets, digits, or special characters.
- String values are enclosed in either single quotation marks (
') or double quotation marks ("). - Example
| Python |
str1 = ‘Hello Friend’ str2 = “452” |
List (B):
- A list is a sequence of items separated by commas and enclosed in square brackets (
[]). - Lists can contain a mix of data types (e.g., integers, floats, strings).
- Example
| python |
list1 = [5, 3.4, “New Delhi”, “20C”, 45] |
Tuple (C):
- A tuple is similar to a list, but its elements are enclosed in parentheses (
()). - Once created, the elements of a tuple cannot be changed (immutable).
- Example
| python |
tuple1 = (10, 20, “Apple”, 3.4, ‘a’) |
Set Data Type:
- A set is an unordered collection of items separated by commas and enclosed in curly braces (
{}). - Sets cannot have duplicate entries, and once created, the elements of a set cannot be changed.
- Example
| python |
set1 = {10, 20, 3.14, “New Delhi”} set2 = {1, 2, 1, 3} |
Understanding these data types in Python is crucial for effectively storing and manipulating collections of data in a program. Each data type has its own characteristics and use cases, providing flexibility and versatility in programming tasks.
None
None Data Type (5.7.4):
None is a special data type in Python that represents the absence of a value.- It has a single value,
None, and supports no special operations. - Example
| python |
myVar = None print(type(myVar)) print(myVar) |