In [52]:
class User(object):
    def __init__(self, last_name, first_name):
        self.last_name = last_name
        self.first_name = first_name
        self.active = False
        self._tweets = []
    def full_name(self):
        # return self.first_name + " " + self.last_name
        return f"First Name: {self.first_name}, Last Name: {self.last_name}"
    def __repr__(self):
        return f"User('{self.last_name}', '{self.first_name}')"
    def activate(self):
        self.active = True
    def add_tweet(self, tweet):
        if not self.active:
            raise ValueError("User is not active")
        self._tweets.append(tweet)
    def print_tweets(self):
        for i, tweet in enumerate(self._tweets):
            print(i, tweet)
In [53]:
alice = User(first_name='Alice', last_name='Smith')
In [54]:
alice.full_name()
Out[54]:
'First Name: Alice, Last Name: Smith'
In [55]:
alice.first_name
Out[55]:
'Alice'
In [56]:
alice.last_name
Out[56]:
'Smith'
In [57]:
alice
Out[57]:
User('Smith', 'Alice')
In [58]:
alice.active
Out[58]:
False
In [59]:
alice.add_tweet("This is my first tweet!")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-59-14d30331d50d> in <module>()
----> 1 alice.add_tweet("This is my first tweet!")

<ipython-input-52-18051283e41c> in add_tweet(self, tweet)
     14     def add_tweet(self, tweet):
     15         if not self.active:
---> 16             raise ValueError("User is not active")
     17         self._tweets.append(tweet)
     18     def print_tweets(self):

ValueError: User is not active
In [ ]:
# Don't do this with atributes/methods starting with _
# alice._tweets.append("This is my first tweet!")
In [60]:
alice.activate()
In [61]:
alice.add_tweet("This is my first tweet")
In [62]:
alice.add_tweet("This is my second tweet. I like this site!")
In [63]:
alice.print_tweets()
0 This is my first tweet
1 This is my second tweet. I like this site!
In [5]:
bob = User(first_name='Bob', last_name='Smith')
In [6]:
bob.first_name
Out[6]:
'Bob'
In [7]:
type(alice)
Out[7]:
__main__.User
In [64]:
def foo(x=[]):
    x.append(1)
    print(x)
In [65]:
foo()
foo()
[1]
[1, 1]
In [87]:
from itertools import zip_longest
In [88]:
list(zip([1, 2, 3], [10, 20]))
Out[88]:
[(1, 10), (2, 20)]
In [90]:
list(zip_longest([1, 2, 3], [10, 20], fillvalue=0))
Out[90]:
[(1, 10), (2, 20), (3, 0)]
In [107]:
class Polynomial(object):
    def __init__(self, var='x', coeffs=None):
        if coeffs is None:
            self.coeffs = []
        else:
            self.coeffs = coeffs
        self.var = var
    def __repr__(self):
        return f"Polynomial({self.var!r}, {self.coeffs!r})"
    def __str__(self):
        return " + ".join([f"({coeff} * {self.var} ** {deg})" 
                for deg, coeff in enumerate(self.coeffs)])
    def __add__(self, other):
        if other.var != self.var:
            raise NotImplementedError("Cannot add polynomials of different variables")
        return Polynomial(self.var,
                          [a + b for a, b in zip_longest(self.coeffs,
                                                         other.coeffs,
                                                         fillvalue=0)])
    def __call__(self, value):
        return sum([coeff * value ** deg 
                    for deg, coeff in enumerate(self.coeffs)])
    def has_free_term(self):
        return self.coeffs[0] != 0
In [113]:
f = Polynomial('x', [-10, -5, 1])
g = Polynomial('x', [10, 20, 15, 2])
In [114]:
(f + g).has_free_term()
Out[114]:
False
In [106]:
(f + g)(4)
Out[106]:
460
In [104]:
f(2)
Out[104]:
0
In [105]:
f(5)
Out[105]:
6
In [98]:
f.__add__(g)
Out[98]:
Polynomial('x', [15, 23, 17, 2])
In [86]:
repr(s)
Out[86]:
"'123'"
In [80]:
p = Polynomial('x', [1, 2, 3])
In [83]:
repr(p)
Out[83]:
"Polynomial('x', [1, 2, 3])"
In [84]:
str(p)
Out[84]:
'(1 * x ** 0) + (2 * x ** 1) + (3 * x ** 2)'
In [81]:
print(p)
(1 * x ** 0) + (2 * x ** 1) + (3 * x ** 2)
In [70]:
v = "12"
f"v = {v}"
Out[70]:
'v = 12'
In [71]:
v = "12"
f"v = {v!r}"
Out[71]:
"v = '12'"
In [ ]: