Basics Flashcards

(25 cards)

1
Q

How does python interact with variables inside of a function.

A

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:

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

What is Python?

A

Interpreted high-level object-oriented dynamically-typed scripting language.

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

Why Python?

A

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.

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

Why not Python?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How Python works?

A

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.

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

Why is python machine independent?

A

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.

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

Using global keyword

A

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

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

dir() and help()

A

dir() -displays defined symbols

help() — displays documentation

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

creating new variables

A

Python doesn’t always create a new memory address for all new variables.

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

what if we assign two different variables a string value such as ‘a’. Will it create two memory addresses?

A

No

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

What if we create two different variables and assign them a long string value

A

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.

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

== vs is

A

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

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

What is a module

A

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.

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

PYTHONPATH

A

This environment variable indicates where the Python interpreter needs to navigate to locate the modules. PYTHONHOME is an alternative module search path.

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

importing modules

A

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__’

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

using from

A

from my_module import my_object

print(my_object)

Modules are only imported on the first import.

17
Q

Packages

A

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

18
Q

__init__.py files

A

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.

19
Q

Compilation And Linking

A

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

20
Q

Iterators

A

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

21
Q

shallow copy

A

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.

22
Q

deep copy

A

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.

23
Q

How to make Python Run Fast?

A

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

24
Q

Writing files

A
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

25
Garbage collection
All of the objects in Python are stored in heap space. This space is accessible to the Python interpreter. It is private. It means Python can allocate and de-allocate the memory for your program automatically such as in C++ or C#.