Python Flashcards

1
Q

What is the output of this:

def a_function(num_1=5, num_2=2):

return num_1 + num_2

print(a_function())

A

Output is 7

The reason is that a_function(num_1=5, num_2=2) has default values defined. If no arguments are passed it will default to 5+2

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

Write a function called opposite. It accepts a string and returns a new string where all of the upper case characters are lower and all the lower case characters are capitalized.

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

for i in range(3, 10):

print(i)

A

3

4

5

6

7

8

9

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

alist = [‘orange’, ‘apple’, ‘pear’, ‘banana’, ‘pickle’, ‘cactus’]

Insert ‘cookie’ as the second element in the list

A

alist(1,’cookie’)

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

fruit = {}

fruit[‘lemon’] = ‘sour’

fruit[‘apple’] = ‘sweet’

fruit[‘banana’] = ‘sweet’

fruit[‘lime’] = ‘sour’

fruit[‘watermelon’] = ‘sour’

Oh no! Someone made a mistake with watermelon or has been eating strange watermelon. Delete this entry.

A

del fruit[‘watermelon’]

fruit.pop(‘watermelon’)

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

alist = [‘orange’, ‘apple’, ‘pear’, ‘banana’, ‘pickle’, ‘cactus’]

Delete anything that starts with ‘a’, ‘b’, or ‘c’

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

alist = [‘orange’, ‘apple’, ‘pear’, ‘banana’, ‘pickle’, ‘cactus’]

Find the index of ‘pear’

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

s = ‘Hello’

What is the output of s[0:2]?

A

‘He’

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

fruit = {}

fruit[‘lemon’] = ‘sweet’

fruit[‘apple’] = ‘sour’

fruit[‘banana’] = ‘sour’

fruit[‘lime’] = ‘sweet’

fruit[‘watermelon’] = ‘sour’

Somebody messed this up….. fix this using the following steps. If it currently has ‘sweet’ change it to ‘sour’. If it currently has ‘sour’ change it to ‘sweet’

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

Write a function that accepts two strings and checks if either of the strings are contained within the other.

string_1: cat string_2: category output:True

string_1 bat string_2: robin output: False

string_1: batman string_2 output: True

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

What is the output of this:

def a_function(num_1=5, num_2=2):

return num_1 + num_2

print(a_function(num_2=14))

A

Output is 19

Since num_1 is not specified it defaults to the value of 5.

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

Write a function that accepts a first and last name and does the following:

first = ‘Waldo’

last = ‘Whereami’

Output: ‘Hello Waldo Whereami!!!!’

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

Write a function called get_index which will accept a string and return a list that contains the index for each capital letter in the string.

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

Write a function called fizz_buzz_strings. It accepts a string and if the length of the string is divisible by 3 it prints ‘fizz’ and if it is divisible by 5 it prints ‘buzz’. If it is divisible by both it prints ‘fizz buzz’. If none of these conditions are satisfied it does not print out anything.

Do not use elif statements.

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

s = ‘Hello’

What is the output of s[1:4]?

A

‘ell’

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

This is a super encoded encrypted secret message. Your job is to break it.

super_secret_message = “!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI”

A

super_secret_message[::-2]

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

alist = [66,123,1,55,14]

Print in ascending order

A

sorted(alist)

or

alist.sort()

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

fruit = {}

fruit[‘lemon’] = ‘sweet’

fruit[‘apple’] = ‘sour’

fruit[‘banana’] = ‘sour’

fruit[‘lime’] = ‘sweet’

fruit[‘watermelon’] = ‘sour’

Somebody really messed this up. Best thing to do is start over. Erase everything.

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

1

5

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

alist = [‘orange’, ‘apple’, ‘pear’, ‘banana’, ‘pickle’, ‘cactus’]

Insert ‘imhere’ before any terms that start with ‘a’, ‘b’, or ‘o’

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

output is 100

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

What is the output of the following:

string = ‘apple, pear, strawberry, pineapple’

print(string.split(‘,’))

A

[‘apple’, ‘pear’, ‘strawberry’, ‘pineapple’]

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

s = ‘Hello’

What is the output of s[:2]?

A

‘He’

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

What is the output of the following:

string = ‘this is a string’

print(string.split())

A

[‘this’, ‘is’, ‘a’, ‘string’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Split a string based on whitespace.
string.split()
26
split a string based on tabs
string.split('\t')
27
fruit = {} fruit['lemon'] = 'sour' fruit['apple'] = 'sweet' fruit['banana'] = 'sweet' fruit['lime'] = 'sour' Write a function called get\_taste that accepts the name of a fruit. If the fruit is in the dictionary return the taste. If the fruit is not in the dictionary return 'https://gph.is/281rBnj' .
28
alist = ['orange', 'apple', 'pear', 'banana', 'pickle', 'cactus'] Delete 'banana'
indx = alist.index('banana') del alist[indx] Also indx = alist.index('banana') alist.pop(indx) Also alist.remove('banana')
29
s = 'Hello' What is the output of s[:-3]?
'He'
30
Write a function that will multiple a list of numbers together. input: [1, 2, 3, 4] output: 24
31
Write a function called fizz\_buzz\_strings. It accepts a string and if the length of the string is divisible by 3 it prints 'fizz' and if it is divisible by 5 it prints 'buzz'. If it is divisible by both it prints 'fizz buzz'. If none of these conditions are satisfied it does not print out anything.
32
something = {} print(type(something))
dict
33
s = 'Hello' What is the output of s[2:]?
'llo'
34
False
35
Write a function called guesser. This function will ask the user to guess a number between 1 and 100. Continue looping until the number is guessed. Use a while loop.
36
13 % 4
1
37
alist = [5,66,33,77,88,33,12] What is alist[3]?
77
38
Print all numbers between 6 and 20 that are even
for i in range(6, 21, 2): print(i)
39
alist = [1,2,3,4,5] Check if this list is empty.
if len(alist): #do\_something if len(alist) \> 0: #do\_something Generally, I do the first one
40
Error. Variable a is not defined
41
How do you read in a text file?
f=open('file.txt', 'r') contents = f.read()
42
How do you accept keyboard input from the user?
some\_value = input('Please enter something')
43
Check if a string ends in 'ing' or 'ed'
44
How do you append to a existing file?
f = open('file.txt', 'a+') f. write('Appending a line to the file\n') f. close()
45
Write a function to find the average of a list of numbers.
46
for i in range(5): print(i)
0 1 2 3 4 5
47
How can you get the last three characters of a string?
string[-3:]
48
split a string based on commas
string.split(',')
49
Find the sum of [1,2,3,4,5]
There are multiple solutions. The one below is the preferred: alist = [1,2,3,4,5] sum(alist) You could also do: tots = 0 for item in alist: tots+=item
50
alist = [5,66,33,77,88,33,12] What is alist[2:4]?
[33, 77]
51
Today you just really don't feel like working with dictionaries and would rather just have key-value pairs of what is in the dictionary. Convert the following dictionary fruit = {} fruit['lemon'] = 'sour' fruit['apple'] = 'sweet' fruit['banana'] = 'sweet' fruit['lime'] = 'sour' fruit['watermelon'] = 'sweet' to fruit = [('lemon','sour'), ('apple','sweet'), ('banana','sweet'), ('lime','sour'), ('watermelon','sweet')]
fruit = fruit.items()
52
The output is 10.
53
Write a function that accepts a string and a substring. The function should return how many times the substring occurs in the string.
54
alist = ['orange', 'apple', 'pear', 'banana', 'pickle', 'cactus'] Create a new list anything that starts with 'a', 'b', or 'c'
55
1 3 5 15 77
56
alist = [5,66,33,77,88,33,12] What is alist[-1]?
12
57
a\_dict = {} a\_list = ['mario', 'zelda', 'sonic', 'megaman', 'double\_dragon'] Insert each element of the list into the dictionary as a key. The value for the key should be the position of the element in the array. For example, 'mario' should have value 1 'zelda' should have value 2 ..... 'double\_dragon' should have value 5 Note: 'mario' is position 1 vs 0 which is its index in the list.
58
Write a function that extracts the middle three characters of a string and returns it.
59
alist = [66,123,1,55,14] Print in descending order
sorted(alist, reverse=True) alist.sort(reverse=True)
60
fruit = {} fruit['lemon'] = 'sour' fruit['apple'] = 'sweet' fruit['banana'] = 'sweet' fruit['lime'] = 'sour' What is the difference between fruit['apricot'] and fruit.get('apricot') ?
'apricot' is not in your dictionary. fruit['apricot'] will throw an error. fruit.get('apricot') will return None and will not throw and error.
61
for i in range(3, 20, 3): print(i)
3 6 9 12 15 18
62
stuff = {'car':'vehicle', 'apple':'fruit', 'cat':'meme'} Convert this to a list.
stuff = list(stuff.items())
63
a=3 b=a What is happening?
64
a=3 a='spam' What happens to 3?
3 is garbage collected as nothing is pointing to it.
65
a = 3 What is the type of a?
A is a name. It is a reference to an object. It has no type. It is a pointer. All we can ever say about a variable in Python is that it references a particular object at a particular point in time.
66
Show three ways of checking for type
if type(L) == type([]) if type(L) == list if isinstance(L, list)
67
Read a file line by line
for line in open('data.txt'): print(line)
68
Read in an entire file
f = open('data.txt') text = f.read()
69
How do you write to a file?
f = open('data.txt', 'w') f. write('Hello\n') f. close()
70
Why use a tuple?
The tuple is immutable. Thus, it can't be changed.
71
Reverse the following: M = ['bb', 'aa', 'cc']
M.reverse()
72
Sort the following: M = ['bb', 'aa', 'cc']
M.sort()
73
L = [123, 'spam', 1.23, 'NI'] Remove the second element
L.pop(2) del L[2]
74
What is the output of: s='spam' s.replace('pa', 'xyz') print(s)
'spam' Notice that you did not set s to equal anything. Strings are immutable so nothing has changed.
75
s='spam' Find the index of 'pa'
s.find('pa')
76
s='spam' What is the output of s[0]='z'
TypeError: 'str' object does not support item assignment.
77
78
In Python, strings are \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.
immutable
79
s = 'Spam' s + 'xyz' print(s)
'Spam'
80
81