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))


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-10-5f98c1bd9d8e> in <module>()
      1 from nose.tools import assert_equal
      2 
----> 3 assert_equal(True, age_check(age))
      4 assert_equal(True, drive_check(age, has_license))
      5 assert_equal(False, drive_check(18, False))

/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.pyc in assertEqual(self, first, second, msg)
    511         """
    512         assertion_func = self._getAssertEqualityFunc(first, second)
--> 513         assertion_func(first, second, msg=msg)
    514 
    515     def assertNotEqual(self, first, second, msg=None):

/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.pyc in _baseAssertEqual(self, first, second, msg)
    504             standardMsg = '%s != %s' % (safe_repr(first), safe_repr(second))
    505             msg = self._formatMessage(msg, standardMsg)
--> 506             raise self.failureException(msg)
    507 
    508     def assertEqual(self, first, second, msg=None):

AssertionError: True != False

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]:
'Hello'

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 [ ]: