Podcast
Podcast
Podcast
Something went wrong
Questions and Answers
Questions and Answers
Which statement best describes the primary benefit of using modules in Python?
Which statement best describes the primary benefit of using modules in Python?
- Modules allow direct manipulation of hardware components connected to the computer.
- Modules bypass Python's interpreter to execute code directly in the operating system kernel.
- Modules facilitate code reusability and organization by logically grouping related functions and classes. (correct)
- Modules automatically optimize code execution speed without requiring explicit optimization techniques.
Suppose you have a module named geometry.py
containing functions for calculating areas. Which of the following is the correct way to import the calculate_circle_area
function from this module?
Suppose you have a module named geometry.py
containing functions for calculating areas. Which of the following is the correct way to import the calculate_circle_area
function from this module?
- import geometry.calculate_circle_area
- from geometry import calculate_circle_area (correct)
- import calculate_circle_area from geometry
- module geometry import calculate_circle_area
If you want to import a module named utils
and give it an alias ut
for easier reference, how would you write the import statement in Python?
If you want to import a module named utils
and give it an alias ut
for easier reference, how would you write the import statement in Python?
- module utils like ut
- import utils = ut
- import utils as ut (correct)
- alias utils as ut
Consider two Python files, main.py
and helper.py
, located in the same directory. If helper.py
defines a function greet()
, how can main.py
access and call this function?
Consider two Python files, main.py
and helper.py
, located in the same directory. If helper.py
defines a function greet()
, how can main.py
access and call this function?
You are developing a calculator application and decide to modularize the code. You create an operations.py
module with functions like add
, subtract
, multiply
, and divide
. How would you use these functions in your main.py
file after importing the operations
module?
You are developing a calculator application and decide to modularize the code. You create an operations.py
module with functions like add
, subtract
, multiply
, and divide
. How would you use these functions in your main.py
file after importing the operations
module?
In the context of the 'Problem Set 1' salary computation app, what is the primary responsibility of the GrossSalary.py
module?
In the context of the 'Problem Set 1' salary computation app, what is the primary responsibility of the GrossSalary.py
module?
Within the 'Problem Set 1' salary application, how is the tax amount (12% of gross salary) and hourly rate (500/hr) typically managed?
Within the 'Problem Set 1' salary application, how is the tax amount (12% of gross salary) and hourly rate (500/hr) typically managed?
In 'Problem Set 2', how would the application select a first name, middle name and last name?
In 'Problem Set 2', how would the application select a first name, middle name and last name?
What does it mean when built-in modules in Python are described as being written in C?
What does it mean when built-in modules in Python are described as being written in C?
How can you list all available built-in modules in Python using the interpreter?
How can you list all available built-in modules in Python using the interpreter?
Which of the following is NOT a built-in module in Python?
Which of the following is NOT a built-in module in Python?
What is the primary purpose of the math
module in Python?
What is the primary purpose of the math
module in Python?
If you want to generate a random integer between 1 and 100 (inclusive) in Python, which function from the random
module should you use?
If you want to generate a random integer between 1 and 100 (inclusive) in Python, which function from the random
module should you use?
Which function from the random
module would you use to shuffle the elements of a list randomly?
Which function from the random
module would you use to shuffle the elements of a list randomly?
What is the main purpose of the datetime
module in Python?
What is the main purpose of the datetime
module in Python?
If you have a datetime
object named my_date
representing a specific date, how would you extract the year from it?
If you have a datetime
object named my_date
representing a specific date, how would you extract the year from it?
How can you format a datetime
object to display the full weekday name (e.g., 'Wednesday') using strftime()
?
How can you format a datetime
object to display the full weekday name (e.g., 'Wednesday') using strftime()
?
What output would datetime.datetime(2023, 1, 15).strftime('%m/%d/%Y')
produce?
What output would datetime.datetime(2023, 1, 15).strftime('%m/%d/%Y')
produce?
A developer is using the datetime
module and needs to display the time in a 12-hour format with AM/PM. Which strftime()
directive should they use?
A developer is using the datetime
module and needs to display the time in a 12-hour format with AM/PM. Which strftime()
directive should they use?
When using strftime()
, which directive represents the day of the year (001-366)?
When using strftime()
, which directive represents the day of the year (001-366)?
Questions and Answers
Something went wrong
Flashcards
Flashcards
What is a Python module?
What is a Python module?
A module is a file that contains Python code, which can include functions, classes, and other definitions.
Advantages of using modules
Advantages of using modules
Using modules logically organizes Python code, making it easier to understand, reuse, and maintain.
How to use a module?
How to use a module?
You can use an 'import' statement to bring functions or definitions from one Python source file (module) into another.
What does 'import moduleName as moduleAlias' do?
What does 'import moduleName as moduleAlias' do?
Signup and view all the flashcards
What does 'from moduleName import functionName' do?
What does 'from moduleName import functionName' do?
Signup and view all the flashcards
What does 'import moduleName' do?
What does 'import moduleName' do?
Signup and view all the flashcards
What are built-in modules?
What are built-in modules?
Signup and view all the flashcards
How to view built-in modules?
How to view built-in modules?
Signup and view all the flashcards
What is the 'Math' module for?
What is the 'Math' module for?
Signup and view all the flashcards
What is the 'Random' module for?
What is the 'Random' module for?
Signup and view all the flashcards
What is the 'DateTime' module for?
What is the 'DateTime' module for?
Signup and view all the flashcards
%A directive for datetime
%A directive for datetime
Signup and view all the flashcards
%Y directive for datetime
%Y directive for datetime
Signup and view all the flashcards
%H directive for datetime
%H directive for datetime
Signup and view all the flashcards
%M directive for datetime
%M directive for datetime
Signup and view all the flashcards
%S directive for datetime
%S directive for datetime
Signup and view all the flashcards
Flashcards
Something went wrong
Study Notes
Study Notes
Modules and Libraries
- A module is a file containing Python code, which can include functions and classes.
- Using modules helps logically organize Python code.
- Modules make code easier to understand and use.
- Any Python source file can be used as a module by using the import statement in another Python source file.
- Import statements include
import moduleName as moduleAlias
,from moduleName import functionName
, andimport moduleName
.
Creating and Accessing Modules
- To create a module, save Python code in a
.py
file (e.g.,greetings.py
). - To access a module, use the
import
statement (e.g.,import greetings
). - Module contents can be accessed using dot notation (e.g.,
greetings.greet()
). - When using modules, ensure that the main script and the module files are in the same project.
Calculator App Example
- A calculator app can use separate modules for each operation like addition, subtraction, multiplication, and division.
- Create a module named
operation.py
with functions for add, subtract, multiply, and divide.
Problem Set 1: Salary Computation App
- Create a Salary Computation App using three modules:
GrossSalary.py
,SalaryDeductions.py
, andNetSalary.py
. - GrossSalary.py computes the gross salary based on user inputs such as name, hours worked, and a fixed hourly rate of Php 500.00/hr.
- SalaryDeductions.py computes deductions.
- NetSalary.py computes the net salary.
- Tax is fixed at 12% of the gross salary.
Problem Set 2: Name Generator App
- Create a Name Generator App.
- Use three lists for first name, middle name, and last name, each containing 10 items.
- Randomly select items from each list to generate a new name.
- Display the generated name with a congratulatory message.
- If the user chooses not to generate a new name, display a thank you message and show all previously generated names.
Built-In Modules
- Built-in modules are written in C and interpreted using the Python interpreter.
- To view all built-in modules, use
help('modules')
. - Some built-in modules include math, random, and datetime.
Random Module
- The Random module implements pseudo-random number generators for various distributions.
random.randrange(myNum, myRange)
function returns a randomly selected element from the specified range.random.shuffle(prizes)
can be used to shuffle the order of elements in a list.
DateTime Module
- This module is used to work with dates as date objects.
datetime.datetime.now()
function returns the current date and time.x.year
returns the year value.x.strftime("%A")
returns the weekday's full name.datetime.datetime(2020, 5, 17)
creates a specific date (year, month, day).
String Placeholders for strftime()
Method
- These placeholders define how date and time components are formatted in strings.
%a
: Weekday, short version (e.g., Wed).%A
: Weekday, full version (e.g., Wednesday).%w
: Weekday as a number (0-6, 0 is Sunday).%d
: Day of the month (01-31).%b
: Month name, short version (e.g., Dec).%B
: Month name, full version (e.g., December).%m
: Month as a number (01-12).%y
: Year, short version, without century.%Y
: Year, full version.%H
: Hour (00-23).%I
: Hour (00-12).%p
: AM/PM.%M
: Minute (00-59).%S
: Second (00-59).%f
: Microsecond (000000-999999).%z
: UTC offset.%Z
: Timezone.%j
: Day number of year (001-366).%U
: Week number of year, Sunday as the first day of week (00-53).%W
: Week number of year, Monday as the first day of week (00-53).%c
: Local version of date and time.%C
: Century.%x
: Local version of date.%X
: Local version of time.%%
: A % character.%G
: ISO 8601 year.%u
: ISO 8601 weekday (1-7).%V
: ISO 8601 weeknumber (01-53).
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Study Notes
Something went wrong