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
Q

Split a string based on whitespace.

A

string.split()

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

split a string based on tabs

A

string.split(‘\t’)

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

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’ .

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

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

Delete ‘banana’

A

indx = alist.index(‘banana’)

del alist[indx]

Also

indx = alist.index(‘banana’)

alist.pop(indx)

Also

alist.remove(‘banana’)

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

s = ‘Hello’

What is the output of s[:-3]?

A

‘He’

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

Write a function that will multiple a list of numbers together.

input: [1, 2, 3, 4]
output: 24

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
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.

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

something = {}

print(type(something))

A

dict

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

s = ‘Hello’

What is the output of s[2:]?

A

‘llo’

34
Q
A

False

35
Q

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.

A
36
Q

13 % 4

A

1

37
Q

alist = [5,66,33,77,88,33,12]

What is alist[3]?

A

77

38
Q

Print all numbers between 6 and 20 that are even

A

for i in range(6, 21, 2):

print(i)

39
Q

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

Check if this list is empty.

A

do_something

if len(alist):

if len(alist) > 0:

Generally, I do the first one

40
Q
A

Error. Variable a is not defined

41
Q

How do you read in a text file?

A

f=open(‘file.txt’, ‘r’)

contents = f.read()

42
Q

How do you accept keyboard input from the user?

A

some_value = input(‘Please enter something’)

43
Q

Check if a string ends in ‘ing’ or ‘ed’

A
44
Q

How do you append to a existing file?

A

f = open(‘file.txt’, ‘a+’)

f. write(‘Appending a line to the file\n’)
f. close()

45
Q

Write a function to find the average of a list of numbers.

A
46
Q

for i in range(5):

print(i)

A

0

1

2

3

4

5

47
Q

How can you get the last three characters of a string?

A

string[-3:]

48
Q

split a string based on commas

A

string.split(‘,’)

49
Q

Find the sum of [1,2,3,4,5]

A

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
Q

alist = [5,66,33,77,88,33,12]

What is alist[2:4]?

A

[33, 77]

51
Q

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’)]

A

fruit = fruit.items()

52
Q
A

The output is 10.

53
Q

Write a function that accepts a string and a substring. The function should return how many times the substring occurs in the string.

A
54
Q

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

Create a new list anything that starts with ‘a’, ‘b’, or ‘c’

A
55
Q
A

1

3

5

15

77

56
Q

alist = [5,66,33,77,88,33,12]

What is alist[-1]?

A

12

57
Q

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.

A
58
Q

Write a function that extracts the middle three characters of a string and returns it.

A
59
Q

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

Print in descending order

A

sorted(alist, reverse=True)

alist.sort(reverse=True)

60
Q

fruit = {}

fruit[‘lemon’] = ‘sour’

fruit[‘apple’] = ‘sweet’

fruit[‘banana’] = ‘sweet’

fruit[‘lime’] = ‘sour’

What is the difference between fruit[‘apricot’] and fruit.get(‘apricot’) ?

A

‘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
Q

for i in range(3, 20, 3):

print(i)

A

3

6

9

12

15

18

62
Q

stuff = {‘car’:’vehicle’, ‘apple’:’fruit’, ‘cat’:’meme’}

Convert this to a list.

A

stuff = list(stuff.items())

63
Q

a=3

b=a

What is happening?

A
64
Q

a=3

a=’spam’

What happens to 3?

A

3 is garbage collected as nothing is pointing to it.

65
Q

a = 3

What is the type of a?

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
Q

Show three ways of checking for type

A

if type(L) == type([])

if type(L) == list

if isinstance(L, list)

67
Q

Read a file line by line

A

for line in open(‘data.txt’):

print(line)

68
Q

Read in an entire file

A

f = open(‘data.txt’)

text = f.read()

69
Q

How do you write to a file?

A

f = open(‘data.txt’, ‘w’)

f. write(‘Hello\n’)
f. close()

70
Q

Why use a tuple?

A

The tuple is immutable. Thus, it can’t be changed.

71
Q

Reverse the following:

M = [‘bb’, ‘aa’, ‘cc’]

A

M.reverse()

72
Q

Sort the following:

M = [‘bb’, ‘aa’, ‘cc’]

A

M.sort()

73
Q

L = [123, ‘spam’, 1.23, ‘NI’]

Remove the second element

A

L.pop(2)

del L[2]

74
Q

What is the output of:

s=’spam’

s.replace(‘pa’, ‘xyz’)

print(s)

A

‘spam’

Notice that you did not set s to equal anything. Strings are immutable so nothing has changed.

75
Q

s=’spam’

Find the index of ‘pa’

A

s.find(‘pa’)

76
Q

s=’spam’

What is the output of s[0]=’z’

A

TypeError: ‘str’ object does not support item assignment.

77
Q
A
78
Q

In Python, strings are ___________________________.

A

immutable

79
Q

s = ‘Spam’

s + ‘xyz’

print(s)

A

‘Spam’

80
Q
A
81
Q
A