Python Flashcards
numbers without fractions are integers.
division in python always returns a float.
How can we make it to return an integer?
how to we use exponent?
- floor division: “//”*
- exponent: “**”*
Strings
- how to tell the interpreter to view “" as a regular key? For example, we want to print ‘C:\folder’.
- How to tell the interpreter to automatically include end of lines in the string?
- How can strings be concatenated?
- How can strings be multiplied
- Useful - a string can be concatenated just by being one beside the other, when is it partically useful?
- String can be indexed, we can also start counting from the end! how?
- we use ‘r’ before the string: print(r’C:\folder’)
- use “”“<content>""" or '''<content>''' and use '\' if end of line is nor required</content></content>
- Concatenation using “+”
- Multiplication using *
- It is very useful when we assign a long string to a variable, and we want to have breaks so the code won’t look sloppy.
- str[-1] = last char.
String
- we can split a string to start and end. how?
- python strings are immutable. what happens when we do str[2] = ‘a’ or str[0:2] = “hi”
- we can use str[start:end], when end is not included. That is: str[2:5] is index 2 to index 4 and str [:2] + str [2:] = str
- we get an error, ‘object does not support item assignment
Lists
describe the new things
- we can index starting at the end, arr[-1]
- we can split using arr[3:]
- we can concat using arr + [1 , 2 , 3 ,4]
- using append we can add a new element at the end of the list using append method, without creating a new list
- we can clean a list using arr[:} = [] or remove desired part of it using arr[start:end] = []
- we can have nested lists: arr = [arr1,arr2]
What’s good about assigning values in python?
we can assign values this way:
- a,b = 0,1
An we can even have swaps!
- a,b = b,a+b
- describe its functionality
- We use ‘,’ to distinguish between items and the functions automatically inserts space between them
- Automatically it ends with a new line
- The keyword argument end= suggest a different end, it might be end=”,” to have a comma between items.
Control flow
- What’s the substiture to else if in python?
- What’s special about for loop in python?
- How can we save the current index in the sequence?
- What’s the functionality of in range
- How can we create the list [2,4,6,8,10]
- How can we sum a list?
- Explain else in a for/while loop.
- When does the pass keyword is used?
- what is the functionality of the zip keyword?
- What is the use of a string right at the beginning of the body of the function?
- elif, and it functions as switch and cases
- It iterates over the items of any sequence(a list or string)
-
using enumerate -
- for index, item in enumerate(list):
- range can have three arguments(start,end,step)
- list(range(2,10,2)) - NOTE! the number 10 is not included
- Similiarly we can ceate a set
- sum(range(4))
- With for loop: else is executed when the loop ends through exhaustion of the variable. With while loop: else is executed when the condition is false. Usually it comes with a break, so that we won’t get to else. similiar to how it is with try.
- It is used to fill a desired emptiness in the body of the code. For instance, creating minimal classes, that has no properties. But the main use it as a body-to-be-filled when some function needs to be implemented
- It is used to iterate over multiple lists. Note: it stops with the shortest list.
- ””“<content>""" is used for documentation</content>
arguments and functions
- How are arguments passed in python?
- The function name holds the reference to the function’s body. How assignment occurs?
- what’s the return value of void functions.
- How are default arguments created?
- What’s special about default arguments?
- How can we have defualt argument not static?
- Arguments are passed by reference. Like in Java.
- As with all variables; newFun = fun
- print(voidFunc()) - None
- func(a, b=4, c = “hello boy”)
- They are static! They are created just once
- Using None:
- def f(a, L=None): if L is None: L = []
keyword arguments
- Explain these
- What’s their functionality?
- When can we use them?
- They have the form keyword:value
- Assume we have default arguments, and we want to change only the fourth argument, then we can change it by stating its name, and not its position.
- non-keyword argument can not appear after keyword argument
*args and **kwargs
explain
only-type-parameters:
- How to declare positional only parameters?
- How to declare keyword only parameters?
- How to declare both?
unpacking arguments:
- what is it?
- How is it done with positional parameters?
- How is it done with keyword parameters?
only-type-parameters:
- *args - saves all positional arguments in a list.*
- **kwargs - saves, as a dictionary, all keyword arguments.*
NOTE: if normal arguments are placed before *args and **kwargs, the former will occupy them.
unpacking arguments:
- unpacking a list/dictionary into its elements
- it is done using * and ** before the parameter.
- Example: arg = [3,6]
- arr = list(range(*arg)) = [3,4,5]
Lambda
- How many expression can lambda have?
- Describe its syntax
- Can a function return a closure?
Lambda
- it can have only one expression
- lambda <arguments> : <expression></expression></arguments>
- Yes. The function returns a closure
Documentation Strings
Describe
- First line should always be short and concise
- First line should start with a capital letter and end with a period
-
The second line should be empty, if there are more than one line
- The following lines should describe the object’s calling conventions, its side-effects, return value,parameters etc.
In python what is the fast and what is slow:
- add and remove from the end of the list or
- add and remove from the beginnng?
What should we use then we we need the functionality of a queue?
- It’s a lot more expensive to add/remove from the beginning of the list*
- We should use Collections.deque. deque is a combination of queue and stack, we can add/remove both from the beginning and from the end at a constant time.
Are tuples mutable or immutable?
They are immutable
- How a Set is defined in Python?*
- (especially an empty set)*
- How to search within a set?*
empty set - set()
- non-empty-set* - {A,B,C}
- using <element><strong><em>> in <</em></strong><em>set</em><strong><em>></em></strong></element>*