When would 2 variables be equal but not have the same identity
?
When they have different addresses in memory
What is the symbol for identity comparison?
In [ ]:
is
Which of the values below return True when used in the following statement?
In [ ]:
num_wheels >= 1024 and num_wheels < 2456
a.) num_wheels = 2456 b.) num_wheels = 1765 c.) num_wheels = 1024 d.) num_wheels = 8000 e.) num_wheels = 1028 f.) num_wheels = 1020
b, c, e
Fix the if statements below so that the checks are correct and the right statements are printed for the age and drivers info.
You may also want to look at the precedence section in the lecture to ensure it is right in all situations.
You DO NOT need to edit variables, just if statements.
In [9]:
age = 23
has_license = True
min_drive_age = 17
cheap_ins_age = 25
def age_check(age):
if age <= 18:
print("Yay, you can drink legally!")
return True
else:
return False
def drive_check(age, has_license):
if age < min_drive_age and has_license:
print("Not legally able to drive but have a license? Do you think I am a fool?")
return False
if age > min_drive_age and not has_license:
print("You can drive but you need to get your license!")
return False
elif age < cheap_ins_age and has_license and age > min_drive_age: #precedence can be used here to make things cleaner
print("You can drive, but insurance is not gonna be cheap")
return True
elif age <= cheap_ins_age and has_license:
print("Go you, cheap insurance all round!")
return True
else:
return False
In [10]:
from nose.tools import assert_equal
assert_equal(True, age_check(age))
assert_equal(True, drive_check(age, has_license))
assert_equal(False, drive_check(18, False))
assert_equal(False, drive_check(14, True))
Write the output of the variable str_two
once the following code block has run:
str_one = "Hello"
str_two = str_one
str_one = "Hi"
print(str_two)
In [27]:
"Hello"
Out[27]:
Explain the difference between mutable and immutable
Mutable types can be changed once they have been created but immutable types maintain their original state on creation and cannot be changed.
In [ ]: