Python test #1 (Objects and Data Structures): Pierian Data

Just completed the first test on a PYTHON boot-camp. Happy to say 100%, although it’s at the most basic level.

Objects and Data Structures Assessment Test — Python

Numbers

Hint: This is just to test your memory of the basic arithmetic commands, work backward from 100.25

In [8]:

x = 1.25 + (5**2*8/2) - 1print(x)100.25Answer these 3 questions without typing code. Then type code to check your answer.What is the value of the expression 4 * (6 + 5) - 44What is the value of the expression 4 * 6 + 5 - 29What is the value of the expression 4 + 6 * 5 - 34

In [7]:

x = 4 * (6 + 5)y = 4 * 6 + 5z = 4 + 6 * 5print(x,y,z)44 29 34What is the *type* of the result of the expression 3 + 1.5 + 4?<br><br> - float

What would you use to find a number’s square root, as well as its square?

In [11]:

# Square root:16**(0.5)

Out[11]:

4.0

In [12]:

# Square:4**2

Out[12]:

16

Strings

In [13]:

s = 'hello'# Print out 'e' using indexings[1]

Out[13]:

'e'

Reverse the string ‘hello’ using slicing:

In [17]:

s ='hello'# Reverse the string using slicings[::-1]

Out[17]:

'olleh'

Given the string hello, give two methods of producing the letter ‘o’ using indexing.

In [18]:

s ='hello'# Print out the 'o'# Method 1:print(s[-1])

In [19]:

# Method 2:print(s[4])

Lists

In [20]:

# Method 1:mylist = [0,0,0]

In [ ]:

# Method 2:

Reassign ‘hello’ in this nested list to say ‘goodbye’ instead:

In [32]:

list3 = [1,2,[3,4,'hello']]list3[2].pop(2)print(list3)list3[2].append('goodbye')print(list3)[1, 2, [3, 4]]
[1, 2, [3, 4, 'goodbye']]

Sort the list below:

In [37]:

list4 = [5,3,4,6,1]list4.sort()print(list4)[1, 3, 4, 5, 6]

Dictionaries

In [42]:

d = {'simple_key':'hello'}# Grab 'hello'd['simple_key']

Out[42]:

'hello'

In [49]:

d = {'k1':{'k2':'hello'}}# Grab 'hello'd['k1']['k2']

Out[49]:

'hello'

In [60]:

# Getting a little trickerd = {'k1':[{'nest_key':['this is deep',['hello']]}]}#Grab hellod['k1'][0]['nest_key'][1]

Out[60]:

['hello']

In [65]:

# This will be hard and annoying!d = {'k1':[1,2,{'k2':['this is tricky',{'tough':[1,2,['hello']]}]}]}d['k1'][2]['k2'][1]['tough'][2]

Out[65]:

['hello']Can you sort a dictionary? Why or why not?<br><br> Dictionaries are mappings not sequences. so cant be orderded

Tuples

What is the major difference between tuples and lists?<br><br> tuples immutable. How do you create a tuple?<br><br> parantheses cruve brackets

Sets

What is unique about a set?<br><br> only one item of each

Use a set to find the unique values of the list below:

In [75]:

list5 = [1,2,2,33,4,4,11,22,3,3,2]x = set(list5)print(x)
{1, 2, 33, 4, 3, 11, 22}

Booleans

OperatorDescriptionExample==If the values of two operands are equal, then the condition becomes true.(a == b) is not true.!=If values of two operands are not equal, then condition becomes true.(a != b) is true.>If the value of left operand is greater than the value of right operand, then condition becomes true.(a > b) is not true.<If the value of left operand is less than the value of right operand, then condition becomes true.(a < b) is true.>=If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.(a >= b) is not true.<=If the value of left operand is less than or equal to the value of right operand, then condition becomes true.(a <= b) is true.

What will be the resulting Boolean of the following pieces of code (answer first then check by typing it in!)

In [76]:

# Answer before running cell : false2 > 3

Out[76]:

False

In [77]:

# Answer before running cell:false3 <= 2

Out[77]:

False

In [78]:

# Answer before running cell:false3 == 2.0

Out[78]:

False

In [79]:

# Answer before running cell:true3.0 == 3

Out[79]:

True

In [80]:

# Answer before running cell:false4**0.5 != 2

Out[80]:

False

Final Question: What is the boolean output of the cell block below?

In [81]:

# two nested listsl_one = [1,2,[3,4]]l_two = [1,2,{'k1':4}]# True or False? falsel_one[2][0] >= l_two[2]['k1']

Out[81]:

False

Great Job on your first assessment! ( I didn’t write this myself 😂)

Follow Me

Follow me on Twitter!

Emerging Technology Enthusiast | Digital Asset Management | Quantitative Strategy | Financial Markets