Course Goals

There are a few primary goals for this course that are worth outlining off the bat since not all beginner courses are the same in python.

I have 3 primary goals for you by the end of the course:

  1. Get you up and running quickly with developing python code/apps
  2. Write Maintainable and Readable code in python
  3. Introduce you to programming concepts if this is your first language

Course layout

Each module/section of this course has 3 components to it:

Reading

Information about the topic, the primary resource for reference to complete the exercises and challenge(s).

Exercises

A set of smaller exercises that are intended to gear you up for the challenges. The exercises are intended to be completed after the reading. You can try them out by simply copy-pasting them into a file on your computer called exercises.py.

Challenges

A challenge on the topic of the module, intended to be completed last. You can try them out by simply copy-pasting them into a file on your computer called challenges.py.

The intention is to complete the modules in order and the sections in the order as they are laid out above (Reading then Exercises then Challenges).

Also where applicable I have put some extra resources for topics in case my explanations suck.

source

Getting setup

This course will require you to have a few things to get started:

  1. Python (this course is designed for python 3 and up. (See Python installation and setup section for installation details)
  2. A code editor or IDE (integrated development environment); (see Code editor/IDE setup section for more details)
  3. git (optional); git is a version control system that can be helpful when coding, it is completely optional since you can download a zipped version of this repository straight from github.

Just a note, I am not a teacher so feel free to submit feedback to me to help improve this course for future generations.

when-your-teacher-is-useless

source

Python installation and setup

First things first, lets get python installed on your computer. Below are details for how to do this on each operating system, so follow the steps for your own.

Windows

  1. Download python(This is version 3.7.4 which is the latest as of 4th of September 2019)

  2. Run the .exe file MAKE SURE you check the box on the first screen of the installer that says "add python to PATH", if you forget this you wont be able to run anything

  3. Verify that everything installed properly by opening up cmd (hit the windows key and type cmd and then hit enter) and running python; You should get something like this:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

press ctrl + z or type exit() to break out of it

Mac OS

  1. Buy a windows or linux machine.

Linux

Debian based distributions (ubuntu, linux mint etc.)

  1. Open your terminal, and run sudo apt-get update && sudo apt install python3 -y && sudo apt install python3-pip -y; This will find the list of most up to date versions of python and install python 3 and a useful addon to python called pip.

  2. Verify everything installed by opening your terminal and running python3 something like this should appear:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

press ctrl + z or type exit() to break out of it

Optional

Optionally (but highly recommended) you can modify your .bashrc file to make python 3 the python default. Assuming you installed VS Code you can do this by running code ~/.bashrc and adding the following two lines to the end:

alias python="python3"

alias pip="pip3"

Now you can run python with python instead of python3 and pip instead of pip3

Also if you want to make life easier on yourself in the future then you can install the additional packages that IMO should be included in the default install:

```sudo apt-get install build-essential libsqlite3-dev libbz2-dev libssl-dev libffi-dev python-dev libpq-dev````

Code editor/IDE setup

There are a few options for editors here are the most popular for python:

Python Files and REPL

There are two different modes to run python:

Files

Python files use the .py extension on the file. Python files are read top down and execute line-by-line, unlike compiled languages, python (since it is interpreted) does not check logical errors before running your code. This means any bugs that are written will only be caught by getting to that point in the running file.

REPL (Read Evaluate Print Loop)

Python has an advantage over other languages in that you can run python code without having it in a file. You can use a REPL environment to run pieces of python code on the fly, with immediate feedback and without having to create & run a file.

To do this on windows you can open a terminal (hit the windows key and type cmd), and then just type python you will then get a prompt that looks something like this:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

On mac and linux you can run python3 and should get a prompt like this:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

From here you can start typing python code in, and it will run line by line as you type it.

Python basic syntax

Now you have an environment setup lets look at some python shall we.

Comments

In python you can add comments (text that doesn't do anything); This is incredibly useful for leaving yourself and others notes about how code works, what code does, or to 'comment out' code that you just don't want to run. Throughout the course most of the python files will contain comments that will help you to understand what is happening in the code. Also I will leave sections of challenges and exercises 'commented out' to allow you to work on them in order.

"Commenting your code is like cleaning your bathroom - you never want to do it, but it really does create a more pleasant experience for you and your guests" - Ryan Campbell

There are two ways of doing comments:

  1. Single line comments; comments that only span a single line are denoted with a # in front of them:
# This is a single line comment
  1. Multiline comments; comments that span multiple lines are denoted with three sets of double quotes:
"""
This
comment
spans
many
lines
"""

As you are going through it is always a good idea to put comments in your code so that other people (and you in 3 months) will know what's happening. Believe me this will make a difference


True programmers don;t comment their code... If it was hard to write then it should be hard to read

- Anonymous


When I wrote this code only God and I understood what it does... Now, only God knows.

- people who don't comment their code


Functions

Functions in python are commands you can use to do specific actions. Functions can also be given data (called arguments), and return data.

The basic syntax looks like this

function-name(arguments)

You can tell that something is a function if it has parenthesis "()" after the function name.


For example the print() function in python takes some text (a string [i'll explain what that is in the next chapter] as an argument) and prints it to the terminal.

print("Hello World!")

Running python code

To run your code (after you've written it) use: python (filename).py or python3 (filename).py (linux/mac)

Exercises

# Welcome to the first set of exercises """ ==== Exercise 1: add and remove comments ==== In the below example comment out the first line of code and uncomment the second, then run the file. """ print("This shouldn't print") # print("This should print") """ ==== Exercise 2: print multiple things ==== For this exercise we are going to print multiple things at once. To do this we can have each thing added to the print function as arguments (comma delimited). For example: print("Hello","World!") For this exercise print your name instead of world. (Try your first and last name also) """

Challenges

# There are no challenges for this section. Feel free to move on to: 1. Data types and variables

Solutions




# Welcome to the first set of exercises """ ==== Exercise 1: add and remove comments ==== In the below example comment out the first line of code and uncomment the second, then run the file. """ # print("This shouldn't print") print("This should print") """ ==== Exercise 2: print multiple things ==== For this exercise we are going to print multiple things at once. To do this we can have each thing added to the print function as arguments (comma delimited). For example: print("Hello","World!") For this exercise print your name instead of world. (Try your first and last name also) """ print("Hello", "Kieran") # Hello + First name print("Hello", "kieran", "Wood") # Hello + First name + Last name