Pyhton Unit - 1
Pyhton Unit - 1
Explain with
examples. Definition:
Python is an object-oriented programming language, meaning everything in Python is treated as an
object. An object is an instance of a class, containing attributes (data) and methods (functions).
Objects allow structured programming by encapsulating related data and behavior in one place.
In Python, even basic types like int, float, list, and dict are objects. Python’s dynamic typing means
that when a variable is assigned a value, it automatically becomes an object of a corresponding type.
Objects make Python flexible, reusable, and easy to manage.
Use:
Objects are useful because they allow:
Reusability – Once a class is created, multiple objects can be made from it.
Example:
python
CopyEdit
class Car:
self.brand = brand
self.model = model
def show(self):
print(f"{self.brand} {self.model}")
c = Car("Toyota", "Camry")
Theory:
In the example, Car is a class, and c is an object. The init method initializes attributes, and
show() is a method that prints the values. Python automatically manages object creation and
deletion using garbage collection. Objects simplify program structure, making code more
readable and maintainable.
2. Explain Python’s standard data types with
examples. Definition:
Python provides standard data types to store and manipulate data. These types determine how
values are stored, accessed, and modified.
Python’s dynamic typing means that variables do not need explicit declaration; they take the type of
the value assigned.
Use:
Example:
python
CopyEdit
x = 10 # int
y = 3.14 # float
s = "Hello" # str
l = [1, 2, 3] # list
Theory:
Standard types make Python versatile and powerful. For example, list is mutable (can be changed),
whereas tuple is immutable (fixed). dict allows fast lookups using keys. set avoids duplicates,
making it useful in scenarios like removing redundant data. Understanding data types helps write
efficient and optimized Python programs.
3. What are Python’s built-in types? Explain with
examples. Definition:
Python provides several built-in types to handle different kinds of data efficiently. These types allow
developers to store, manipulate, and process information effectively. Python categorizes built-in
types into numeric types, sequence types, mapping types, set types, boolean types, and binary
types.
Python is dynamically typed, meaning variables do not need explicit type declarations; they take the
type of the assigned value.
Use:
Example:
python
CopyEdit
a = 10 # int
b = 3.14 # float
c = "Python" # str
Theory:
Python’s built-in types improve efficiency and flexibility. list and tuple store collections, with lists
being mutable and tuples immutable. dict allows quick key-based lookups. set removes duplicates
and supports operations like union and intersection. Understanding these types is essential for
writing optimized and readable Python programs.
examples. Definition:
Operators are symbols used to perform operations on values and variables. Python provides
standard type operators for different data types, allowing manipulation of numbers, sequences,
and logical expressions.
Categories of Operators:
2. Comparison Operators (==, !=, >, <, >=, <=) – Compare values and return True or False.
5. Membership Operators (in, not in) – Check for the presence of values in sequences.
Use:
Example:
python
CopyEdit
a=5
b = 10
print(a + b) # Arithmetic: 15
Theory:
Operators make Python powerful and expressive. Arithmetic and logical operations are
fundamental in computations and control flow. Understanding them helps in building efficient
programs with minimal code.
5. Explain built-in functions for standard types in
Python. Definition:
Built-in functions are predefined functions that perform common operations on standard data
types. They simplify tasks, eliminating the need for additional code.
Use:
Example:
python
CopyEdit
numbers = [4, 2, 8, 1]
print(len(numbers)) # 4
print(max(numbers)) # 8
print(sorted(numbers)) # [1, 2, 4, 8]
Theory:
Built-in functions enhance productivity by handling common operations efficiently. They help in
mathematical calculations, string manipulations, and data structure operations. Understanding
them improves coding speed and efficiency.
examples. Definition:
Python categorizes standard types based on how they store and manipulate data. These categories
help in choosing the right type for a particular operation. The main categories are:
1. Numeric Types: int, float, complex – Store numerical values.
Use:
Sequence types help in storing and processing text, lists, and tuples.
Sets remove duplicate values and allow set operations like union and intersection.
Example:
python
CopyEdit
num = 42 # int
Theory:
Understanding type categorization helps in optimizing memory and performance. Choosing the right
type ensures faster computations and efficient data handling. Python’s dynamic typing allows
changing types at runtime, making programming flexible.
exist? Definition:
Unsupported types refer to data types that are not directly available in Python. These are types
found in lower-level languages like C or Java but are absent in Python due to its high-level
nature. Examples include:
1. Pointers – Used in C for direct memory access, but Python manages memory automatically.
Use:
dynamic. Example:
python CopyEdit
'A'
Theory:
Python focuses on simplicity and readability, removing unnecessary low-level types. Unsupported types
are either replaced with more flexible alternatives or handled automatically by Python’s
interpreter.
examples. Definition:
Python supports three numeric types:
Use:
Example: python
CopyEdit
x = 10 # int
y = 3.14 # float
z = 2 + 3j # complex
Theory:
Python automatically assigns the correct type based on value. Integers can be arbitrarily large
without overflow. Floating-point numbers follow IEEE 754 standards. Complex numbers help in
engineering and physics. Understanding numeric types helps in choosing the right format for
calculations.
Tuples. Definition:
Sequence types store ordered collections of elements. The three main sequence types in Python
are:
Use:
Example:
python
CopyEdit
s = "Hello" # str
l = [1, 2, 3] # list
t = (1, 2, 3) # tuple
Theory:
Lists are dynamic, allowing modifications like addition and removal of elements.
Choosing the right sequence type ensures better performance and memory management.
Use:
operations. Example:
python
CopyEdit
# Dictionary
# Set
s = {1, 2, 3, 4}
Theory:
Dictionaries are efficient for searching, while sets help in data validation and filtering.
Understanding these structures improves data management and program efficiency.
Buffer Question 1: Explain the significance of Python’s built-in types and their operations.
Definition:
Python has several built-in types that store and manipulate data efficiently. These include numeric
types (int, float, complex), sequence types (str, list, tuple), mapping types (dict), set types (set,
frozenset), boolean type (bool), and binary types (bytes, bytearray, memoryview).
3. Rich Operations – Python provides operators and built-in functions to work with these
types easily.
Example:
python CopyEdit
x = [1, 2, 3] # List
print(x) # [1, 2, 3, 4]
Theory:
Understanding Python’s built-in types is essential for efficient coding. The ability to perform operations
directly on these types simplifies programming, reduces development time, and makes Python code
more readable and maintainable.
Buffer Question 2: Explain Python’s number system and the operations available for numbers.
Definition:
Python supports three numeric types:
Mathematical Operations:
Example: python
CopyEdit
x = 10 # Integer
y = 3.5 # Float
z = 2 + 3j # Complex
print(x + y) # 13.5
Theory:
Numbers are fundamental in programming, used for calculations, data representation, and scientific
computing. Python provides built-in functions like abs(), pow(), round(), and math module
functions to work with numbers efficiently. Understanding Python’s number system allows better
handling of calculations and data representation.