ML / Python Flashcards
Create array from array without reference [python]
a = b[:]
how to declare a list and map? [python]
mp = {} lst = []
How to check if the element is int or array? [python]
for v in data_list: if isinstance(v, int):
Implement decorator which returns function signature and its return value
def debug(func): """ :param func: function """ def wrapper(*args, **kwargs): output = func(*args, **kwargs) return f'{func.\_\_name\_\_}{args} was called and returned {output}' return wrapper
@debug def add(a, b):
How to remove the element from the list with the index? [python]
del lst[i]
how to inherit class in Python?
class Shuttle(Rocket):
how to call super from child class?
super().__init__(name, mission)
What Is Python?
Interpreted high-level object-oriented dynamically-typed scripting language.
python standard data types
numbers, strings, sets, lists, tuples, and dictionaries
Can we update string in Python?
Strings are immutable. Once they are created, they cannot be changed e.g.
a = ‘me’
Updating it will fail:
a[1]=’y’
What if we create variable in for-loop or if ??? [python]
if - we can use it after if
for-loop - we can’t use it
if is_python_awesome:
test_scope = “Python is awesome”
print(test_scope) it is ok
how to use global variable in Python?
TestMode = True def some_function(): global TestMode TestMode = False some_function() print(TestMode)
how to get type of variable in Python?
type(‘farhad’)
–> Returns
what is ‘divmod’ in python?
print(divmod(10,3)) #it will print 3 and 1 as 3*3 = 9 +1 = 10
repeat string in python
‘A’*3 will repeat A three times: AAA
how to reverse string in python?
x = 'abc' x = x[::-1]
how to find index of second ‘a’ in a string? [python]
name = 'farhad' index = name.find('a', 2) # finds index of second a
Regex uses in python for working with strings
split(): splits a string into a list via regex
sub(): replaces matched string via regex
subn(): replaces matched string via regex and returns number of replacements
how to cast variable to type in python?
str(x): To string
int(x): To integer
float(x): To floats
how to remove element from the set in Python?
set. remove(item) — removes item from the set and raises error if it is not present
set. discard(item) — removes item from the set if it is present
how to get any element from the set in python?
set.pop() — returns any item from the set, raises KeyError if the set is empty
operations with sets in python?
a = {1,2,3} b = {3,4,5} c = a.intersection(b) c = a.difference(b) c = a.union(b)
what is pickling in python?
Converting an object into a string and dumping the string into a binary file is known as pickling. The reverse is known as unpickling.
If your function can take in any number of arguments - what to do in python?
then add a * in front of the parameter name: def myfunc(*arguments): return a