Basics Flashcards
(25 cards)
How does python interact with variables inside of a function.
If we assign a new value to a variable, regardless of if it’s immutable and mutable data type then the change will be lost once we come out of the function:
What is Python?
Interpreted high-level object-oriented dynamically-typed scripting language.
Why Python?
Python is an object-oriented programming language and can be used to write functional code too.
Subsequently, it takes less time to bring a Python program to market compared to other languages such as C#/Java.
Why not Python?
Python is slower than C++, C#, Java. This is due to the lack of Just In Time optimisers in Python.
Python is not suitable for low-level systems and hardware interaction.
How Python works?
A Python virtual machine is created where the packages (libraries) are installed. Think of a virtual machine as a container.
The python code is then written in .py files
CPython compiles the Python code to bytecode. This bytecode is for the Python virtual machine.
Why is python machine independent?
When you want to execute the bytecode then the code will be interpreted at runtime. The code will then be translated from the bytecode into the machine code. The bytecode is not dependent on the machine on which you are running the code. This makes Python machine-independent.
Using global keyword
You can declare a global variable outside of functions. It’s important to note that to assign a global variable a new value, you will have to use the “global” keyword
dir() and help()
dir() -displays defined symbols
help() — displays documentation
creating new variables
Python doesn’t always create a new memory address for all new variables.
what if we assign two different variables a string value such as ‘a’. Will it create two memory addresses?
No
What if we create two different variables and assign them a long string value
Yes,
This is because Python creates an internal cache of values when it starts up. This is done to provide faster results. It creates a handful of memory addresses for small integers such as between -5 to 256 and smaller string values. This is the reason why both of the variables in our example had the same ID.
== vs is
If we use == then it will check whether the two arguments contain the same data
If we use is then Python will check whether the two objects refer to the same object. The id() needs to be the same for both of the objects
What is a module
Modules encourage componentised design in your solution.
They provide the concept of namespaces to help you share data and services.
Modules encourage code reusability and reduce variable name clashes.
PYTHONPATH
This environment variable indicates where the Python interpreter needs to navigate to locate the modules. PYTHONHOME is an alternative module search path.
importing modules
import my_module
print(my_module.my_object)
If you do not want the interpreter to execute the module when it is loaded then you can check whether the __name__ == ‘__main__’
using from
from my_module import my_object
print(my_object)
Modules are only imported on the first import.
Packages
Package is a directory of modules.
Packages enable us to organise our modules better which helps us in resolving issues and finding modules easier.
If our solution offers similar functionality then we can group the modules into a package:
from packageroot.packagefolder.mod import my_object
__init__.py files
As __init__.py files are imported before the modules are imported, you can add custom logic such as start service status checks or to open database connections, etc.
Compilation And Linking
It is important to add a line in the Setup.local file to ensure that the newly created file can be loaded. Run the file using spam file.o
Changing the file requires running rebuildMakefile
Iterators
All iterators contain __iter__() and __next__() functions
Simply execute iter(x) on lists, dictionaries, strings or sets.
Therefore we can execute next(iter) where iter = iter(list) as an instance.
Iterators are useful if we have a large number of items in a collection and we do not intend to load all of the files in memory at once. Iterators are memory efficient and can represent an infinite stream
shallow copy
import copy
m = MyClass(123)
mm = copy.copy(m)
This will result in shallow copy as the reference pointers of the properties will be copied.
updating the property in the source object will result in updating the property in the target object.
deep copy
import copy
m = MyClass(123)
mm = copy.deepcopy(m)
If MyClass contains a property that references MyOtherClass object then the contents of the property will be copied in the newly created object via deepcopy.
How to make Python Run Fast?
Implement C language-based extensions.
Create multi-processes by using Spark or Hadoop
Utilise Cython, Numba, and PyPy to speed up your Python code or write it in C and expose it in Python like NumPy
Writing files
with open(file path) as my_file, open(another path) as second_file: for (line number, (line1, line2)) in enumerate(zip(my_file, second_file):
rw — read-write mode
r — read mode
rb — read in binary format mode e.g. pickle file
r+ — read for both reading and writing mode
a — append mode.
w+ — for reading and writing, overwrites if the file exists