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