Objects and Data Structure Assessment Test Flashcards
Numbers
There are various number type in Python e.g integers which are just whole numbers like 1 or 2, and floating point numbers which have a decimal point 2.4 or 1.0. Exponentials like squares and square roots come under floating point numbers
Strings
Strings are a record of text information and are constructed using double or single quotes
Lists
lists are a collection of items constructed using brackets [] and commas that seperate every element.
Tuples
Tuples are like lists however, unlike lists tuples are immutable meaning they cannot be added to or changed. Tuples can be used for day or months on a calendar
Write an equation that uses multiplication, division, an exponent, addition, and subtraction that is equal to 100.25
100.25 = 1.25 + (5**2*8/2) - 1
What is the value of the expression 4 * (6 + 5)
44
What is the value of the expression 4 * 6 + 5
29
What is the value of the expression 4 + 6 * 5
34
What is the type of the result of an expression 3 + 1.5 + 4?
Floating point number
What would you use to find a number’s square root, as well as it’s square?
Square root: 100 ** 0.5 = 10.0
Square: 10 ** 2 = 100
s = ‘hello’
# Print out the e string using indexing
s[1]
‘e’
s = ‘hello’
# Reverse the string using slicing
s[::-1]
s = ‘hello’
# Print out the ‘o’
Method 1:
Method 2:
Method 1: s[4]
Method 2: s[-1]
Build this list [0,0,0] two seperate ways.
Method 1:
Method 2:
Method 1: [0]*3
Method 2:
list1 = [0,0,0]
list1
Reassign ‘hello’ in this nested list to say ‘goodbye’ instead.
list3 = [1,2,[3,4,’hello’]]
list3[2][2] = ‘goodbye’
list3 = [1,2,[3,4,’hello’]]
Sort the list in two methods:
list4 = [5,3,4,6,1]
Method 1: .sorted(list4)
Method 2:
list4.sort()
list4
- Grab ‘hello’ using indexing and slicing.
d = {‘simple_key’ : ‘hello’}
d[‘simple_key]
- Grab ‘hello’ using indexing and slicing.
d = {‘k1: [1,2,{‘k2’ :[‘this is tricky’,{‘tough’ :[1,2,[‘hello’]]}]}]}
d’[k1’][2][‘k2’][-1][‘tough’][2]
- Grab ‘hello’ using indexing and slicing.
d = {‘k1’: {‘k2’ : ‘hello’}}
d[‘k1’][‘k2’]
- d = {‘k1’ :[{‘nest_key’ :[‘this is deep’, [‘hello’]]}]}
d[‘k1’][0][‘nest_key’][-1]
Can you sort a dictionary? Why or why not?
No, because normal dictionaries are mappings not a sequence
How do you create a tuple?
t = (1,2,3)
What is the major difference between tuples and lists
Tuples, unlike lists are immutable which means they cannot be added to or changed
What’s unique about a set?
Sets are unique because they don’t allow for duplicate items
Use a set to find the unique values of the list below:
list5 = [1,2,2,33,4,4,11,22,3,3,2]
set(list5)
2 > 3
False
3 <= 2
False
3 == 2.0
False
3.0 == 3
True
4**0.5 !=2
False
l_one = [1,2,[3,4]]
l_two = [1,2,{‘k1’ :4}]
-
True or False?
l_one[2][0] >= l_two[2][‘k1’]
False