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:
- A brief intro to get you on the right path.
- A list of exercises to complete (not every unit has these)
- A list of 'references' that you will need to read to complete any tasks or exercises for this unit
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!
Next SectionUnit 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:
Next SectionUnit 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.
- Create a new virtual machine by hitting the 'New' button in the upper left hand corner
- 
  Choose a name (e.g. 'Sandbox') and make sure to selec the 'Type' as
  Linuxand 'Version' asDebian (32-bit)
- Select a memory size that your computer can handle (1024MB is a good size, you can go as low as 512MB)
- Select "Use an existing virtual hard drive file" and when prompted, select the uncompressed Hypatia Software virtual data image.
- Hit the 'Create' button.
- There should be a new machine listed to the left with text indicating that it's 'Powered Off'.
- Highlight the new machine listed on the left and hit the 'Start' button at the top.
The virtual machine should start to boot up and you're ready to go!
Next SectionUnit 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):
- 
  man- Display the manual for the given command. This is one of the most important commands you will learn today! Use themancommand to look up how to use other commands, example:man ls
- 
  pwd- Show the Present Working Directory that you are currently in (where you are)
- 
  ls- List current files and directories in your current working directory (what is here)
- 
  cd DIRECTORY_NANE- Change Directory to the DIRECTORY_NAME you give (move around)
- 
  mkdir DIRECTORY_NAME- Create a new directory with the name DIRECTORY_NAME
- 
  cat FILE_NAME- Print a file to the screen
- 
  rm FILE_NAME- Remove a file (delete)
- 
  nano- A Text Editor that can be used to edit files at the command line
 
  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
- 
  Use the
  mancommand to look up how to use thelscommand's-aflag
- List a directory
- Show the current directory
- Create a directory
- Go into the newly created directory
- List the directory with hidden files
- Print something out on the shell
- Put something into a file
- Show contents of file
- Remove a file
- Move a file
References
Next SectionUnit 1 - Command Line Interface (cli) [HINT]
Exercises
- Create a Python program to print out "Hello Python!" with the text editor and run it from the command line.
- list a directory
- show the current directory
- create a directory
- go into the newly created directory
- list the directory with hidden files
- print something out on the shell
- put something into a file
- show contents of file
- remove a file
- move a file
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
- 
  Search for the package
  figlet
- 
  Install the package
  figlet
- Run figlet and have it print out "hello"
- Remove the package figlet
Unit 1 - apt-get [HINT]
 
  apt-get
 
 is Debian's package management suite.
Exercises
- 
  Search for the package
  figlet
- 
  Install the package
  figlet
- Run figlet and have it print out "hello"
- Remove the package figlet
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:
- commit you work to save it and add a log message to document why you made the changes
- push your work to the remote git server so others can see your changes
- Best practices mean commit and push your work often so work history is not lost!
- With advanced usage this will allow you to revert your changes if you decide you made a mistake.
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!
Next SectionUnit 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
- 
  Create a Python program to print out "Hello Python!" with the text editor. Save the file as
  Unit1.pyand then add and commit it to your repository.
- 
  Create a new repository on https://github.com named
  hypatia-exercisesand then add the remote repository and push your repository to it.
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:
- Add, subtract, multiply, divide and exponentiate 9 and 3
- Concatenate "koala" and "bear" with a space in between them
- Put an integer list into a variable, add the second to the first and store it back into the first element
- Put a string list into a variable, concatenate the second to the first and store it back into the first element
- Append two new elements to the back of each list
- 
  Remove the second and third element to create a new list 
- 
  Make a list with five elements, 'koalas', 'are', 'are', 'arr', 'cute' 
- Extend the list with another list ['and', 'so', 'are', 'sloths']
- Append the string '!'
- Count the number of occurrences of the word 'sloths', 'koalas', and the word 'are'
- Remove the first element whose value is 'are'
- Now count the number of occurrences of the word 'sloths', 'koalas', and the word 'are'
- Pop the third element
- 
  Create a string by joining all remaining elements and putting a space in between them 
- 
  Find the index of where the string 'sloths' occurs 
- Create a new list with the third element, the two elements before the element 'sloths', and the element 'sloths'
- 
  Reverse it and join them into a string with a space separator 
- 
  Create a list [10, 3, 1, 50, 10] and sort it 
- 
  Create a list of lists, one of strings and the other of strings. Show the third elements in each. 
References
Next SectionUnit 2 - Exploring Python (Integers/Strings/Lists) [HINT]
Excercise
In a Python shell:
- Add, subtract, multiply, divide and exponentiate 9 and 3
- Concatenate "koala" and "bear" with a space in between them
- Put an integer list into a variable, add the second to the first and store it back into the first element
- Put a string list into a variable, concatenate the second to the first and store it back into the first element
- Append two new elements to the back of each list
- 
  Remove the second and third element to create a new list 
- 
  Make a list with five elements, 'koalas', 'are', 'are', 'arr', 'cute' 
- Extend the list with another list ['and', 'so', 'are', 'sloths']
- Append the string '!'
- Count the number of occurances of the word 'sloths', 'koalas', and the word 'are'
- Remove the first element whose value is 'are'
- Now count the number of occurances of the word 'sloths', 'koalas', and the word 'are'
- Pop the third element
- 
  Create a string by joining all remaining elements and putting a space in between them 
- 
  Find the index of where the string 'sloths' occurs 
- Create a new list with the third element, the two elements before the element 'sloths', and the element 'sloths'
- 
  Reverse it and join them into a string with a space separator 
- 
  Create a list [10, 3, 1, 50, 10] and sort it 
- 
  Create a list of lists, one of strings and the other of strings. Show the third elements in each. 
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
- For each element in the list ["koala", "koala", "koala", "sloth", "koala"], print it out with the string "s are cute!" appended to each.
- 
  For each element in the list ["koala", "koala", "koala", "sloth", "koala"], print it out with the string "s are cute!" if it's a sloth and print "s are kinda cute" if it's a koala. 
- 
  While the list ["koala", "koala", "koala", "sloth", "koala"] is not empty, remove the first element and print it out 
- 
  While the list ["koala", "koala", "koala", "sloth", "koala"] is not empty, remove the first element but only print it out if it's a sloth 
- 
  Print out the numbers from 5 to 111 
- Print out the numbers from 5 to 111 but only if they're divisible by 5 or 3 but not both.
References
Next SectionUnit 3 - Flow [HINT]
Exercises
- For each element in the list ["koala", "koala", "koala", "sloth", "koala"], print it out with the string "s are cute!" appended to each.
- 
  For each element in the list ["koala", "koala", "koala", "sloth", "koala"], print it out with the string "s are cute!" if it's a sloth and print "s are kinda cute" if it's a koala. 
- 
  While the list ["koala", "koala", "koala", "sloth", "koala"] is not empty, remove the first element and print it out 
- 
  While the list ["koala", "koala", "koala", "sloth", "koala"] is not empty, remove the first element but only print it out if it's a sloth 
- 
  Print out the numbers from 5 to 111 
- Print out the numbers from 5 to 111 but only if they're divisible by 5 or 3 but not both.
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
- Write a function to print out "koalas"
- Write a function that returns "sloths"
- Write a function that capitalizes the first letter of a string
- Write a function to square each element in an integer list
- 
  Write a function to return a new list that has each element squared. 
- 
  Put these in a module and use them 
References
Next SectionUnit 4 - Functions and Modules [HINT]
Exercises
- Write a function to print out "koalas"
- Write a function that returns "sloths"
- Write a function that capitalizes the first letter of a string
- Write a function to square each element in an integer list
- 
  Write a function to return a new list that has each element squared. 
- 
  Put these in a module and use them 
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:
- Create a "set" named "myset" with the following values in it: "x", "y", "z", and "cats"
- Create a "dictionary" named "mydict" with the following key-value pairs: "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
- Create a tuple named "mytuple" with the following values in it: "cats", "dogs", "puppies", "kittens"
- Demonstrate the Immutability of "mytuple" (show that values in a tuple cannot be modified)
References
Next SectionUnit 5 - Exploring Python Types part II [HINT]
Exercises
In a Python shell:
- Create a "set" named "myset" with the following values in it: "x", "y", "z", and "cats"
- Create a "dictionary" named "mydict" with the following key-value pairs: "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
- Create a tuple named "mytuple" with the following values in it: "cats", "dogs", "puppies", "kittens"
- Demonstrate the Immutability of "mytuple"
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:
- 
  How to use
  print()to print a variable or string to the screen.
- 
  How to use
  format()to be fancy about printing variables to the screen.
 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:
- 
  Create a variable named
  hellowith the value "Hello" andprint()it to the screen.
- 
  Create a variable
  worldwith the value "Pythonic World!" andprint()to the screen.
- 
  Use
  format()to combine thehelloandworldvariables together, add a space between them.
- 
  Use
  print()to print thehelloandworldvariables together while usingformat()from the previous exercise.
 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
- 
  Create a new program named
  unit6.pythat prints "Exercise 6!" to the screen (make sure you remember to add , commit , and push your work to GitHub!)
References
Next SectionUnit 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:
- 
  Create a file named
  inputfile.txtwith a text editor. Edit the file to have the following contents:
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
- 
  Expand the
  unit6.pyprogram that readsinputfile.txtandprint()s it to the screen.
- 
  Expand the
  unit6.pyprogram to write the contents of theinputfile.txtto a new file namedoutputfile.txt.
References
Next SectionUnit 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:
- 
  Modify the
  unit6.pyprogram to usewithto open files so they automatically close when you are done reading and writing from them.
Unit 7 - Exception handling
Exercises
Next SectionUnit 7 - Magic Methods
Exercises
Next SectionUnit 7 - Metaclasses
Exercises
Next SectionUnit 7 - Inheritance
Exercises
Next SectionUnit 8 - Python Standard Libraries and Built-ins
Exercises
 In your
 
  hypatia-exercises
 
 repository:
- 
  Create a program named
  unit8.pythat does the following tasks:
- Prints the current working directory to the screen
- 
  Lists all files ending with
  .pyand print them to the screen
References
- [Brief Tour of the Standard Library (tutorial)](https://docs.python.org/3/tutorial/stdlib.html?highlight=standard%20libraries
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:
- 
  Add a python3 virtual environment to your
  hypatia-exercisesdirectory, make sure you do not check it into git. (never check your virtual environment directory into git).
- 
  Install the
  requestslibrary into the virtual environment you just created.
- 
  Create a new program named
  unit9.pythat does the following: Gets the content of this web-page and print it to the screen.
References:
Next SectionUnit 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:
- manage.py - Script used to manage your Django project. Can even start a built-in web server to run your project!
- firstproject/ - Directory your Django project files are stored in.
- firstproject/ init.py - Empty file that tells Django that your project needs to be loaded.
- firstproject/settings.py - Configuration file for your Django project.
- firstproject/urls.py - URL declarations file. This is where you set what valid URLs your application take (example: http://localhost:8000/page1, http://localhost:8000/page2)
- firstproject/wsgi.py - Used for WSGI compatible web servers (don't worry about this for now).
 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:
- 
  Create a new directory named
  unit9-django.
- 
  Install a Python virtual environment (make sure to specify python3!) into the
  unit9-djangodirectory.
- 
  Use the
  django-admincommand to start a new project in the `unit9-django' directory.
- 
  Use
  manage.pyto start your development server. Confirm it is running with a web-browser.
- Add and commit your new project files and push them to your GitHub repository (make sure you do not commit your virtual environment!).
References:
Next SectionUnit 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:
- blog/ init.py - Empty file that tells Django to load this app.
- blog/admin.py - File for setting up Django Admin.
- blog/apps.py - File for initializing this app.
- 
  blog/migrations/ - Directory that stores Django
  migrationsused to manage your database.
- 
  blog/modals.py - File for defining your
  modals, these are Python Classes that define objects in the database.
- 
  blog/tests.py - File for defining
  teststo test that your app works as intended.
- 
  blog/views.py - File for defining
  views, this controls the content users see on your app.
 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:
- Create
References:
Next SectionUnit 10 - Hosting Your Django App!
Exercises
Next SectionUnit 11 - Setting Up Nginx as a Reverse Proxy
Exercises
Next SectionUnit 11 - Event logic VS Polling
Exercises
Next SectionUnit 11 - Debugging
Exercises
Next SectionUnit 11 - Power of Duck Typing!
Exercises
Next SectionUnit 12 - Working With Git and Github part II
Exercises
Next SectionUnit 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:
- Pick your project! We recomend writing a web application in django! It can be anything you want, if you are having trouble coming up with something here are some ideas: A reminder program, game, photo gallery, etc.
- 
  Create a new repository on GitHub.com. Be sure to add a
  README.mdfile as well as aLICENSEfile
- Continue to meet with your mentor one hour a week as you work on this project. Be sure to come with questions prepared for when you get stuck.
Good luck and happy hacking!
