Python Flashcards

1
Q

Εστω array A[3,5,7,8,9]. Ποιά η τιμή του A[-2]?

A

8

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

for i in range(10)
is the same as:
for I in range(0,10,1)
?

A

Yes, default step is 1 and default start is 0. End (10) is non-inclusive

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

For i in range(0,10,1) is the c equivalent
for(I=0; I<10; I++)
?

A

yes

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

Result of this code?

x = 8

def foo():
    x = 5
    print(x)

foo()
print(x)
A

5
8

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

Result of this code?

x = 8
def foo():
    global x
    x = 5
    print(x)

foo()
print(x)
A

5
5

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

what is a package?

A

a package is a collection of modules which can me imported and used in other python scripts

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

What’s tha value of __name__?

A

It depends:
If you run the script the value of __name__ is main.
If you don’t run it then it is the name of the script

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

How to get the length of a list L?

A

len(L). len is a function that takes the list as an argument. L.list doesn’t work.

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

What is a list in python?

A

Ordered (you can have indices)
Allows duplicates
Is mutable (changeable) e.g. L[2] = 8

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

What is a set in python?

A

Unordered(no indices)
No duplicates
Immutable (you can only add or remove elements)
You can iterate with “in”

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

What is a tuple

A

Ordered
Immutable
Less memory than list
Faster to loop -> the way to go in data science

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

Are these two the same?

dict x = {"a":5, "b":6}
x.clear()
dict x = {"a":5, "b":6}
x = {}
A

No, clear empties the dictionary so all references are affected.
x = {} will create a new empty dictionary and assign it to x variable but the previous dictionary remains unaffected

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

Difference between
open()
and
with open()

A

If you use open() you have to call close().
“with open()” doesn’t require the programmer to close the file. Its done automatically

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

What is the JSON equivalent of a Python dict?

A

Object

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

JSON equivalent of a python list?

A

Array

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

JSON equivalent of a python tuple?

A

Array

17
Q

JSON equivalent of a python str?

A

String

18
Q

JSON equivalent of a python int, float?

A

Number

19
Q

JSON equivalent of python None?

A

null

20
Q

How are class methods defined?

A

A method without “self” as parameter is a class method that can be call like that:
Classname.method().

If an instance tries to call this method exception is raised.

if the method is declared like
@staticmethod
def foo(x) #no self param

then both class and instances can call this method.

Methods with self param are instance methods and can be called Object.method()

21
Q

What is a comprehension?

A

An elegant and more efficient way to create a new list from another list. Conditions may apply.

22
Q

What are generators?

A

Generator expressions can be used to create a new list from an iterable. They are memory efficient because they only calculate the next value when it is requested.

23
Q

Result of 14/8?

A

1.75

24
Q

Result of 14//8?

A

1

25
Q

What are the two types of casting?
Is there any data loss?

A

Implicit: Performed by python interpreter. No data loss
Explicit: Performed by us for example x = (int)1.75 –> x=1. Could have data loss.

26
Q

What are the types of supported comprehensions in python?

A

List, set and dictionary