CS52 - Spring 2016 - Class 12
Example code in this lecture
while.py
bit.py
number_and_bit.py
Lecture notes
admin
- how did assignment 4 go?
- assignment 5
- out
- should be able to start after today's class
high level
- look at first two functions of
while.py code
- lists
- defining classes in python: look at
bit.py code
python basics
- running python and interacting with the python shell
- python is an interpreted language, like SML
- can just type "python" at the terminal to get a shell
- execfile
- like "use" in SML
- it's a function, though:
>>> execfile("while.py")
- :
- defines a block, we'll see it a lot
- indentation indicates blocks (the role previously reserved for curly braces)
- indentation is critical and is not just stylistically important
- basic control flow
- if/elif/else
- for
- use of range
- while
- defining functions
- def
- no return type
- no parameter types
- comments
- docstrings
- print
- using str()
- talk about pass
lists
- similar to arrays in Java
- indexable
- can iterate over them
- some differences
- actually a class, so they have methods
- they're extensible and mutatable (sort of like ArrayList class in Java)
- can append to them
- can insert into them
- can delete elements in them
- creating with []
>>> l = [1, 2, 3]
>>> l
[1, 2, 3]
>>> l = ["apples", "bananas", "carrots"]
>>> l
['apples', 'bananas', 'carrots']
>>> l = [1, "apples", [1, 2]]
>>> l
[1, 'apples', [1, 2]]
- indexing
>>> l = [1, 2, 3, 4]
>>> l2 = ["apples", "bananas", "carrots"]
>>> l3 = [1, "apples", [1, 2]]
- starts counting at 0, like in Java
>>> l[0]
1
>>> l2[2]
'carrots'
>>> l3[1]
'apples'
- can index from the back using negative numbers
>>> l[-1]
4
- slicing
- can also "slice" the list to get a sublist
>>> l[1:3]
[2, 3]
- many methods (see the documentation)
- append
>>> l = []
>>> l.append(2)
>>> l.append(3)
>>> l
[2, 3]
- indexing
- iterating over
- using for
- talk about what range does
defining classes in python: look at
bit.py code
camelcasing vs. underscore
- in python, we use underscores to separate multi-word variables and function names (anything lowercase
- but use camelcasing for uppercase things (like classes)
inheritance in python: look at
number_and_bit.py code
- What does the Number class do?
- like the Bit class, but just stores a value (any value, in fact)
- __str__ method:
- like toString in Java
- To inherit from a class (i.e. extend in Java) we put parenthesis after the class name and put the class we're extending from
- NotSafeBit(Number)
- NotSafeBit inherits from Number
- We get all of the methods defined in Number, e.g. the constructor, set, get and __str__
- When inheriting we can also define our own methods
- if they don't exist already, they're added to the class
- if they do exist already, they override the parent class's method
- bit_and in NotSafeBit is a new method
- Why is the class called NotSafeBit?
- It's almost like the Bit class...
- However, because we inherit from Number, we can't assume that the value will always be 0 and 1!
- Look at the Bit class in
number_and_bit.py code
: How does his solve it?
- override the constructor and the set methods to enforce 0 and 1 values
- What will the different_types function print?
Num: 10
nsBits: 10, 1
bits: 1, 1
ns AND: 0
bit AND: 1
- declares a Number with value 10
- declares two lists
- in the nsBits list, adds two NotSafeBits constructed with 10 and 1
- in the bits list, adds two Bits constructed with 10 and 1
- Because the NotSafeBits are just numbers, the 10 stays as a 10 and gives us the wrong answer!