Python Flashcards

Memorise Python Syntax

1
Q

How would you define a function in Python?

A
def functionName(args...):
      return value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the equivalent of a javascript array in python?

A

It’s a list.

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

How do you define a list in Python?

A

listName = []

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

How do you initialize a list with 11 slots in python?

A

list = [None] * 11

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

Are python lists typed?

A

No.

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

How do you define a class in python?

A
class MyCustomHashTable:
    def \_\_init\_\_(self, length):
        self.table = ['None'] * length
def findItemHashPosition(self, item):
    return remainder(item, self.length()) ~~~ ~~~
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you convert a string to integer in python?

A
(int('1') == 1) == True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you convert an integer to string in python?

A
('{}'.format(1) == '1') == True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you define a calculated property in Python?

A

You can define calculated properties in python by using the @property decorator. The @property decorator acts as a getter and enables retrieving its result by simply accessing its name like a property. ` MyCustomHashTable(11).tableSize `

class MyCustomHashTable:
def \_\_init\_\_(self, length):
    self.table = ['None'] * length ~~~
    @property
    def tableSize(self):
        return len(self.table)

print(MyCustomHashTable(11).tableSize) // prints 11
~~~

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

How do you define infinity in Python?

A
print(float("inf") > 9999 // prints True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you check if a value is a bool in Python?

A

There are three ways to check type equality in Python.

print(type(True) == bool)
print(isinstance(True, bool))
print(issubclass(True, bool))

A few more things to consider.

isinstance

Use this built-in function to find out if a given object is an instance of a certain class or any of its subclasses. You can even pass a tuple of classes to be checked for the object. The only gotcha you will discover is this: in Python a bool object is an instance of int! Yes, not kidding!

issubclass

This built-in function is similar to isinstance, but to check if a type is an instance of a class or any of its subclasses. Again, the only gotcha is that bool type is subclass of int type.

type

If a single argument (object) is passed to type() built-in, it returns type of the given object. If three arguments (name, bases and dict) are passed, it returns a new type object.

A few more things to consider…

>>> isinstance(True, int)
True
>>> isinstance(True, float)
False
>>> isinstance(3, int)
True
>>> isinstance([1,2,3], list)
True
>>> isinstance("aa", str)
True
>>> isinstance(u"as", unicode)
True
>>> issubclass(bool, int)
True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you iterate through the characters of a string in Python?

A

A string in python is iterable and has native support.

for char in "string":
  print char

That prints

s
t
r
i
n
g
How well did you know this?
1
Not at all
2
3
4
5
Perfectly