1-10 Py
1-10 Py
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}!")
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.
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
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
The open() function in Python allows you to open files in different modes:
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