Save
BSIE First Year-1st Semester
CMPE
Week 5
Save
Share
Learn
Content
Leaderboard
Learn
Created by
Akin lang si Maloi
Visit profile
Cards (43)
What is a module in Python?
A
self-contained
file containing Python code.
View source
What is the purpose of modules in Python?
To
organize
and
reuse
code effectively.
View source
How do modules promote code modularity?
By encapsulating
functions
,
classes
, and
variables
.
View source
What are the key benefits of using modules in Python?
Code
Organization
: Logical grouping and improved readability.
Code
Reusability
: Shared functionality and standardization.
Collaboration
: Facilitates teamwork on independent modules.
View source
What is an example of a module that could be created for a web application?
A utils.py module for common utility functions.
View source
How would you define a function to send an email in a module?
def send_email(recipient, message):
View source
What is the purpose of the import statement in Python?
To import a module into your Python script.
View source
What is the basic syntax for importing a module?
import
module_name
View source
How would you import the math module in Python?
import
math
View source
How do you access a function from an imported module?
Using
dot
notation.
View source
How would you calculate the square root of 16 using the math module?
result = math.sqrt(16)
View source
What is the syntax to import specific functions from a module?
from module_name import function_name, variable_name
View source
How would you import only the pi constant from the math module?
from
math import pi
View source
How do you calculate the area of a circle using the imported pi constant?
area = pi * radius**2
View source
What are some additional notes regarding module imports in Python?
Multiple Imports
: Import multiple modules in one statement.
Renaming Imports
: Use 'as' to rename imports.
Package Imports
: Use dot notation for packages.
View source
What does the math module provide?
A
variety
of mathematical functions.
View source
What does the math.sqrt(x) function do?
Calculates the
square root
of a number x.
View source
How would you calculate the hypotenuse of a right triangle with sides 3 and 4 using the math module?
hypotenuse = math.sqrt(3**2 + 4**2)
View source
What does the random module do?
Generates
pseudo-random
numbers.
View source
What does random.randint(a, b) do?
Generates a random integer between a and b.
View source
How would you simulate a dice roll using the random module?
dice_roll = random.
randint
(1, 6)
View source
What does random.random() return?
A random
floating-point
number between 0.0 and 1.0.
View source
What does random.choice(sequence) do?
Randomly selects an element from a sequence.
View source
What does random.shuffle(sequence) do?
Shuffles the elements in a sequence in-place.
View source
What does the datetime module allow you to do?
Work with dates and times.
View source
What does datetime.now() return?
The current date and time as a datetime object.
View source
How would you calculate the time difference between two events using the datetime module?
time_difference = end_time - start_time
View source
How do you create a specific date object using the datetime module?
datetime.date(year, month, day)
View source
How do you create a specific time object using the datetime module?
datetime.time(hour, minute, second, microsecond)
View source
What is the structure of a Python module?
A
.py
file containing Python code.
View source
How do you import a module from the same directory?
Use the
import statement
.
View source
How do you import a module from a different directory?
Add the directory to
Python's
module search path.
View source
What is a package in Python?
A directory containing one or more modules and an
__init__.py
file.
View source
What is the purpose of the __init__.py file in a package?
It declares the
directory
as a package and can contain initialization code.
View source
How does Python treat directories containing an __init__.py file?
As
packages
, allowing
module
imports using
dot notation
.
View source
What can the __init__.py file contain?
Initialization code that runs when the
package
is imported.
View source
How can you control imports in a package using __init__.py?
By selectively importing
modules
in the __init__.py file.
View source
How would you import a specific function from a module in a package?
from .
module_name
import
function_name
View source
What is the structure of a package in Python?
A directory with an
__init__.py
file and one or more
modules
.
View source
What does the Person class in the example do?
Calculates
age based on the
current year
and
birth year
.
View source
See all 43 cards