Lecture Week 2 Mon 10/7

Lecture Week 2 Mon 10/7#

# f-string allow you to embed expressions inside the string by wrapping them in curly braces
a = 'hello'
formatted_string = f'a = {a}'
print(formatted_string)
a = hello
a = '1.2'
formatted_string = f'a = {a}'
print(formatted_string)
a = 1.2
sin(pi)
# sin and pi are not defined and will raise an error
1.2246467991473532e-16
# import the definition of sin and pi from the math module
from math import sin,pi
pi
3.141592653589793
sin(pi)
1.2246467991473532e-16
# list is an example of an object: it has some data (the elements of the list) and some methods (append, remove, etc.)
mylist = [1,2]
type(mylist)
list
mylist.append(3)
print(mylist)
[1, 2, 3]
from math import sqrt
class Vector():
    dim = 2

    def __init__(self, input_x, input_y):
        # constructor, called when new object is created, Vector(x,y)
        # self is the object being created
        # to create a vector: v = Vector(3,4)
        # .x and .y are object attributes
        self.x = input_x
        self.y = input_y

    def __repr__(self):
        # returns the string representation of the object
        # so that print(v) is formatted
        return f'({self.x}, {self.y})'

    def length(self):
        # custom method to returns the length of the vector
        l = sqrt(self.x**2 + self.y**2)
        return l

    def scale(self, c):
        # method that scales the vector by a constant c
        # changes the object itself
        # no return value, so returns None
        self.x = self.x * c
        self.y = self.y * c

    def normalize(self):
        # method that scales the vector to unit length
        # can call other methods of the same object
        l = self.length()
        self.scale(1/l)

    def add(self, other_vector):
        # method that adds another vector to this vector, returns new vector
        # to use this method: u = v1.add(v2)
        x_new = self.x + other_vector.x
        y_new = self.y + other_vector.y
        return Vector(x_new, y_new)

    def __add__(self, other_vector):
        # special method that overloads the + operator
        # to use this method: u = v1 + v2
        # without this method, u = v1 + v2 would raise an error: unsupported operand type(s) for +: 'Vector' and 'Vector'
        # that is, Python does not know the meaning of + for two Vector objects
        x_new = self.x + other_vector.x
        y_new = self.y + other_vector.y
        return Vector(x_new, y_new)
        
v = Vector(3,4)
w = Vector(1,0)
# dim is shared by all objects of the class
print(v.x, v.y, v.dim)
3 4 2
print(v.length())
5.0
z = v.scale(2)
# scale returns None, so z is None
print(z)
None
# x and y of v have been changed
print(v)
(6, 8)
v.normalize()
print(v)
(0.6000000000000001, 0.8)
print(w.x, w.y, w.dim)
1 0 2
a = v.add(w)
print(a)
(1.6, 0.8)
b = v + w
print(b)
(1.6, 0.8)
# dir function returns the list of attributes and methods of an object
# everything with double underscores has special meaning in Python
dir(v)
['__add__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getstate__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'add',
 'dim',
 'length',
 'normalize',
 'scale',
 'x',
 'y']

Extra, not exam material#

A module in Python is a file that contains Python code, such as definitions of functions, classes, and variables

You can save the definitions of the class or function in a separate file and import it in your notebook. This is useful when you have a large class or function that you want to reuse in multiple notebooks.

For example, if we save the definition of our Vector class into a file called myVector.py, we can import it in a new notebook like this:

from myVector import Vector
v = Vector(1, 2)