Python Crash Course Flashcards

1
Q

What restrictions are there on Python variable names?

A

They can’t start with numbers or special symbols.

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

How do you use the .format function for string replacement?

A

‘my number is {} and my name is {}.’.format(num, name)

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

What naming convention do you use for functions and variables?

A

CamelCase and underscore naming convention respectively.

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

How do you avoid having to worry about variable positioning with .format?

A

‘my number is {a} and my name is {b}.’.format(a=num, b=name)

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

What does s[0:] do?

A

It slices the string s from the start ( index 0 ) to the end.

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

What does s[:3] do?

A

It slices the string s from the start to index 2 ( 3-1 )

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

How do you add values to a list?

A

my_list.append(‘d’)

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

How would you access 4 in nest = [1,2,[3,4]] ?

A

nest[2][1]

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

How would you access ‘target in nest = [1,2,[3,4,[‘target’]]] ?

A

nest[2][2][0]

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

What syntax is used for Python dictionaries?

A

d = {‘key1’:’value’, ‘key2’:123}

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

Give an example of a dictionary that contains a list.

A

d = {‘k1’:[1,2,3]}

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

Give an example of a dictionary nested inside a dictionary and access one of its innermost members.

A

d = {‘k1’:{‘innerkey’:[1,2,3]}}

d[‘k1’][‘innerkey’][1]

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

What’s the difference between tuples and lists?

A

Tuples are immutable, their members cannot be changed.

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

What syntax is used to create a set?

A

Braces. The distinguishing element between dictionaries and sets are colons.

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

Can sets contain more than one of the same value?

A

No. Sets by definition can only contain unique elements.

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

Write a for loop that uses the keyword ‘in’

A

seq = [1,2,3]

for item in seq:
print(item)

16
Q

Print the elements of the range 0 through 4

A

for x in range(0,5):
print(x)

17
Q

Create a list of the numbers 0 through 4 using range

A

list(range(5))

18
Q

Simplify the following using list comprehension:

x = [1,2,3,4]
out = []
for num in x:
out.append(num**2)
print(out)

A

x = [1,2,3,4]
out = [num**2 for num in x ]

19
Q

Write a basic function using Python

A

def my_func(param1):
print(param1)

20
Q

Write a function with a default parameter value.

A

def my_func(name=’DEFAULT’):
print(‘Hello ‘ + name)

21
Q

How do you access the DocStrings of a function?

A

Shift + Tab

22
Q

How do you write a Docstring?

A

”””
DOCSTRING
“””

23
Q

Write a map for the following:

def times2(var):
return var*2

seq = [1,2,3,4,5]

A

list(map(times2,seq))

=> [2,4,6,8,10]

24
Q

Write a lambda expression for the following:

def times2(var):
return var*2

seq = [1,2,3,4,5]

A

list(map(lambda num:num*2, seq))

25
Q

Write a filter expression that results in only even numbers for the following:

seq = [1,2,3,4,5]

A

list(filter(lambda num: num%2 == 0, seq))

26
Q

how do you split an email address into its identifier and domain?

A

email.split(‘@’)

27
Q

How do you split a string s into a list of strings by spaces?

28
Q

How do you remove the first element of a list ‘lst’ adn assign it to a variable ‘first’?

A

first = lst.pop()

29
Q

How do you unpack a tuple?

A

for a,b in x:
print(b)