python unit 1
python unit 1
1. Reference Counting: Every object has a reference count that tracks how many references point to it. When the reference
count reaches zero, the memory is deallocated.
2. Garbage Collection: Python has an automatic garbage collector to remove unused objects and free memory.
3. Heap Memory Management: All Python objects and data structures are stored in a private heap.
4. Memory Pools: Python uses memory pools like the PyMalloc allocator for better performance.
5. Dynamic Typing: Variables can hold different data types, and memory is allocated accordingly.
python
CopyEdit
def print_even_length_words(sentence):
words = sentence.split()
for word in words:
if len(word) % 2 == 0:
print(word)
# Example usage
sentence = "Hello world this is Python"
print_even_length_words(sentence)
2. Define operator associativity with its type. What are the factors for expression evaluation?
Operator Associativity
Operator associativity determines the direction in which an expression is evaluated when multiple operators of the same precedence
appear. Types:
Dynamic Typing
python
CopyEdit
x = 5 # x is an integer
x = "hello" # Now x is a string
Strongly Typed
python
CopyEdit
print("Age: " + 20) # TypeError: cannot concatenate 'str' and 'int'
python
CopyEdit
print("Age: " + str(20)) # Correct
5. Can we make multiline comments in Python? Do we need to declare variables with data types?
Multiline Comments
Python does not support explicit multiline comments, but we can use:
python
CopyEdit
"""
This is a
multiline comment
"""
6. What is Python? How is it interpreted? What tools help find bugs? What are Python decorators?
What is Python?
Python is a high-level, interpreted, dynamically typed, and general-purpose programming language. It is widely used in various
fields such as:
Python is popular due to its simplicity, readability, and vast library support, making it beginner-friendly and powerful for
professional use.
Python is an interpreted language, meaning that it executes code line by line rather than compiling the entire program before
running. The interpretation process follows these steps:
Since Python is interpreted, it enables quick prototyping and easy debugging, but may run slower compared to compiled languages
like C or Java.
These tools help developers write cleaner and error-free Python code.
A decorator in Python is a function that modifies the behavior of another function without changing its actual code. It is used to
dynamically add functionality to functions or methods.
def my_decorator(func):
def wrapper():
print("Before function execution")
func()
print("After function execution")
return wrapper
say_hello()
Output:
Decorators are commonly used in logging, authentication, caching, and modifying function behavior dynamically.
In Python, a semicolon (;) at the end of a statement does not cause an error, but it is not required and generally discouraged as
Python uses newlines to separate statements.
Example:
print("Hello, World!");
Output:
Hello, World!
The semicolon has no effect here. However, Python allows multiple statements on a single line if separated by semicolons:
x = 5; y = 10; print(x + y)
Output:
15
Even though this is valid, it reduces readability, which is why using semicolons in Python is not recommended.
Example:
x = 10
y = 5
print(x > y) # True
print(x == y) # False
1. Writing the Code – Create a .py file using any text editor or IDE.
2. Saving the File – Save the script with a .py extension.
3. Running the Code – Execute the script using the Python interpreter (python filename.py).
4. Debugging – Identify and fix errors using debugging tools.
5. Testing & Execution – Test different inputs and validate outputs.
2. Elements of Python
• Variables & Data Types – int, float, string, list, tuple, dict, set, etc.
• Operators – Arithmetic, logical, relational, bitwise, etc.
• Control Structures – if-else, for, while, try-except, etc.
• Functions & Modules – Built-in and user-defined functions, importing modules.
• Classes & Objects – Supports object-oriented programming (OOP).
Python allows conversion of data from one type to another using explicit and implicit conversion.
Operator precedence determines the order in which operations are performed in an expression.
Example:
result = 10 + 2 * 3 ** 2
print(result) # Output: 28
5. Boolean Expression
A Boolean expression is an expression that returns either True or False based on logical conditions.
Example:
x = 10
y = 20
print(x < y) # True
print(x == y) # False
Boolean expressions are used in if-else statements, loops, and logical operations.
10.Write a simple program in Python to convert a decimal number into binary, octal, and hexadecimal number system.
Python Program:
Example Output:
This program uses bin(), oct(), and hex() functions to convert a decimal number into its respective number systems.
11.When evaluating a complicated expression, what is the role of associativity? Why is it important?
Types of Associativity:
Explanation:
(10 - 4) + 2 → 6 + 2 → 8
Explanation:
2 ** (3 ** 2) → 2 ** 9 → 512
Thus, associativity plays a crucial role in ensuring that expressions are evaluated correctly and consistently.
Python Program:
def is_prime(n):
if n < 2:
return False # Numbers less than 2 are not prime
for i in range(2, int(n**0.5) + 1): # Check divisibility up to sqrt(n)
if n % i == 0:
return False
return True
Example Output:
Enter a number: 17
17 is a Prime Number
Enter a number: 20
20 is Not a Prime Number
Explanation:
This approach optimizes the checking process using the square root method, making it more efficient.
13.How would you convert a long integer to a regular int in Python if needed? What precautions should be taken when doing
this conversion?
In Python versions 2.x, there was a distinction between int (fixed-size integer) and long (arbitrarily large integer). To convert a
long integer to a regular int, you could use:
However, in Python 3.x, the long type was merged with int, meaning all integers are now of unlimited precision. So, explicit
conversion is not required, as Python automatically handles large integers.
If you need to convert a large number to a fixed-size integer (for example, when interacting with external systems like databases or
programming languages with fixed integer sizes), keep in mind:
1. Loss of Precision
o Some languages (like C or Java) have fixed integer limits (e.g., int32 or int64).
o Converting a large Python integer to a smaller integer type may result in overflow or truncation.
2. Memory Considerations
o Python’s int dynamically allocates memory.
o Converting to a fixed-size integer may reduce memory usage, but may also lead to data loss if the number is too
large.
3. Use sys.maxsize for Limits
o Before conversion, check the maximum supported integer size in your system:
4. import sys
5. print(sys.maxsize) # Typically 2^63 - 1 on 64-bit systems
import sys
Conclusion
• In Python 3.x, there is no need to convert long to int, as int supports arbitrary precision.
• When converting large numbers for use in external systems (C, Java, databases), be mindful of integer limits to avoid
overflow or data loss.
14.Concept of "Automatic Conversion" in Python When Dealing with Arithmetic Operations Involving int and long Values
Automatic conversion, also known as implicit type conversion, is when Python automatically converts one data type into another
without explicit intervention by the programmer.
When performing arithmetic operations that exceeded the storage limit of int, Python automatically converted the result to long
to prevent overflow.
In Python 3.x, the long type was removed, and all integers are now of unlimited precision. This means that Python no longer
needs to automatically convert between int and long, as int can handle arbitrarily large numbers.
Here, Python dynamically allocates memory to accommodate large integers, so there is no risk of overflow.
Key Takeaways:
1. In Python 2.x, when an int exceeded its limit, Python automatically converted it to long.
2. In Python 3.x, int has unlimited precision, so no explicit conversion is needed between int and long.
3. Python dynamically manages memory to handle large integers efficiently.
This behavior makes Python more robust for handling large numbers compared to languages like C or Java, which impose strict
integer size limits.
15.What is the difference between mutable and immutable data types in Python? Provide examples of each and discuss why
immutability is important in certain contexts.
In Python, data types are classified as mutable or immutable based on whether their values can be modified after creation.
1. Mutable Data Types
Mutable objects can be changed after creation. Any modification happens in place, meaning the object's memory address remains
the same.
1. Lists (list)
2. my_list = [1, 2, 3]
3. print(id(my_list)) # Memory address before modification
4.
5. my_list.append(4) # Modifying the list
6. print(my_list) # Output: [1, 2, 3, 4]
7.
8. print(id(my_list)) # Same memory address (modified in place)
9. Dictionaries (dict)
10. my_dict = {"a": 1, "b": 2}
11. my_dict["c"] = 3 # Adding a new key-value pair
12. print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3}
13. Sets (set)
14. my_set = {1, 2, 3}
15. my_set.add(4) # Modifying the set
16. print(my_set) # Output: {1, 2, 3, 4}
Immutable objects cannot be changed after creation. Any modification creates a new object in memory, rather than modifying the
existing one.
1. Integers (int)
2. x = 10
3. print(id(x)) # Memory address before modification
4.
5. x = x + 5 # Creates a new object
6. print(x) # Output: 15
7.
8. print(id(x)) # New memory address (new object created)
9. Strings (str)
10. s = "Hello"
11. print(id(s)) # Memory address before modification
12.
13. s = s + " World" # Creates a new string
14. print(s) # Output: Hello World
15.
16. print(id(s)) # New memory address (new object created)
17. Tuples (tuple)
18. my_tuple = (1, 2, 3)
19. # my_tuple[0] = 10 # TypeError: 'tuple' object does not support item
assignment
20. Booleans (bool)
21. x = True
22. x = False # A new Boolean object is created
23. Floating Points (float)
24. y = 3.14
25. y = y + 1.0 # A new float object is created
Summary Table
Conclusion
• Use mutable types when you need modifiable data structures (e.g., lists, dictionaries).
• Use immutable types for safety, memory efficiency, and reliability (e.g., tuples, strings).
16.What are the advantages and disadvantages of using dynamic typing in Python? How does it compare to static typing in
other programming languages?
Python uses dynamic typing, meaning variables do not have fixed types. Instead, the type is determined at runtime, and the same
variable can hold different data types at different points in the program.
x = 10 # x is an integer
x = "Hello" # x is now a string
x = 3.14 # x is now a float
print(x)
Comparison: Dynamic Typing (Python) vs. Static Typing (C, Java, etc.)
Feature Dynamic Typing (Python, JavaScript) Static Typing (C, Java, C++)
Type Declaration No need to declare types explicitly Must declare variable types (int, float, string)
Flexibility Highly flexible, variables can change types Strict, variable type cannot change
Performance Slower due to runtime type checking Faster due to compile-time type checking
Error Detection Errors occur at runtime Errors detected at compile time
Debugging Harder to debug large codebases Easier debugging due to early error detection
Best For Scripting, prototyping, rapid development Large-scale applications, high-performance computing
Conclusion
• Dynamic Typing makes Python easier to write and flexible, but it increases the risk of runtime errors and performance
issues.
• Static Typing (used in C, Java, etc.) helps with error prevention, optimization, and performance, but requires more code.
Python partially solves this with type hints (typing module) in Python 3.6+, allowing optional type checking:
17.Explain the concept of data type coercion in Python. How does Python handle operations between different data types, and
what rules govern these conversions?
Data type coercion in Python is the automatic conversion of one data type into another when performing operations involving
different types. Python implicitly converts the data type to avoid errors and ensure smooth execution of expressions.
• Python automatically converts smaller data types to larger ones to prevent data loss.
x = 10 # int
y = 2.5 # float
result = x + y # int is converted to float automatically
print(result) # Output: 12.5
print(type(result)) # Output: <class 'float'>
Here, x (integer) is implicitly converted to a float to match y, ensuring a consistent data type.
• When implicit conversion is not possible, Python allows explicit conversion using functions like int(), float(),
str(), etc.
num_str = "100"
num_int = int(num_str) # Explicit conversion
print(num_int + 50) # Output: 150
pi = 3.14
integer_value = int(pi) # Converts to 3 (decimal part is lost)
print(integer_value) # Output: 3
Key Takeaways
Implicit conversion (automatic coercion) happens when Python safely converts smaller types to larger types (e.g., int →
float).
Explicit conversion (type casting) is needed when converting between incompatible types (e.g., str → int).
Data loss can occur when converting from higher to lower precision (e.g., float → int).
Boolean values (True = 1, False = 0) can be used in numeric operations.
18.Explain the role of the is and == operators in Python when comparing objects. How do they differ, and when should you
use one over the other?
Both is and == are used for comparison, but they serve different purposes:
• Checks if two objects have the same value, even if they are stored at different memory locations.
Example:
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True, because their values are the same
print(a is b) # False, because they are different objects in memory
Here, a and b are two separate list objects, but they contain the same values, so a == b is True.
• Checks if two variables point to the same memory location (i.e., they are the same object).
Example:
a = [1, 2, 3]
b = [1, 2, 3] # A new list with the same values
Even though a and b have the same values, they are stored at different memory locations, so a is b is False.
For small integers (-5 to 256) and short strings, Python caches objects to optimize memory usage. This means is might return
True for numbers and strings with the same value.
a = 100
b = 100
print(a is b) # True (Python caches small integers)
x = "hello"
y = "hello"
print(x is y) # True (Python caches short strings)
However, for large numbers and dynamically created strings, is may return False:
a = 5000
b = 5000
print(a is b) # False (Different objects in memory)
x = "hello world!"
y = "hello world!".replace("!", "")
print(x is y) # False (Different memory locations)
• Checking if two variables refer to the same object in memory (useful for performance optimizations).
Conclusion
19.Explain the difference between local and global variables in Python. When and why might you choose to use one over the
other?
def example_function():
local_var = 10 # Local variable
print("Inside function:", local_var)
example_function()
# print(local_var) # Error: local_var is not defined outside the function
Output:
Inside function: 10
Here, local_var is defined inside the function and cannot be accessed outside.
def example_function():
print("Inside function:", global_var)
example_function()
print("Outside function:", global_var) # Accessible globally
Output:
Inside function: 20
Outside function: 20
Here, global_var is defined outside the function and accessible both inside and outside.
By default, modifying a global variable inside a function creates a new local variable. To modify a global variable, use the global
keyword.
x = 5 # Global variable
def change_x():
x = 10 # This creates a new local variable, doesn't modify global x
print("Inside function:", x)
change_x()
print("Outside function:", x) # Original global variable remains unchanged
Output:
Inside function: 10
Outside function: 5
Here, Python creates a local variable x inside change_x(), leaving the global x unchanged.
x = 5 # Global variable
def change_x():
global x # Declaring x as global
x = 10 # Now modifies the global x
print("Inside function:", x)
change_x()
print("Outside function:", x) # Now x is modified globally
Output:
Inside function: 10
Outside function: 10
By using global x, we modified the global variable instead of creating a new local one.
5. Best Practices
20.Explain the concept of reserved words (keywords) in Python. Why are they important, and how should you avoid naming
conflicts with keywords?
Example of Keywords:
False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except,
finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise,
return, try, while, with, yield
• Keywords are essential for writing control flow (if, else, while, for), functions (def, return), and exception
handling (try, except).
import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global',
'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise',
'return', 'try', 'while', 'with', 'yield']
my_def = 10 # No conflict
5. Summary
Using keywords correctly ensures clean, readable, and error-free Python code. Let me know if you need more examples!