0% found this document useful (0 votes)
2 views3 pages

Types in Python

The document outlines various data types in programming, including numeric types (int, float, complex), boolean (bool), text (str), sequence types (list, tuple, set), and mapping types (dict). It distinguishes between mutable types, which can be modified after creation (e.g., list, dict, set), and immutable types, which cannot be modified (e.g., int, str, tuple, float). Understanding these distinctions aids in error prevention and efficiency in memory usage.

Uploaded by

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

Types in Python

The document outlines various data types in programming, including numeric types (int, float, complex), boolean (bool), text (str), sequence types (list, tuple, set), and mapping types (dict). It distinguishes between mutable types, which can be modified after creation (e.g., list, dict, set), and immutable types, which cannot be modified (e.g., int, str, tuple, float). Understanding these distinctions aids in error prevention and efficiency in memory usage.

Uploaded by

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

1.

Numeric Types:
 int: Represents whole numbers.
Example: x = 5
 float: Represents decimal numbers.
Example: y = 3.14
 complex: Represents complex numbers with real and imaginary
parts.
Example: z = 2 + 3j
2. Boolean Type:
 bool: Represents logical values, True or False.
Example: is_valid = True
3. Text Type:
 str: Represents text data.
Example: greeting = "Hello"
4. Sequence Types:
 list: A mutable sequence.
Example: fruits = ["Ramesh", "Mahesh", "Suresh"]
 tuple: An immutable sequence.
Example: dimensions = (1920, 1080)
 set: An unordered collection of unique items.
Example: nums = {1, 2, 3, 4}
5. Mapping Type:
 dict: A collection of key-value pairs.
Example: person = {"name": "Ramesh", "age": 25}
1. Mutable Types

• Can be modified after creation.

• Examples:
 list
 dict
 set
2. Immutable Types

• Cannot be modified after creation. Any operation that modifies an


immutable object creates a new object.

• Examples:
 int: x = 5
x=x+1 (creates a new integer object)
 float: pi = 3.14
 tuple: t = (1, 2, 3)
 str: name = "Ramesh"
name += "Das" (creates a new string object)
Aspect Mutable Immutable
Modification Can be changed in place Cannot be changed in place
Examples list, dict, set int, str, tuple, float
Performance Slower for certain operations Faster for read operations

Understanding the distinction helps in:


• Error prevention: Immutable types prevent unintended side-effects.
• Efficiency: Mutable types can save memory during updates.

You might also like