What is the purpose of using modules in Python?
To organize code into reusable and logical pieces.
How do you define a class in Python?
Using the 'class' keyword followed by the class name and a colon.
What method is automatically called when an object is instantiated?
__init__ method.
How can inheritance be implemented in Python?
By specifying the parent class name in parentheses after the class name.
What is the difference between 'append()' and 'extend()' methods in lists?
'append()' adds its argument as a single element, 'extend()' adds each element of its argument.
How do you handle naming conflicts in modules?
Using namespaces or importing modules with specific names using 'import as'.
What is a namespace package?
A directory containing modules, allowing for hierarchies and grouping of similar modules.
How do you define a function in Python?
Using the 'def' keyword, function name, parentheses with any parameters, and a colon.
How can a method of a superclass be invoked from a subclass?
Using the 'super()' function.
What are Python's built-in data types mentioned?
bool, int, float, str, list, tuple, range, set, dict.
Explain:
typed/non typed
dynamic/static
programming languages
what is python?
typed:
dynamic:
python: strongly typed + dynamic
rules for variable names
letters, digits, underscores allowed
not letter as first digit
no words from keyword.kwlist
use_this
doNotUseThis
else if statement
match-cased
calculate with booleans
possible -> as 0 or 1
str:
multiline
ignore linebreak
concat
sequences
ordered collection of values
Strings, lists, tupels and ranges
list
mutable -> veränderbar
heterogeneous -> versch. datatypes speichdern
ordered -> sortiert
sequences of elements
append
vs. extend
vs. concat
tuple
immutable -> nicht vereänderbar
heterogeneous -> versch. Datatypes möglich
range(3)
range(3, 9, 2)
range(0,-10,-4)
0, 1, 2
3, 5, 7
0, -4, -8
loops
dictionary
mapping hash values to arbitrary values
heterogeneous -> versch. datatypes
unordered -> keine sortierung
NO concatenation with +
add entries
set
mutable
heterogeneous
unordered
.add()
xxx in my_set
type conversion
bool()
int()
float()
str()
list()
tuple()
set()
data types overview
control flow overview
function with empty body
function names
Use lowercase for the function names, with words separated by underscores (just like the variables).
classes
initialize
The class mechanism allows us to create new data types, combining state and behavior. To create a new class, use the class keyword:
classes inheritance
overwrite methods
classes names
Class names should follow upper camel case convention (WriteYourClassNamesLikeThis). The same restrictions on characters apply.
We can develop code in Python. We can make it persistent by saving a Jupyter Notebook. How do we save our code as a program that can be run later?
Let’s say we’ve decided to develop a bunch of functions implementing matrix multiplication and function differentiation. How can we group them together for easy access in the future, like a... library?
We need to use modules.
Where does import statement load files from?
To simplify things, from the Python installation directory, from the system path, and from the working directory.
What if our collection of i18n packages grew to include french, spanish, italian, portuguese, etc.? We don’t want to have all these modules (and, presumably, many others) in the root directory of our program.
We can use packages to solve the problem!
We explicitly specified the modules we wanted to import from a package. What happens if we import only the package itself? Let’s create a farewells directory and the following files in the directory:
Loading a namespace package doesn’t give us access to the modules contained in the package.
To access the modules we either have to explicitly import them or use a“regular” package.
regular package
conda
Last changed10 months ago