Python Flashcards

1
Q

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?

A
  • floor division: “//”*
  • exponent: “**”*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

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

Lists

describe the new things

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

What’s good about assigning values in python?

A

we can assign values this way:

  • a,b = 0,1

An we can even have swaps!

  • a,b = b,a+b
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

print

  • describe its functionality
A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

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?
A
  • 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 = []
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

keyword arguments

  • Explain these
  • What’s their functionality?
  • When can we use them?
A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

*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?
A

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

Lambda

  • How many expression can lambda have?
  • Describe its syntax
  • Can a function return a closure?
A

Lambda

  • it can have only one expression
  • lambda <arguments> : <expression></expression></arguments>
  • Yes. The function returns a closure
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Documentation Strings

Describe

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

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?

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

Are tuples mutable or immutable?

A

They are immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  • How a Set is defined in Python?*
  • (especially an empty set)*
  • How to search within a set?*
A

empty set - set()

  • non-empty-set* - {A,B,C}
  • using <element><strong><em>&gt; in &lt;</em></strong><em>set</em><strong><em>&gt;</em></strong></element>*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Dictionaries

  • which types can the ‘key’ hold?
  • what happens when we add an already-exists -key?
  • what happens when we try to extract a value given a non-existing key?
  • What the items() method is for?
A
  • It can only hold immutable objects
    • ​string
    • number
    • tuple** where its elements are also **immutable.
  • It’s value changes to the new one
  • Error
  • For getting both key and value
17
Q

Executable statements within a module.

  • when we use import, when do these statement occur?*
  • Do the symbol table of different modules intermix?*
  • why if _name_ == “main” is necessary?*
A
  • They occur the first time we call import.
  • No, each module has its out symbol table.
  • When a call python file.py <arguments> </arguments>the module is imported where _name_ = “main”. This signifies that the module is being run, and not just being imported. for all other modules which are just being imported, _name_ == <name>, thus the if condition is false and module is not being executed.</name>
18
Q

How can I add a path to python?

A

import sys

sys.path is a string which holds the paths. to add a path we use sys.path.append(‘<path>')</path>

19
Q

What is the functionality of _init_.py

A

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string, unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

20
Q

How can we define which packages are to be imported using .* ?

A

within the _init_.py file, the variable _all_ defined the list of all packages to be imported using *.

21
Q

Describe the namespace of a recursive call?

A

Each recursive invocation has its own local namespace, and all are deleted once the function returns