ALevel

0.1 - Class Notes

Computer Science Keywords

Extra

Python Errors

TypeError

Incorrect type

a="sdhuisahd"
print(a+1)
print(None*3)

ZeroDivisionError

print(1/0)

SyntaxError

print())

KeyError

accessing a non-existent key in dictionary

a={}
print(a['a'])

UnicodeEncodeError

encoding error

print('\x93'.encode('ascii'))

NameError

when variable not defined

print(a)

AttributeError

when class does not have the attribute defined

class NewType:
    pass
inst=NewType()
print(inst.a)

User Defined Types

class NewType:
    def __init__(self):
        self.a=2
inst=NewType()
inst.a=923
print(inst.a)

user defined types’ behaviour achieved with class instances

Here, the class ‘NewType’ is defined with attribute ‘a’ which is initialised to some value. Demonstration of instantiating that class and replacing attribute ‘a’ with some other variable + demonstration of attribute access.