Python Flashcards
Εστω array A[3,5,7,8,9]. Ποιά η τιμή του A[-2]?
8
for i in range(10)
is the same as:
for I in range(0,10,1)
?
Yes, default step is 1 and default start is 0. End (10) is non-inclusive
For i in range(0,10,1) is the c equivalent
for(I=0; I<10; I++)
?
yes
Result of this code?
x = 8 def foo(): x = 5 print(x) foo() print(x)
5
8
Result of this code?
x = 8 def foo(): global x x = 5 print(x) foo() print(x)
5
5
what is a package?
a package is a collection of modules which can me imported and used in other python scripts
What’s tha value of __name__?
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 to get the length of a list L?
len(L). len is a function that takes the list as an argument. L.list doesn’t work.
What is a list in python?
Ordered (you can have indices)
Allows duplicates
Is mutable (changeable) e.g. L[2] = 8
What is a set in python?
Unordered(no indices)
No duplicates
Immutable (you can only add or remove elements)
You can iterate with “in”
What is a tuple
Ordered
Immutable
Less memory than list
Faster to loop -> the way to go in data science
Are these two the same?
dict x = {"a":5, "b":6} x.clear()
dict x = {"a":5, "b":6} x = {}
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
Difference between
open()
and
with open()
If you use open() you have to call close().
“with open()” doesn’t require the programmer to close the file. Its done automatically
What is the JSON equivalent of a Python dict?
Object
JSON equivalent of a python list?
Array