Modules, Packages and PIP Flashcards

1
Q

What is a module?

A

a file containing Python definitions and statements, which can be later imported and used when necessary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a namespace?

A

A space in which some names exists and the names don’t conflict with each other

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

difference between:
import math
from math import pi

A

import math imports all entities in math and they will be called like math.pi

from math import pi imports pi only and will be called like pi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

from module import * is risky because…

A

it imports all entities from module as their name e.g. pi not math.pi. need to be careful or conflicting names in code already

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Import module using alias?

A

import module as alias

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the platform module allow you to do?

A

The platform module lets you access the underlying platform’s data, i.e., hardware, operating system, and interpreter version information.

from platform import platform

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you find out the generic name of the processor which runs your OS together with Python and your code?

A

from platform import machine

print(machine())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the processor function do?

A

The processor() function returns a string filled with the real processor name (the name of the CPU) (if possible).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the system function do?

A

A function named system() returns the generic OS name as a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the version function do?

A

The OS version is provided as a string by the version() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does python_implementation() do?

A

returns a string denoting the Python implementation (expect CPython here, unless you decide to use any non-canonical Python branch)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does python_version_tuple() do?

A

returns a three-element tuple filled with:
the major part of Python’s version;
the minor part;
the patch level number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

(Complete the sentence) Setting the generator’s seed with the same value each time your program is run guarantees that…

A

.. the pseudo-random values emitted from the random module will be exactly the same.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

functions make up modules and modules make up ……

A

Packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does the name variable do when run directly or imported as a module?

A

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 file’s name (excluding .py)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does a name beginning with _ or __ mean?

A

Inform users of your module that this is your variable, that they may read it, but that they should not modify it under any circumstances.

The entity should be treated as PRIVATE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does the #! line at the beginning of a module do?

A

For Unix and Unix-like OSs (including MacOS) such a line instructs the OS how to execute the contents of the file (in other words, what program needs to be launched to interpret the text). In some environments (especially those connected with web servers) the absence of that line will cause trouble;

The names shabang, shebang, hasbang, poundbang, and hashpling describe the digraph written as #!, used to instruct Unix-like OSs how the Python source file should be launched. This convention has no effect under MS Windows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does a doc-string do?

A

Explain the contents of the module as a comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you specify the folder you want the module from?

A

from sys import path

path.append(‘C:\Users\user\py\modules’)

import module

If you want convince Python that it should take into account a non-standard package’s directory, its name needs to be inserted/appended into/to the import directory list stored in the path variable contained in the sys module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does the __init__.py file do?

A

The __init__.py file lets the Python interpreter know that a directory contains code for a Python module. An __init__.py file can be blank. Without one, you cannot import modules from another folder into your project.

A Python file named __init__.py is implicitly run when a package containing it is subject to import, and is used to initialize a package and/or its sub-packages (if any). The file may be empty, but must not be absent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Define a package?

A

a package is a container which enables the coupling of several related modules under one common name. Such a container can be distributed as-is (as a batch of files deployed in a directory sub-tree) or it can be packed inside a zip file.

22
Q

What does the __pycache__ directory contain?

A

During the very first import of the actual module, Python translates its source code into the semi-compiled format stored inside the pyc files, and deploys these files into the __pycache__ directory located in the module’s home directory.

23
Q

What is the centralized repository of all available software packages called?

A

PyPI. (it’s short for Python Package Index) and it’s maintained by a workgroup named the Packaging Working Group, a part of the Python Software Foundation, whose main task is to support Python developers in efficient code dissemination.

24
Q

What does pip stand for?

A

pip install packages

25
Q

How can you see what python package have already been installed?

A

pip list

26
Q

How can you get info on installed packages?

A

pip show package_name

27
Q

How can you see which packages are dependent on which?

A

pip show package_name

the two lines at the bottom of the output. They show:

which packages are needed to successfully utilize the package (Requires:)
which packages need the package to be successfully utilized (Required-by:)

28
Q

What does pip install –user package_name do?

A

–user The presence of this option instructs pip to act locally on behalf of your (non-administrative) user.

29
Q

How to update a package or get a specific version?

A

pip install -U package_name
pip install package_name==package_version

30
Q

How to uninstall package?

A

pip uninstall package_name

31
Q

What does a pyc file contain?

A

Complied python code

32
Q

What does dir() do?

A

A list of all module attributes, including the hidden ones

33
Q

What does pip show do?

A

pip show will give you information about installed packages

34
Q

What does pip wheel do?

A

pip wheel archives requirements and dependencies (making Anaconda Navigator seem even less useful)

35
Q

What does pip help do?

A

pip help is where you get information about pip itself

36
Q

What are the pip commands?

A

Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
cache Inspect and manage pip’s wheel cache.
index Inspect information available from package indexes.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.

37
Q

What will srqt() reutrn?

A

A FLOAT!!!!!!

38
Q

How would you check pip version?

A

pip - - version

39
Q

print(__name__)

A

__main__

40
Q

What does trunc() do?

A

The math.trunc() method returns the truncated integer part of a number.

Note: This method will NOT round the number up/down to the nearest integer, but simply remove the decimals.

41
Q

What does hypot() do?

A

The math.hypot() method returns the Euclidean norm. The Euclidian norm is the distance from the origin to the coordinates given.

math.hypot(x1, x2, x3, …, xn)

calculated by:
sqrt(x1x1 + x2x2 +x3x3 …. xnxn).

42
Q

What does randrange() do?

A

random.randrange(start, stop, step)

stop NOT INCLUDED

43
Q

What does random() do?

A

The random() method returns a random floating number between 0 and 1.

44
Q

What does choice() do?

A

The choice() method returns a randomly selected element from the specified sequence.

The sequence can be a string, a range, a list, a tuple or any other kind of sequence.

random.choice(sequence)

45
Q

What does sample() do?

A

random.sample(sequence, k)

The sample() method returns a list with a randomly selection of a specified number of items from a sequence.

46
Q

This is the output of which platform function:

Linux-5.10.147+-x86_64-with-glibc2.27

A

platform()

47
Q

This is the output of which two platform function:

x86_64

A

machine() or processor()

48
Q

This is the output of which platform function:

Linux

A

system()

49
Q

1 SMP Sat Dec 10 16:00:40 UTC 2022

This is the output of which platform function:

A

version()

50
Q

This is the output of which platform function:

CPython

A

python_implementation()

51
Q

This is the output of which platform function:

(‘3’, ‘8’, ‘16’)

A

python_version_tuple()