programming midterm 2 study guide
how do you reverse user input?
' '.join(reversed(word))
How do you create an empty tuple in Python
() and tuple()
Fill in the Blank: To concatenate two strings a and b, you can use the ___operator.
+
Fill in the Blank: To count the number of occurrences of an element in a tuple, use the method ___().
count
How do you access the value associated with the key 'a' in the dictionary d = {'a': 1, 'b': 2}?
d.get('a') and d['a']
What will be the output of the following code? s = "Hello World" print(s[1:5])
ello
are tuples mutable or immutable?
immutable
Which of the following methods is used to find the index of a substring in a string?
index() and find()
Fill in the Blank: To add a new key-value pair to a dictionary, use the syntax dictionary[___] = value.
key
The method to convert a string to all lowercase in Python is string.___()
lower
Does a set guarantee the order of items as they are added? Specifically, when using a loop to print the items, will they be returned in the same order in which they were added?
no
can sets contain duplicate elements?
no
how to make all spaces in a sentence into an _
output = text.replace(" ", "_")
Which of the following operations will add an element to the set s?
s.add( )
how to count vowels from user input?
sum(1 for letter in word if letter in vowels)
Which is a valid way to access the second element of a tuple t?
t[1]
.isalpha() method is used to check weather all the characters in a given strings are alphabets
true
.isdigit() method is used to check weather all the characters in a given strings are numerical digits in range 0 to 9
true
The keys in a dictionary can be accessed using .values() method.
true
The keys in the dictionary are unique
true
The string method strip() removes all whitespace from both ends of a string.
true
endswith( suffix ) returns whether (True or False) the string ends with the given string suffix
true
he keys in a dictionary can be accessed using .keys() method.
true
join( sequence ) joins the sequence of values into a single string value with the "called on" string as the separator
true
startswith( prefix ) return whether (True or False) the string starts with the given prefix string
true
What is the output of the following code snippet? A = {1, 2, 3} B = {3, 4, 5} print( A.union(B))
{1, 2, 3, 4, 5}
what will be the output of the following code? s = {1, 2, 3} s.add(2) print(s)
{1, 2, 3}