1.
Define function in Python
A function in Python is a reusable block of code that performs a specific task. You define a function using
the def keyword followed by the function name, parentheses for arguments, a colon, and the function
body indented with statements.
Python
def greet(name):
"""Prints a greeting message."""
print(f"Hello, {name}!")
greet("Alice") # Output: Hello, Alice!
Use code with caution.
2. fopen syntax (Not applicable to Python)
The fopen function is a C function for opening files. Python has a built-in open() function that provides a
more convenient way to handle file operations.
3. Four methods in the math module
The math module provides various mathematical functions:
math.sqrt(x): Calculates the square root of x.
math.ceil(x): Returns the ceiling of x (smallest integer greater than or equal to x).
math.floor(x): Returns the floor of x (largest integer less than or equal to x).
math.pow(x, y): Calculates x raised to the power of y.
Python
import math
result = math.sqrt(16) # result will be 4.0
Use code with caution.
4. Built-in class attributes
Every class has some built-in attributes like:
__dict__: A dictionary containing the class’s namespace.
__doc__: The docstring of the class.
__name__: The name of the class.
__bases__: A tuple containing the base classes of the class (for inheritance).
You can access them using the class name or an object of the class.
5. Use of NumPy
NumPy is a powerful library for numerical computation in Python. It provides efficient multidimensional
arrays and a wide range of mathematical functions. Here’s an example:
Python
import numpy as np
array = np.array([1, 2, 3])
mean = np.mean(array) # mean will be 2.0
Use code with caution.
6. Namespace in Python
A namespace is a logical naming system that prevents naming conflicts between variables or functions
defined in different parts of your code. Python uses a hierarchical namespace with built-in names at the
top level and modules/classes having their own namespaces. Names defined within a function or class
are local to that scope.
7. Module in Python
A module is a reusable block of Python code that groups functions, classes, and variables. Modules
promote code organization and reusability. Here’s an example of creating a simple module:
Python
# my_module.py
def greet(name):
print(f"Hello, {name}!")
Use code with caution.
Then, use import to access the module’s contents:
Python
import my_module
my_module.greet("Bob") # Output: Hello, Bob!
Use code with caution.
Advantages of modules:
Code organization and reusability.
Separation of concerns.
Sharing code across projects.
8. Different modes of opening a file
The open() function in Python allows you to open files in different modes:
’r’: Read mode (default) - Opens the file for reading.
’w’: Write mode - Creates a new file or overwrites an existing one.
’a’: Append mode - Opens the file for appending content at the end.
’x’: Exclusive creation mode - Creates a new file and fails if it already exists.
’b’: Binary mode - Opens the file in binary mode (useful for non-text data).
9. Exception handling for negative input
Python
def get_positive_number():
while True:
try:
value = int(input("Enter a positive number: "))
if value < 0:
raise ValueError("Input must be a positive number.")
return value
except ValueError as e:
print(e)
number = get_positive_number()
print(f"You entered: {number}")
Use code with caution.
10. Concatenating strings
Using the + operator:
Python
str1 = "Hello"
str2 = "World"
combined_str = str1 + " " + str2
print(combined_str) # Output: Hello World