Unit 0 - Hypatia Python Coursework

Welcome to our self study guide! We really hope this is of use to you as you make your way though Hypatia's Mentorship program. This study guide uses the following format as you work you way though the units and sub-units:

If at any time you get stuck or lost, contact your mentor! Do not be afraid to ask questions, the only foolish questions are those we never ask.

Also remember to make use of the #learning channel on Slack. This is a great place to ask for help if you would like more generic feedback from the group.

If you encounter errors while working with this Syllabus please report them on our bug tracker found here on GitHub!

Unit 1 - Installing the development system

VirtualBox allows you to run a virtual machine no matter what your host operating system is (Windows, Mac, Linux). VirtualBox will be used to run Hypatia Software's Debian virtual machine image so that there is a standard system to do development on.

First install VirtualBox. This will be different depending on which host operating system you use.

References:

Unit 1 - VirtualBox (Windows/Mac/Linux)

To get started, first download the data image we will use:

Uncompress it to a directory and remember where it's located.

Now, go to the VirtualBox.org website and select the appropriate binary from the list provided. Download and run the VirtualBox program.

The virtual machine should start to boot up and you're ready to go!

Unit 1 - Command Line Interface (cli)

The command line provides a more advanced interface to interact with the operating system. Textual commands are entered in the "shell" to issue various operations.

The main advantage to working in the "shell" is that it enables the user (you) to have full control over the way commands are executed. While there are graphical tools that allow you to do many of these same functions, it is very important that you learn what is going on behind the scenes. This way if a graphical program is not working right you will know what is going on under the hood, and will most likely be equipped to solve the problem.

While navigating your shell there are some basics commands that you will need to know to get started (when you see DIRECTORY_NAME or FILE_NAME be sure to replace this with the directory of your choice):

WARNING : Using commands like rm -rf can delete large parts of your filesystem if you are not careful. Always double check your command before typing it. If you are unsure of yourself, use rm -ri instead so that rm will confirm the filename with you before deleting it.

Patrice creating and deleting files and directories:

 hypatian@hypatia:~$ mkdir testing
 hypatian@hypatia:~/testing$ cd testing
 hypatian@hypatia:~/testing$ mkdir dir1 dir2 dir3
 hypatian@hypatia:~/testing$ ls
 dir1 dir2 dir3
 hypatian@hypatia:~/testing$ rm -rf dir2
 hypatian@hypatia:~/testing$ ls
 dir1 dir3

When you are done clean up your testing directory:

 hypatian@hypatia:~/testing$ pwd
 /home/hypatian/testing
 hypatian@hypatia:~$ cd ..
 hypatian@hypatia:~$ rm -rf testing

Do this for a while until you feel you have the hang of creating and deleting files and directories. Be sure you understand how to navigate files and directories at the shell before moving on.

Exercises

References

Unit 1 - Command Line Interface (cli) [HINT]

Exercises

Hints

Use the man command to look up how to use the ls command's -a flag

~ man ls

(the -a flag shows hidden files!)

List a directory:

~ ls

Print the directory:

~ pwd

Make a directory:

~ mkdir sandbox

Go into the newly created sandbox directory:

~ cd sandbox

List the hiddne files in the directory:

~ ls -la

Print out "hello" from the command line:

~ echo hello

Put the "hello" into a file:

~ echo hello > file.txt

Show the contents of the file:

~ cat file.txt

Remove the file:

~ rm file.txt

Move a file:

~ mv ../hello_python.py .

Unit 1 - apt

apt is Debian's package management suite. You can use apt's apt-get and apt-cache commands to install and remove software as well as search the package cache for programs that you can install. For example if we wanted to search the package cache for the cowsay program. First we would update our repository list with the following command:

 hypatian@hypatia:~$ sudo apt-get update

Take note that we must use the sudo command to "Switch Users and DO" the apt-get command. By default this uses the root account, which is the administrative account for Linux and Unix based Operating Systems. Now that we have the up to date package list from the repository we can search it for cowsay :

 hypatian@hypatia:~$ apt-cache search cowsay
 ...

As you can see the cowsay program exists, so it can be installed. We can do this with apt-get :

 hypatian@hypatia:~$ sudo apt-get install cowsay

Exercises

Unit 1 - apt-get [HINT]

apt-get is Debian's package management suite.

Exercises

Hints

~ apt-cache search figlet
...

~ sudo apt-get install figlet
...

~ figlet "hello!"
 _          _ _       _ 
| |__   ___| | | ___ | |
| '_ \ / _ \ | |/ _ \| |
| | | |  __/ | | (_) |_|
|_| |_|\___|_|_|\___/(_)


~ sudo apt-get remove figlet
...

Unit 1 - Gitting with Git!

This is the final part of Unit 1 and the last piece of the basic skill set you will need to successfully write your first Python program. So what is git ? Git is a type of program known as a Version Control System (VCS). A VCS is program that tracks changes in your code and allows you to share your changes with others. Think of git as a tool to "save" your work. The basic functions of git are as follows:

Getting Setup

Now that you have an idea of the basics lets git! But first we have some initial setup to do:

1) Open a web-browser and navigate to github.com (if you already have an account, you can skip this step), create an account,

2) Now lets use our comand line skills we learned earlier. Open up a terminal on your virtual machine, lets create an install git with pkg :

pkg install git

3) Now lets create an ssh-key . You do not have to worry about the specifics of this now, but you do need to know that it works as a strong password that allows you to push your changes to github.com. Create the ssh-key with the following command:

ssh-keygen -t ed25519

4) Now we have to display the public peice of our ssh-key, do this with the following command:

cat ~/.ssh/id_ed25519.pub

5) Go back to github.com, and navigate to your account settings. Click the option to add an SSH Key to your account.

6) Copy the ssh key you displayed to the screen, and add it to your github.com account. You can name it anything you like. The ssh-key should look something like:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAj7b/sXdlzasdfj/S0drcKasdfiIwCfh2o+ztxE2I6S username@hostname

And thats it! That is all the setup you need to begin using git .

The next and last section of Unit 1 will be making your first git commit! Happy Gitting!

Unit 1 - Using Git!

From this point forward you will keep all your exercises in a git repository! By doing this you can share your progress with your mentor easily. Additionally your mentor will be able to provide you with feedback and suggestions via git, which will get you acquainted to the way programmers work together in teams!

The next step is to make your first local repository:

Your first Commit!

1) At the command line create a new directory for you git repository:

mkdir helloworld

2) Change your current working directory to the directory you just made:

cd helloworld

3) Turn your directory into a git repository by initializing it with the following command:

git init

4) Create an empty README.txt file as our first file:

echo "Hello world" > README.txt

5) Now lets add our changes to git:

git add README.txt

6) Before our changes are saved, we must commit our work:

git commit -m 'Added README.txt file'

7) Check to see if our changes were commited, you should see the message you added "Added README.txt file":

git log

And thats it! You did it. You should now have your first repository with your first commit!

Exercises

Unit 1 - Using Git! [HINT]

Exercise Hints

Create a Python program to print out "Hello Python!" with the text editor. Save the file as hello.py and then add and commit it to your repository.

git add hello.py
git commit -m 'Added hello world'

Create a new repository on https://github.com named hypatia-exercises and then add the remote repository and push your repository to it.

git remote add git@github.com:gitusername/repository_name.git
git push origin master

Unit 2 - Exploring Python (Integers/Strings/Lists)

Exercises

In a Python shell:

References

Unit 2 - Exploring Python (Integers/Strings/Lists) [HINT]

Excercise

In a Python shell:

References

Hint

Add, subtract, multiply, divide and exponentiate 9 and 3

>>> 3**5
243
>>> 9+3
12
>>> 9 - 3
6
>>> 9 *3
27
>>> 9/3
3
>>> 9**3
729

Find the quotient of 12512501 and 19761. Find the remainder (modulo).

>>> numerator = 12512501
>>> denominator = 19761
>>> numerator / denominator
633
>>> quotient = numerator / denominator
>>> remainder = numerator % denominator
>>> print quotient, remainder
633.5788
>>> denominator * quotient + remainder
12512501

Concatenate "koala" and "bear" with a space in between them

>>> "koala" + " " + "bear"
'koala bear'

Put an integer list into a variable, add the second to the first and store it back into the first element

>>> x = [ 2, 3, 5 ]
>>> x
[2, 3, 5]
>>> x[0] = x[0] + x[1]
>>> x
[5, 3, 5]

Put a string list into a variable, concatenate the second and third to the first and store it back into the first element

>>> y = [ "koala", " ", "bear" ]
>>> y
['koala', ' ', 'bear']
>>> y[0] = y[0] + y[1] + y[2]
>>> y
['koala bear', ' ', 'bear']
>>> y[0]
'koala bear'

Append two new elements to the back of each list

>>> y.append('s')
>>> y.append(' are cute!')
>>> y
['koala bear', ' ', 'bear', 's', ' are cute!']
>>> ' ^-^ '.join(y)
'koala bear ^-^   ^-^ bear ^-^ s ^-^  are cute!'

Remove the second and third elements

>>> del y[1]
>>> del y[2]
>>> y
['koala bear', 'bear', ' are cute!']

Koalas and sloths:

>>> z = [ 'koalas', 'are', 'are', 'arr', 'cute' ]
>>> z
['koalas', 'are', 'are', 'arr', 'cute']
>>> z.extend(['and', 'so', 'are', 'sloths'])
>>> z
['koalas', 'are', 'are', 'arr', 'cute', 'and', 'so', 'are', 'sloths']
>>> z.append('!')
>>> z
['koalas', 'are', 'are', 'arr', 'cute', 'and', 'so', 'are', 'sloths', '!']
>>> z.count("koalas")
1
>>> z.count("sloths")
1
>>> z.count("are")
3
>>> z.remove('are')
>>> z
['koalas', 'are', 'arr', 'cute', 'and', 'so', 'are', 'sloths', '!']
>>> z.count("are")
2
>>> z.pop(2)
'arr'
>>> z
['koalas', 'are', 'cute', 'and', 'so', 'are', 'sloths', '!']
>>> " ".join(z)
'koalas are cute and so are sloths !'

Creating new lists, reversing

>>> i = z.index('sloths')
>>> i
6
>>> w = z[2:3] + z[i-2:i+1]
>>> w
['cute', 'so', 'are', 'sloths']
>>> w.reverse()
>>> w
['sloths', 'are', 'so', 'cute']
>>> " ".join(w)
'sloths are so cute'

Integer list and sorting

>>> t = [10, 3, 1, 50, 10]
>>> t
[10, 3, 1, 50, 10]
>>> t.sort()
>>> t
[1, 3, 10, 10, 50]

List of lists:

>>> q = [["cute", "koala", "bears"], [3, 5, 7]]
>>> q[0]
['cute', 'koala', 'bears']
>>> q[1]
[3, 5, 7]
>>> q[0][2]
'bears'
>>> q[1][2]
7

Unit 3 - Flow

Exercises

References

Unit 3 - Flow [HINT]

Exercises

References

Hint

>>> mylist =  ["koala", "koala", "koala", "sloth", "koala"]
>>> for v in mylist:
...     print v + "s are cute!"
... 
koalas are cute!
koalas are cute!
koalas are cute!
sloths are cute!
koalas are cute!


>>> mylist = ["koala", "koala", "koala", "sloth", "koala"]
>>> for v in mylist:
...     if v == "sloth":
...         print v + "s are cute!"
...     elif v == "koala":
...         print v + "s are kinda cute"
...     else:
...         print v + "s are ok"
... 
koalas are kinda cute
koalas are kinda cute
koalas are kinda cute
sloths are cute!
koalas are kinda cute


>>> mylist = ["koala", "koala", "koala", "sloth", "koala"]
>>> while len(mylist) > 0:
...     v = mylist.pop(0)
...     print v
... 
koala
koala
koala
sloth
koala


>>> mylist = ["koala", "koala", "koala", "sloth", "koala"]
>>> while len(mylist) > 0:
...     v = mylist.pop(0)
...     if v == "sloth":
...         print v
... 
sloth


>>> for x in range(5,112):
...     print x
... 
5
6
7
...
109
110
111


>>> for x in range(5,112):
...     if ((x%5) == 0) and ((x%3) == 0):
...         pass
...     elif (x%5) == 0:
...         print x
...     elif (x%3) == 0:
...         print x
... 
5
6
9
10
12
18
...

Unit 4 - Functions and Modules

Exercises

References

Unit 4 - Functions and Modules [HINT]

Exercises

References

Hints

alas():
    print "koalas"

def sloths():
    return "sloths"

def capitalize(s):
    return s[0].upper() + s[1:]

def square_elements(a):
    for k,v in enumerate(a):
        a[k] = v*v

def return_square_elements(a):
    X = []
    for val in a:
        X.append(val*val) 
    return X

Put these into a file called mymodule.py . Then from another program or interactive Python shell:

>>> import mymodule
>>> mymodule.koalas()
koalas
>>> v = mymodule.sloths()
>>> print v
sloths
>>> print mymodule.capitalize("sloths")
Sloths
>>> a = [1,2,5,8]
>>> mymodule.square_elements(a)
>>> print a
[1, 4, 25, 64]
>>> mymodule.return_square_elements([1,2,5,8])
[1, 4, 25, 64]

Unit 5 - Exploring Python Types part II

Exercises

In a Python shell:

References

Unit 5 - Exploring Python Types part II [HINT]

Exercises

In a Python shell:

References

Hints

Create a "set" named "myset" with the following values in it: "x", "y", "z", and "cats"

>>> myset = { "x", "y", "z", "cats" }
>>> myset
{ "x", "y", "z", "cats" }

Create a "dictionary" named "mydict" with the following key-value pairs: "Sarah" : 1, "James" : 11, "Sally" : 5, "Bob" : "apples"

>>> mydict = { "Sarah" : 1, "James" : 11, "Sally" : 5, "Bob" : "apples" }
>>> mydict
{ "Sarah" : 1, "James" : 11, "Sally" : 5, "Bob" : "apples" }

Create a "list" named "mylist" with the following values in it: "apples", "cats", "dogs", "cats"

Use a List Comprehension to only print the word "cats" for "mylist" which you created previously

>>> [ item for item in mylist if item == 'cats' ]

Create a tuple named "mytuple" with the following values in it: "cats", "dogs", "puppies", "kittens"

>>> mytuple = ("cats", "dogs", "puppies", "kittens")
>>> mytuple
('cats', 'dogs', 'puppies', 'kittens')

Demonstrate the Immutability of "mytuple" (show that values in a tuple cannot be modified)

>>> mytuple[0] = 'a'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Unit 6 - Input and Output

In this unit we will be focused on inputting and outputting data to the screen and files. The key things to walk away from this section are:

Lets set with print() , open your Python shell so you can follow along. First lets use print() to print the string "Printing things":

>>> print("Printing things")
Printing things

Now lets create a variable named mystring to store a string and use print() to print our variable:

>>> mystring = "Printing things"
>>> print(mystring)
Printing things

We can expand on this by creating a new variable named endingstring with the value "is fun!" and combining it with mystring using the string method format() , this is what it looks like:

>>> endingstring = "is fun!"
>>> 'Guess what? {} {}'.format(mystring, endingstring)
'Guess what? Printing things is fun!'

As you can see format() combines mystring and endingstring into the string we provided. The brackets {} is where our variables are inserted. The variables are inserted into the {} brackets in the order they are given.

Lets put it all together now and print() all of our variables while using format() :

>>> print('Guess what? {} {}'.format(mystring, endingstring))
Guess what? Printing things is fun!

Exercises

In a Python shell:

In your hypatia-exercises repository:

NOTE : For these exercises you will need to commit and push your work to https://GitHub.com. If you need to brush up on your Git, check out Unit1 where you set up your GitHub account and pushed to your first Repository: Unit1-using-git.md

References

Unit 6 - Reading and writing to files

As part of input and output we will now learn how to read and write to and from from files. Python makes this very ease for us by providing several methods for working with file objects. Lets open our Python shell and write() a new file:

>>> myfile = open('/tmp/testingfile.txt')
>>> myfile.write('Writing to a file in python, yay!')
>>> myfile.close()

Notice that we must close() our file after we are done working with it. We must always close our files. Now that we have written a file lets open it and print the contents to the screen:

>>> myfile = open('/tmp/testingfile.txt')
>>> file_contents = myfile.read()
>>> myfile.close()
>>> print(file_contents)
Writing to a file in python, yay!

Pratice it a few times until you get the hang of it. Remember to close() your file when you are done reading or writing to it.

Exercises

In your hypatia-exercises repository:

inputfile.txt:

This is line one
This is line two
This is line three
This is line four
You get the point, this is line five!
Line six, pick up sticks

References

Unit 6 - With the right context!

When writing and reading from files you will have noticed that you must close() them when you are finished working with them. To avoid having to do this you can using Python's built-in context manager with . The with statement is built-into Python so there is nothing you need to do on your part to start using with . So how does with help us? Consider this following example:

>>> myfile = open('mytextfile.txt')
>>> data = myfile.read()
>>> myfile.close()
>>> myfile.closed
True

You will notice that we must use close() . It is a best practice in programming to always close your files as soon as you are done working with them. This frees up unused system resources and stops your programs from using more memory than they need to. You can use with to automatically close a file when you are done working with it, here is an example:

>>> with open('mytextfile.txt') as myfile:
...      data = myfile.read()
...
>>> myfile.closed
True

As you can see by using with the file we are working with automatically closes when we move our indention block back a level (the context we are working in ends when we de-indent).

Exercises

In your hypatia-exercises repository:

Unit 7 - Exception handling

Exercises

Unit 7 - Magic Methods

Exercises

Unit 7 - Metaclasses

Exercises

Unit 7 - Inheritance

Exercises

Unit 8 - Python Standard Libraries and Built-ins

Exercises

In your hypatia-exercises repository:

References

Unit 9 - Working with Python virtual environments

Before we can work with bigger libraries and web development frameworks we must first learn how to work with Python virtual environments (virtualenv). Python's virtualenv allow us to install Python libraries locally for only the project we are currently working on. This allows you the developer to install Python libraries in a way that will not conflict with your systems required libraries, and avoid major headaches before they start.

To get started working with virtual environments lets get out our Bash shell (terminal) and create a new directory and change to it to test creating a virtual environment (for a brush up on the command line, see: Unit1: Command Line ):

 hypatian@hypatia:~$ mkdir just-testing
 hypatian@hypatia:~$ cd just-testing
 hypatian@hypatia:~/just-testing$

Now that we have a place to test things lets create our first virtual environment, and activate it:

 hypatian@hypatia:~/just-testing$ virtualenv --python=python3 .venv
 hypatian@hypatia:~/just-testing$ ls -a
 . .. .venv
 hypatian@hypatia:~/just-testing$ source .venv/bin/activate
 (.venv) hypatian@hypatia:~/just-testing$

You will notice that when you activate your virtual environment it puts the directory name of the virtual environment, in this case (.venv) , in-front of your prompt. Make sure you always check to see if your virtual environment is activated! Failing to check this will make your commands run against your operating system, instead of safely inside your virtual environment. Lets now use our virtual environment to install the very powerful requests library. This library will let us write a quick demonstration program that gets a web-page. Using pip we can install the requests library into our virtual environment. Don't forget to make sure you see a (.venv) in front of your prompt:

 (.venv) hypatian@hypatia:~/just-testing$ pip install requests
 ...

Now that we have the requests library installed we can use it! Here is a simple example of using requests to get the content of the HypatiaSoftware.org homepage. Here we will start our Python shell while inside our virtual environment:

 (.venv) hypatian@hypatia:~/just-testing$ python
 ...

Now that we are inside our Python shell, lets get started with requests :

 >>> import requests
 >>> request = requests.get('https://hypatiasoftware.org')
 >>> request.text
 ...

When you are done working with your project, make sure to deactivate your virtual environment (alternatively, you can close your terminal):

 (.venv) hypatian@hypatia:~/just-testing$ deactivate
 hypatian@hypatia:~/just-testing$

Note the (.venv) marker has vanished from our prompt.

Exercises

In your hypatia-exercises repository:

References:

Unit 9 - Creating your First Django App!

Now that you an work with virtual environments we can install the Web-based framework library known as Django. First lets make sure our environment we set up in the previous section is activated :

 hypatian@hypatia:~/just-testing$ source .venv
 (.venv) hypatian@hypatia:~/just-testing$

Now lets install Django 2!

 (.venv) hypatian@hypatia:~/just-testing$ pip install Django==2.0.3
 ...

We can confirm Django is installed by using the django-admin utility to check what version of Django we are using:

 (.venv) hypatian@hypatia:~/just-testing$ django-admin --version
 2.0.3

Django helps us create a modern web application with a clear division of what each file does. When we start a new Django project we use the Django-admin utility to create the initial directories and files we will need to start building our web application. To create our new Django project in your current directory use the following command:

 (.venv) hypatian@hypatia:~/just-testing$ django-admin startproject firstproject .

After you run the above command it will create the following files and directories for you:

Now that we have a working file structure, it is time to start our development server . We can do this with manage.py script's runserver command. While our virtual environment is activated run the following command to start your server:

 (.venv) hypatian@hypatia:~/just-testing$ python manage.py runserver
 Performing system checks...

 System check identified no issues (0 silenced).

 You have unapplied migrations; your app may not work properly until they are applied.
 Run 'python manage.py migrate' to apply them.

 April 10, 2018 - 15:50:53
 Django version 2.0, using settings 'mysite.settings'
 Starting development server at http://127.0.0.1:8000/
 Quit the server with CONTROL-C.

The development server is now running ! Good work! You can confirm it is running by using Firefox from inside the Hypatia Developers Image (or locally from a web-browser of your choice) and navigate to the following URL: http://localhost:8000

If everything went right you should the Django "Congratulations" page.

To stop the server type CTRL+C in the terminal you started the development server in.

Exercises

In your hypatia-exercises repository:

References:

Unit 9 - Creating your First Django App, part 2!

As we continue along, lets use our just-testing directory we created in the previous section. First lets make sure we activate the virtual enviorment if we have not already:

 hypatian@hypatia:~/just-testing$ source .venv/bin/activate
 (.venv) hypatian@hypatia:~/just-testing$

We can now create our app inside the Django project we created in the pervious section. To do this we will need to use the manage.py scirpt's startapp command:

 (.venv) hypatian@hypatia:~/just-testing$ python manage.py startapp blog

This will create a new directory named blog with the following files and directories inside it:

To get started we will want to create our first view . This will allow us to display content to the user from our web app. We start this by using our text editor to open the blog/views.py file and adding the following content to it:

 from django.http import HttpResponse


 def index(request):
 return HttpResponse("Hello, world. You're at the polls index.")

Exercises

In your hypatia-exercises repository:

References:

Unit 10 - Hosting Your Django App!

Exercises

Unit 11 - Setting Up Nginx as a Reverse Proxy

Exercises

Unit 11 - Event logic VS Polling

Exercises

Unit 11 - Debugging

Exercises

Unit 11 - Power of Duck Typing!

Exercises

Unit 12 - Working With Git and Github part II

Exercises

Unit 13 - Final Project

Congratulations, you made it this far! The remainder of this course will take the form of a final project. You will work with your mentor to create a new open source project with an MIT or GPL type license (your choice). To help you with this process you can use this useful list to start your project:

Good luck and happy hacking!

Python Coursework