summaryrefslogtreecommitdiff
path: root/advanced_python
diff options
context:
space:
mode:
authorPrabhu Ramachandran2017-11-17 13:55:12 +0530
committerPrabhu Ramachandran2017-11-17 13:55:12 +0530
commit5f87e0ae8e4460dc8b5c6649b011a3b382228504 (patch)
tree6ff10100ff6d923d4f081028b5e650907d856b22 /advanced_python
parent89158d47b5f02b4f2029cbf70b7fb47aa6ae01b5 (diff)
downloadpython-workshops-5f87e0ae8e4460dc8b5c6649b011a3b382228504.tar.gz
python-workshops-5f87e0ae8e4460dc8b5c6649b011a3b382228504.tar.bz2
python-workshops-5f87e0ae8e4460dc8b5c6649b011a3b382228504.zip
Add many exercises.
Diffstat (limited to 'advanced_python')
-rw-r--r--advanced_python/oop_basics.tex125
-rw-r--r--advanced_python/oop_inheritance.tex217
2 files changed, 342 insertions, 0 deletions
diff --git a/advanced_python/oop_basics.tex b/advanced_python/oop_basics.tex
index 1da56fb..d6f4e24 100644
--- a/advanced_python/oop_basics.tex
+++ b/advanced_python/oop_basics.tex
@@ -177,15 +177,140 @@ In []: type(bdfl)
\end{frame}
\begin{frame}[fragile]
+ \frametitle{Changing/adding attributes}
+\begin{lstlisting}
+In []: talk.speaker = 'Arun K P'
+
+In []: talk.tags = 'python,bioscience'
+\end{lstlisting}
+\pause
+\begin{lstlisting}
+# Adding attributes
+
+In []: talk.x = 1
+
+In []: talk.y = 'hello'
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}
\frametitle{Summary}
\begin{itemize}
\item Introduction to Object Oriented Programming
\item A simple example
\item Defining a \lstinline{class}
\item Methods and attributes: encapsulation
+ \item Changing/adding attributes
\end{itemize}
\end{frame}
+\begin{frame}[fragile]
+ \frametitle{Exercise: simplest class}
+ \begin{block}{}
+ Create the simplest possible class, called \lstinline{Simple}, which has
+ no attributes or methods. Try this out on your own IPython interpreters.
+ You should be able to instantiate it as follows:
+ \end{block}
+\begin{lstlisting}
+In []: s = Simple()
+\end{lstlisting}
+ \pause
+ \begin{block}{Hint}
+ Remember the \lstinline{pass} statement?
+ \end{block}
+\end{frame}
+
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+class Simple:
+ pass
+\end{lstlisting}
+ \vspace*{0.5in}
+
+ That is all!
+\end{frame}
+
+\begin{frame}[fragile,plain, fragile]
+ \frametitle{Exercise: \lstinline{Person} class 1}
+ \begin{block}{}
+ Create a simple \lstinline{Person} class with two attributes: a
+ \lstinline{name} (a string) and an \lstinline{age} (a float). To be used
+ as follows:
+ \end{block}
+\begin{lstlisting}
+In []: p = Person('Arun', 22.0)
+\end{lstlisting}
+\end{frame}
+
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+class Person:
+ def __init__(self, name, age):
+ self.name = name
+ self.age = age
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Exercise: \lstinline{Person} class 2}
+ \begin{block}{}
+ Create a simple \lstinline{Person} class with two attributes a
+ \lstinline{name} and an \lstinline{age}. However, let the name default to
+ the string \lstinline{'name'} and the age default to 0.0.
+ \end{block}
+\begin{lstlisting}
+In []: p = Person()
+In []: print(p.name, p.age)
+name 0.0
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+class Person:
+ def __init__(self, name='name', age=0.0):
+ self.name = name
+ self.age = age
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile,plain, fragile]
+ \frametitle{Exercise: simple method}
+ \begin{block}{}
+ Create a simple \lstinline{Person} class with two attributes as before.
+ Add a method called \lstinline{birthday} which takes no arguments and
+ which prints \lstinline{'HBD <name>'} and also increases the age by 1.
+ Note that \lstinline{<name>} is the name of the instance.
+ \end{block}
+
+\begin{lstlisting}
+In []: p = Person(name='Ram')
+In []: p.birthday()
+HBD Ram
+In []: p.age
+Out[]: 1.0
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+class Person:
+ def __init__(self, name='name', age=0.0):
+ self.name = name
+ self.age = age
+
+ def birthday(self):
+ print('HBD', self.name)
+ self.age += 1.0
+\end{lstlisting}
+\end{frame}
+
\end{document}
diff --git a/advanced_python/oop_inheritance.tex b/advanced_python/oop_inheritance.tex
index d9f7558..183eff5 100644
--- a/advanced_python/oop_inheritance.tex
+++ b/advanced_python/oop_inheritance.tex
@@ -2,6 +2,8 @@
\input{macros.tex}
+\newcommand{\py}[1]{\lstinline{#1}}
+
\title[OOP Inheritance]{Advanced Python}
\subtitle{Object Oriented Programming: Inheritance}
@@ -216,6 +218,221 @@ In []: t.get_speaker_firstname())
\end{itemize}
\end{frame}
+\begin{frame}[fragile]
+ \frametitle{Exercise: \lstinline{Animal} class}
+ \begin{block}{}
+ Create an \lstinline{Animal} class that has a name attribute (str) and a
+ single method called \lstinline{greet} which takes no arguments but returns
+ a string about how the animal makes a greeting. By default let
+ \lstinline{greet} return \lstinline{'<name> says greet'}.
+ \end{block}
+\begin{lstlisting}
+In []: a = Animal('human')
+In []: a.greet()
+Out[]: 'human says greet'
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+class Animal:
+ def __init__(self, name):
+ self.name = name
+ def greet(self):
+ return self.name + ' says greet'
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+ \begin{itemize}
+ \item Note that this is the same as
+ \end{itemize}
+\begin{lstlisting}
+class Animal(object):
+ # ...
+\end{lstlisting}
+ \begin{itemize}
+ \item Otherwise \lstinline{object} is implicitly the base class
+ \end{itemize}
+
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Exercise: \lstinline{Cat} class}
+ \begin{block}{}
+ Create \lstinline{Cat} class which derives from the \lstinline{Animal}
+ class \alert{override} \lstinline{greet} return
+ \lstinline{'<name> says meow'}.
+ \end{block}
+\begin{lstlisting}
+In []: a = Cat('Felix')
+In []: a.greet()
+Out[]: 'Felix says meow'
+\end{lstlisting}
+\end{frame}
+
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+class Animal:
+ def __init__(self, name):
+ self.name = name
+ def greet(self):
+ return self.name + ' says greet'
+
+class Cat(Animal):
+ def greet(self):
+ return self.name + ' says meow'
+\end{lstlisting}
+ \begin{itemize}
+ \item Note, no need to override \lstinline{__init__}
+ \end{itemize}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Exercise: \lstinline{Mammal} class}
+ \begin{block}{}
+ Create a \lstinline{Mammal} class that derives from \lstinline{Animal}
+ that takes an additional argument that takes an additional argument
+ specifying the number of legs (an integer) and stores it in the attribute
+ \lstinline{legs}.
+ \end{block}
+
+\begin{lstlisting}
+In []: m = Mammal('dog', 4)
+In []: m.legs
+Out[]: 4
+In []: m.greet()
+Out[]: 'dog says greet'
+\end{lstlisting}
+\end{frame}
+
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+class Mammal(Animal):
+ def __init__(self, name, legs):
+ super().__init__(name)
+ self.legs = legs
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution alternative}
+\begin{lstlisting}
+class Mammal(Animal):
+ def __init__(self, name, legs):
+ self.name = name
+ self.legs = legs
+\end{lstlisting}
+ \begin{itemize}
+ \item Will also work but a \alert{bad} idea
+ \item What if \lstinline{Animal.__init__} changes?
+ \end{itemize}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Exercise: \lstinline{Human} class}
+ \begin{block}{}
+ Create a \lstinline{Human} class that derives from \lstinline{Mammal}
+ modify its \lstinline{greet} method to return
+ \lstinline{'<name> says hello'}. Also change the default number of legs
+ to 2.
+ \end{block}
+
+\begin{lstlisting}
+In []: h = Human('Ram')
+In []: h.legs
+Out[]: 2
+In []: h.greet()
+Out[]: 'Ram says hello'
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+class Human(Mammal):
+ def __init__(self, name, legs=2):
+ super().__init__(name, legs)
+ def greet(self):
+ return self.name + ' says hello'
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[plain, fragile]
+ \frametitle{Complete Solution}
+ \vspace*{-0.1in}
+ \small
+\begin{lstlisting}
+class Animal:
+ def __init__(self, name):
+ self.name = name
+ def greet(self):
+ return self.name + ' says greet'
+
+class Mammal(Animal):
+ def __init__(self, name, legs):
+ super().__init__(name)
+ self.legs = legs
+
+class Human(Mammal):
+ def __init__(self, name, legs=2):
+ super().__init__(name, legs)
+ def greet(self):
+ return self.name + ' says hello'
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}
+ \frametitle{Observations}
+ \begin{itemize}
+ \item \py{Human} is a \py{Mammal}
+ \item \py{Human} is an \py{Animal}
+ \end{itemize}
+\end{frame}
+
+
+\begin{frame}[plain, fragile]
+ \frametitle{Exercise: Methods in subclass}
+ \begin{block}{}
+ Add a method to \py{Human} called \py{speak} which prints
+ \py{'My name is <name>'}.
+ \end{block}
+
+\begin{lstlisting}
+In []: h = Human('Ram')
+In []: h.speak()
+My name is Ram
+\end{lstlisting}
+\end{frame}
+
+
+\begin{frame}[plain, fragile]
+ \frametitle{Solution}
+\begin{lstlisting}
+class Human(Mammal):
+ def __init__(self, name, legs=2):
+ super().__init__(name, legs)
+ def greet(self):
+ return self.name + ' says hello'
+ def speak(self):
+ print('My name is', self.name)
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}
+ \frametitle{Observations}
+ \begin{itemize}
+ \item \py{Human} is a \py{Mammal}
+ \item \py{Human} is an \py{Animal}
+ \item \py{Human} can speak but \py{Mammal, Animal} cannot
+ \end{itemize}
+\end{frame}
\end{document}