Python Flashcards
What is the output of this:
def a_function(num_1=5, num_2=2):
return num_1 + num_2
print(a_function())
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
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.
for i in range(3, 10):
print(i)
3
4
5
6
7
8
9
alist = [‘orange’, ‘apple’, ‘pear’, ‘banana’, ‘pickle’, ‘cactus’]
Insert ‘cookie’ as the second element in the list
alist(1,’cookie’)
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.
del fruit[‘watermelon’]
fruit.pop(‘watermelon’)
alist = [‘orange’, ‘apple’, ‘pear’, ‘banana’, ‘pickle’, ‘cactus’]
Delete anything that starts with ‘a’, ‘b’, or ‘c’
alist = [‘orange’, ‘apple’, ‘pear’, ‘banana’, ‘pickle’, ‘cactus’]
Find the index of ‘pear’
s = ‘Hello’
What is the output of s[0:2]?
‘He’
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’
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
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))
Output is 19
Since num_1 is not specified it defaults to the value of 5.
Write a function that accepts a first and last name and does the following:
first = ‘Waldo’
last = ‘Whereami’
Output: ‘Hello Waldo Whereami!!!!’
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.
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.
s = ‘Hello’
What is the output of s[1:4]?
‘ell’
This is a super encoded encrypted secret message. Your job is to break it.
super_secret_message = “!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI”
super_secret_message[::-2]
alist = [66,123,1,55,14]
Print in ascending order
sorted(alist)
or
alist.sort()
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.
1
5
alist = [‘orange’, ‘apple’, ‘pear’, ‘banana’, ‘pickle’, ‘cactus’]
Insert ‘imhere’ before any terms that start with ‘a’, ‘b’, or ‘o’
output is 100
What is the output of the following:
string = ‘apple, pear, strawberry, pineapple’
print(string.split(‘,’))
[‘apple’, ‘pear’, ‘strawberry’, ‘pineapple’]
s = ‘Hello’
What is the output of s[:2]?
‘He’
What is the output of the following:
string = ‘this is a string’
print(string.split())
[‘this’, ‘is’, ‘a’, ‘string’]
Split a string based on whitespace.
string.split()
split a string based on tabs
string.split(‘\t’)
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’ .
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’)
s = ‘Hello’
What is the output of s[:-3]?
‘He’
Write a function that will multiple a list of numbers together.
input: [1, 2, 3, 4]
output: 24
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.
something = {}
print(type(something))
dict
s = ‘Hello’
What is the output of s[2:]?
‘llo’
False
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.
13 % 4
1
alist = [5,66,33,77,88,33,12]
What is alist[3]?
77
Print all numbers between 6 and 20 that are even
for i in range(6, 21, 2):
print(i)
alist = [1,2,3,4,5]
Check if this list is empty.
do_something
if len(alist):
if len(alist) > 0:
Generally, I do the first one
Error. Variable a is not defined
How do you read in a text file?
f=open(‘file.txt’, ‘r’)
contents = f.read()
How do you accept keyboard input from the user?
some_value = input(‘Please enter something’)
Check if a string ends in ‘ing’ or ‘ed’
How do you append to a existing file?
f = open(‘file.txt’, ‘a+’)
f. write(‘Appending a line to the file\n’)
f. close()
Write a function to find the average of a list of numbers.
for i in range(5):
print(i)
0
1
2
3
4
5
How can you get the last three characters of a string?
string[-3:]
split a string based on commas
string.split(‘,’)
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
alist = [5,66,33,77,88,33,12]
What is alist[2:4]?
[33, 77]
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()
The output is 10.
Write a function that accepts a string and a substring. The function should return how many times the substring occurs in the string.
alist = [‘orange’, ‘apple’, ‘pear’, ‘banana’, ‘pickle’, ‘cactus’]
Create a new list anything that starts with ‘a’, ‘b’, or ‘c’
1
3
5
15
77
alist = [5,66,33,77,88,33,12]
What is alist[-1]?
12
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.
Write a function that extracts the middle three characters of a string and returns it.
alist = [66,123,1,55,14]
Print in descending order
sorted(alist, reverse=True)
alist.sort(reverse=True)
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.
for i in range(3, 20, 3):
print(i)
3
6
9
12
15
18
stuff = {‘car’:’vehicle’, ‘apple’:’fruit’, ‘cat’:’meme’}
Convert this to a list.
stuff = list(stuff.items())
a=3
b=a
What is happening?
a=3
a=’spam’
What happens to 3?
3 is garbage collected as nothing is pointing to it.
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.
Show three ways of checking for type
if type(L) == type([])
if type(L) == list
if isinstance(L, list)
Read a file line by line
for line in open(‘data.txt’):
print(line)
Read in an entire file
f = open(‘data.txt’)
text = f.read()
How do you write to a file?
f = open(‘data.txt’, ‘w’)
f. write(‘Hello\n’)
f. close()
Why use a tuple?
The tuple is immutable. Thus, it can’t be changed.
Reverse the following:
M = [‘bb’, ‘aa’, ‘cc’]
M.reverse()
Sort the following:
M = [‘bb’, ‘aa’, ‘cc’]
M.sort()
L = [123, ‘spam’, 1.23, ‘NI’]
Remove the second element
L.pop(2)
del L[2]
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.
s=’spam’
Find the index of ‘pa’
s.find(‘pa’)
s=’spam’
What is the output of s[0]=’z’
TypeError: ‘str’ object does not support item assignment.
In Python, strings are ___________________________.
immutable
s = ‘Spam’
s + ‘xyz’
print(s)
‘Spam’