Mod 5 Flashcards
How are Modules identified? What do they consist of? How do you use them? How do they relate to the Python standard library?
- it is identified by its name
- forms the Python standard library (along with functions)
- consists of entities (functions, variables, constants, classes and objects)
- must import before using
Write the code to import the math module
import math
Where must the “import math” instruction be placed in the code?
It can go anywhere in the code, but must be placed before it is used.
Write the code to access the pi entity after the “import math” statement:
math.pi for example (math.pi/2)
Write the import statement to access the pi entity within the math module:
from math import pi
Write the code to access the pi entity after the “from math import pi” statement:
pi
for example pi/2
Write the code to import all entities from the math module:
from math import *
Write the code to import the math module with an alias of "m": Also, is the math module name usable? (for example math.pi)
import math as m
no
Write the code to import the pi entity with an alias of “PI”
from math import pi as PI
what does dir(module) do?
returns sorted list containing all entities names in the module
What does the random() function do? What is its syntax?
produces a random float number between 0 and 1.
random()
What does randrange() do? What is its syntax?
produces a random integer value within a specified range.
randrange(start, stop, step)
What does choice() do? What is its syntax?
Takes in a sequence (list for example) and chooses a random element
choice(sequence)
What does sample() do? What is its syntax?
draws the numbers randomly so they are unique and no duplicates sample(sequence, # to choose) sample of code: from random import * list=[1,2,3,4,5] print(sample(list,3))
What does the platform module do? What of the platform() function?
module - underlying platforms data (hardware, operating system, interpreter version info) function - environment description Sample of code: from platform import platform print(platform()) Linux-4.4.0-206-generic-x86_64-with
What does the machine() function do?
See generic name of the processor Sample of code: from platform import machine print(machine()) x86_64
What does the processor() function do?
See real processor name (if possible) Sample of code: from platform import processor print(processor()) #returns nothing for me
What does the system() function do?
See Operating System name Sample of code: from platform import system print(system()) Linux
What does the version() function do?
See Operating System version Sample of code: from platform import system print(version()) #238-Ubuntu SMP Tue Mar 16 07:52:37 UTC 2021
What does the python_implementation() function do?
see the python implementation Sample of code: from platform import python_implementation print(python_implementation()) CPython
What does python_version_tuple function do?
see 3 element function with the major part of Pythons version, the minor part, the patch level number Sample of code: from platform import python_version_tuple for atr in python_version_tuple(): print(atr) 3 7 10
How do functions, modules and packages relate to each other?
Packages contains modules and modules contain functions.
When a module is imported what happens to the module and its content?
- its content is implicitly executed by Python
- it initializes (which only happens once)
What happens to __name__ when its file is ran?
What happens when a file with __name__ is imported?
- when you run a file directly, its __name__ variable is set to __main__
- when a file is imported as a module, its __name__ variable is set to the files name (excluding .py)
Sample of code:
module.py file:
print(“I like python”) #output: I like python
print(__name__) output: __main__
main.py file:
import file #output: I like python
module
When underscores are added before and after a variable name what does it do?
It makes the variable personal/private. It is a convention however and users may or may not obey it.
True or False: Python is able to treat zip files as ordinary folders
True
What module is the path variable from? What does it do? Explain its behavior?
- Accessible through sys module
- It stores all locations that are searched to find a module (module requested by the import instruction)
- searches in order, based on the list
- the search starts with the first path’s element
Write the code to find a module, start the search in “‘C:\Users\user\py\modules” folder:
from sys import path path.append('..\\modules') import module or from sys import path path.insert(1, 'C:\\Users\\user\\py\\modules') import module
How do you transform a folder directory to a Python package?
Initialize by including the “__init__.py” file inside the package’s folder.
The content of the file is executed when any of the packages modules are imported. You can leave the file empty if you there are no special initializations.
Facts about the __init__.py file (4):
- The content of the file is executed when any of the packages modules are imported.
- You can leave the file empty if you there are no special initializations.
- the file can go anywhere in the package
- can have multiple init files per package
Where do you put the subtree to make it accessible to Python?
anywhere
Write the code to access the funS() function from the sigma module from the best, good, extra package(3):
from sys import path path.append('..\\packages') import extra.good.best.sigma print(extra.good.best.sigma.funS()) or from sys import path path.insert(1, 'C:\\Users\\user\\py\\modules') from extra.good.best.sigma import funS print(funS()) or from sys import path path.append('..\\packages') import extra.good.best.sigma as sig print(sig.funS())
What are the 2 steps that Python does when raising an exception?
- Stops the program
2. Creates data called an exception
What happens when an exception is handled? What of when it is not handled?
- the program will resume and execution can continue
- The program will be forcibly terminated and user will see an error message in the console
What is the output of the code when 1 and 1 are inputted?
firstNumber = int(input(“Enter the first number: “))
secondNumber = int(input(“Enter the second number: “))
try:
print(firstNumber / secondNumber)
except:
print(“This operation cannot be done.”)
print(“THE END.”)
1.0
THE END.
What is the output of the code when 1 and 0 are inputted?
firstNumber = int(input(“Enter the first number: “))
secondNumber = int(input(“Enter the second number: “))
try:
print(firstNumber / secondNumber)
except:
print(“This operation cannot be done.”)
print(“THE END.”)
This operation cannot be done.
THE END.
What is the output of: try: print("1") x = 1 / 0 print("2") except: print("Oh dear, something went wrong...")
print(“3”)
1
Oh dear, something went wrong…
3