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

Intermediate Python Code Examples

This document provides example questions and code snippets for intermediate Python programming concepts, including lists, tuples, functions, modules, and inheritance. It demonstrates how to find the second largest number in a list, count occurrences in a tuple, calculate factorials using recursion, create custom modules, and implement inheritance with method overriding. Each section includes practical code examples to illustrate the concepts effectively.

Uploaded by

outlooksinet
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)
22 views3 pages

Intermediate Python Code Examples

This document provides example questions and code snippets for intermediate Python programming concepts, including lists, tuples, functions, modules, and inheritance. It demonstrates how to find the second largest number in a list, count occurrences in a tuple, calculate factorials using recursion, create custom modules, and implement inheritance with method overriding. Each section includes practical code examples to illustrate the concepts effectively.

Uploaded by

outlooksinet
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

Python Intermediate Programming

Example Questions
1. List Examples
 Q: Write a Python program to find the second largest number in a list.

Code:
nums = [10, 20, 4, 45, 99]
[Link]()
print('Second largest:', nums[-2])

 Q: Create a new list with squares of only even numbers from another list.

Code:
numbers = [1, 2, 3, 4, 5, 6]
even_squares = [x*x for x in numbers if x % 2 == 0]
print(even_squares)

2. Tuple Examples
 Q: Write a Python program to count the occurrences of an element in a tuple.

Code:
t = (1, 2, 3, 2, 2, 4)
print([Link](2))

 Q: Unpack a tuple of three elements into variables and print them.

Code:
my_tuple = (10, 20, 30)
a, b, c = my_tuple
print(a, b, c)

3. Function Examples
 Q: Write a function to calculate the factorial of a number using recursion.

Code:
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
print(factorial(5))

 Q: Write a Python function that accepts any number of arguments and prints them.

Code:
def print_args(*args):
for arg in args:
print(arg)

print_args(1, 2, 3)

4. Module Examples
 Q: Create a custom module with a function and import it.

Code:
# [Link]

def greet(name):
return f"Hello, {name}!"

# [Link]
import mymodule
print([Link]('Alice'))

 Q: Use the math module to calculate the square root of a number.

Code:
import math
print([Link](16))

5. Inheritance Examples
 Q: Create a base class Animal with a derived class Dog that adds a method bark().

Code:
class Animal:
def speak(self):
print("Animal speaks")

class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
[Link]()
[Link]()

 Q: Demonstrate use of super() in inheritance.

Code:
class Parent:
def __init__(self):
print("Parent constructor")

class Child(Parent):
def __init__(self):
super().__init__()
print("Child constructor")

c = Child()

Common questions

Powered by AI

The `math` module in Python provides a collection of mathematical functions and constants, which are implemented in C for performance. For calculations like square root, `math.sqrt(x)`, it is not only concise and easy to implement but also efficient compared to manually implementing the method. Using built-in modules such as `math` ensures optimized and accurate computations . Additionally, these functions have been tested extensively for correctness, reliability, and edge cases.

Creating a custom module allows code to be organized into reusable components, promoting separation of concerns and maintainability. Define functions or variables in a Python file (`mymodule.py`) and use them in another script with `import`. The example shows `def greet(name): return f"Hello, {name}!"` which can then be used as `import mymodule print(mymodule.greet('Alice'))` . This not only modularizes code but facilitates testing, versioning, and collaborative development.

List comprehensions in Python offer a concise way to create lists, enhancing readability and often performance compared to traditional loops. They are particularly beneficial when filtering data, as they allow conditions to be applied directly within the comprehension. This can be seen in `even_squares = [x*x for x in numbers if x % 2 == 0]`: only even numbers are squared and included in the new list . They reduce boilerplate code and potential loop-related errors, making operations like mapping and filtering intuitive and efficient.

To find the second largest number in a list in Python, you can sort the list and then retrieve the second-to-last element. This approach is efficient for lists that need to be sorted for other operations, as sorting is O(n log n). You use the sorted list, nums, and fetch the element at index -2: `nums.sort() print('Second largest:', nums[-2])` . However, if sorting is not needed, using a single pass to find the largest and second largest would be more optimal with O(n) complexity.

Unpacking tuples in Python is beneficial because it allows for the assignment of multiple variables in a single, concise operation, enhancing code readability and reducing errors from manual assignments. Typical use cases include extracting configuration settings, returning multiple values from functions, and iterating over combinations from data structures. The provided example `my_tuple = (10, 20, 30) a, b, c = my_tuple` demonstrates unpacking, where each element of the tuple is assigned to a corresponding variable . This can simplify handling functions that return tuples, particularly when dealing with multiple returned values.

Using variable arguments in functions, achieved with `*args` or `**kwargs`, allows these functions to accept an arbitrary number of arguments, facilitating flexibility and adaptability. They can support functions where the number of inputs is not predetermined, enabling dynamic operations such as logging multiple events or operations. The function `def print_args(*args): for arg in args: print(arg)` demonstrates this, printing any number of arguments provided . They need to be used judiciously to avoid obscuring function input expectations, often accompanied by clear documentation.

When choosing between lists and tuples, consider mutability, performance, and use case needs. Lists (`[1, 2, 3]`) are mutable, allowing for dynamic modifications, such as appending or removing items, suitable for collections where frequent changes are required. Tuples (`(1, 2, 3)`) are immutable, potentially improving read access performance and data integrity for fixed datasets . Since tuples can be used as dictionary keys or elements of sets while lists cannot, their immutability makes tuples ideal for constant data that is accessed frequently but not modified. Additionally, memory usage may vary during operations where mutability is a factor.

The output order in tuple unpacking in Python is fixed and corresponds directly to the order of elements in the tuple. This predictable sequence ensures that each variable receives the expected element value, which is crucial for program logic requiring specific positional data, as shown with `my_tuple = (10, 20, 30) a, b, c = my_tuple` assigns 10 to `a`, 20 to `b`, and 30 to `c` . Misordering can lead to logic errors and faulty data manipulation, underscoring the importance of order consistency in tuple operations.

Recursive functions are preferable when the problem has a natural recursive structure, such as navigating tree data structures or when a mathematical formula is naturally defined recursively, like calculating factorials (`def factorial(n): if n == 0: return 1 return n * factorial(n-1)`). They can make complex problems easier to solve and understand by reducing otherwise convoluted iterative code to clean, readable forms. However, recursion may lead to increased memory usage and hitting recursion limits, so it's best when stack depth is predictable and small.

Inheritance in Python allows a class (subclass) to inherit attributes and behaviors (methods) from another class (superclass), promoting code reuse and reducing redundancy. The `super()` function is particularly useful as it allows you to call methods from the superclass in your subclass. For instance, in the example `class Child(Parent): def __init__(self): super().__init__() print("Child constructor")`, `super()` is used to call the `__init__()` method of the `Parent`, ensuring that any initialization logic in the parent class is executed . It is crucial when extending or modifying inherited behaviors while maintaining the existing logic.

You might also like