This is a short introduction to the Python programing language for beginners.
Download and install the latest version of Python 3.x:
- MacOS: https://www.python.org/downloads/mac-osx/
- If using brew, simply run
brew install python
- If using brew, simply run
- Linux (generic source, you'll have to build it manually): https://www.python.org/downloads/source/
- Alternatively, install from your distro's package manager
- Windows: https://www.python.org/downloads/windows/
Python 3 broke backward compatibility with Python 2 programs when it was released. It introduced native unicode character support, and a slew of other changes to the core language. Due to the work involved in switching, many libraries used to only work on Python 2, but that has become far less of an issue over the last few years. You will still run into Python 2 being used in the industry or for certain libraries, but support for Python 2 is being discontinued in 2020. Unless you have a really strong reason to use Python 2, you should start all your new projects using Python 3.
Start the REPL interpreter with the command python
or python3
if you have both 2 and 3 installed.
>>> 4 + 4
8
>>> 55 - 10
45
>>> 2 * 4
8
>>> 4 / 2 # Division float
2.0 # Note the automatic conversion to a floating point number
>>> 5 / 2
2.5
>>> 5 // 2 # Division floor
2 # Note that an integer type is returned
>>> 7 % 4 # Modulo
3
>>> 3 == 3 # Check for equality
True # All boolean constants are capitalized.
>>> 3 != 3 # Check for inequality
False
>>> 2 < 3 # Less than
True
>>> 2 > 3 # Greater than
False
>>> 3 != 3 or 3 == 3 # OR operator
True
>>> 'abc' == 'abc' and 'cdb' == 'abc' # AND operator (note that string literals can be wrapped in either single or double quotes)
False
>>> not 'abc' == 'abc'
False
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Float Division |
// | Floor Division |
% | Modulus |
Operator | Description |
---|---|
> | Greater than |
< | Less than |
== | Equal to |
!= | Not equal to |
>= | Greater than or equal |
<= | Less than or equal |
Operator | Description |
---|---|
and | AND operator |
or | OR operator |
not | Negation |
Full List (Including Bitwise and Assignment Operators)
Lists are mutable ordered data structures of all the same type.
>>> l = ['a', 'b', 'c'] # Variable l is now bound to the list object
>>> l[0] # Lists are zero-indexed
'a'
>>> l[2]
'c'
>>> l.append('e') # Adds to end of list
>>> l
['a', 'b', 'c', 'e']
>>> l[3] = 'd' # Lists are mutable.
>>> l
['a', 'b', 'c', 'd']
>>> l.append('e')
>>> l
['a', 'b', 'c', 'd', 'e']
>>> l[1:3] # Slicing lists
['b', 'c']
>>> l[:4]
['a', 'b', 'c', 'd']
>>> l[2:]
['c', 'd', 'e']
>>> l.pop(2)
'c'
>>> l
['a', 'b', 'd']
>>> l.remove('b')
>>> l
['a', 'd']
>>> l.insert(1, 'b')
>>> l
['a', 'b', 'd']
>>> l + ['e', 'f', 'g']
['a', 'b', 'd', 'e', 'f', 'g']
Tuples are non-mutable ordered data structures of either the same or mixed types.
>>> t = (1, 2, 'a')
>>> t
(1, 2, 'a')
>>> t[1]
2
>>> x, y, z = t # Tuple unpacking
>>> x
1
>>> y
2
>>> z
'a'
Dictionaries are maps of keys to values. The types of the keys and the types of the values can be different. Note that dictionaries are unordered.
>>> d = {'a': 1, 'b': 2, 'c': 3} # You can also create an empty dictionary with dict()
>>> d
{'a': 1, 'b': 2, 'c': 3}
>>> d['b']
2
>>> d['c'] = 5
>>> d
{'a': 1, 'b': 2, 'c': 5}
>>> d['d'] = 4
>>> d
{'a': 1, 'b': 2, 'c': 5, 'd': 4}
>>> del d['c']
>>> d
{'a': 1, 'b': 2, 'd': 4}
>>> d.keys()
dict_keys(['a', 'b', 'd'])
>>> d.values()
dict_values([1, 2, 4])
>>> d.items()
dict_items([('a', 1), ('b', 2), ('d', 4)]) # Note this is iterable, see below how to use it
>>> d.pop('d')
4
>>> d
{'a': 1, 'b': 2}
>>> 'a' in d
True
>>> 'd' in d
False
>>> 'a' not in d
False
>>> if 'a' in d:
... print(d['a']) # Each line in the if statement needs to start with a tab. Python uses white space instead of braces
... # Note that the dots indicate the interpreter is waiting for further input before executing your code.
1
>>> if 'd' in d:
... print(d['d'])
... else:
... print('Not here!')
...
Not here!
>>> if 'd' in d:
... print(d['d'])
... elif 'b' in d: # elif is else if in other languages
... print(d['c'])
... else:
... print('Not here!')
...
2
>>> l = ['a', 'b', 'c', 'd']
>>> for item in l:
... print(item)
a
b
c
d
>>> d = {'a': 1, 'b': 2, 'c': 3}
>>> for k, v in d.items():
... print(k + ': ' + str(v)) # Note the conversion from an int to a string using the str function
a: 1
b: 2
c: 3
Python has no support for the traditional index based for-loops (think for (i = 0; i < x; i++)
style for-loop). It is very rare that you will need this method, but it is still useful if you are manipulating the index as you run through the loop. To simulate this, you can use the range function:
>>> for i in range(len(l) - 1):
... print(l[i]) # Print every element except the last one (normally in this case you'd just slice the list, but just take this as an example)
a
b
c
But what if you just want the index of the item? You can use the enumerate function:
>>> for i, v in enumerate(l):
... print(str(i) + ' ' + v)
0 a
1 b
2 c
List comprehensions are a special type of for loop that can transform one list into another. They are useful replacements for the map
and filter
functions you'd find in a language like Racket (note that python does include these functions, as well as lambdas, but list comprehensions are the preferred method)
>>> l = [2, 1, 3, 1, 5, 6]
>>> [x*2 for x in l] # Multiply each value by two
[4, 2, 6, 2, 10, 12]
>>> [x for x in l if x == 1] # Filter the list down to just the values equal to 1
[1, 1]
Python has while loops as well.
>>> x = 0
>>> while x < 4:
... print(x) # Again, there is a built in function for this (range), but this is an easy example
... x += 1 # Note the assignment operator (there is no x++)
...
0
1
2
3
Python does have break
, continue
, and pass
keywords for controlling the flow of loops. You can read more about these, as well as read the full documentation about flow control here:
Functions in python are straight forward and defined by the def
keyword. Functions have their own scope.
>>> def addThreeVars(x, y, z):
... return x + y + z
...
>>> addThreeVars(1, 2, 3)
>>> 6
The REPL is great for testing out small pieces of code, but we need to put our code in actual files if we want to use the language for real!
- Create a new file called
hello.py
in your favorite text editor - Type the following in the file:
def say_hello(name): # Note that python functions should be all lower case, separated by underscores
print('Hello ' + name)
say_hello('John')
- Save the file
- To run the file, simply call
python3 hello.py
from the command line (Don't forget tocd
to the correct directory!)
You should see the output: Hello John
Note that everything written in the file will be executed from top to bottom in this case.
Now, let's say we want to import our function into another file or the REPL environment so we can use it over and over again.
- Remove the
say_hello('John')
line from your file - Open the python REPL by running
python3
- Type
import hello
- Now call your function using
hello.say_hello('Jack')
- You should see
Hello Jack
In this case, the name of the file you just imported (hello
) is the root module. The function say_hello
is attached to this module, thus why you have to call hello.say_hello()
.
Note that there is also now a folder called __pycache__
in the same directory as your hello.py
file. If you list this directory, you should see a .pyc
file. Python is an interpreted language, but the interpreter will compile modules into bytecode files to be reused later. This happens automatically when you first load the module, and when you change the source, the .pyc
file will be recompiled. You should not commit the .pyc
files to source control (e.g your git repo).
If you just want to import a specific item or items from a module, you can use the from . import .
syntax:
- Open the python REPL by running
python3
- Type
from hello import say_hello
- Now call
say_hello('George')
- You should see
Hello George
In python, everything is actually an object. Strings for example have their own methods (such as split). You can easily make your own classes!
- Create a new file called
person.py
- Enter the following:
class Person():
<<<<<<< HEAD
def __init__(self, first, middle, last): # Note that "self" is always passed in as the first argument to every one of the class's functions. This is how you access the class's member functions and variables
=======
def __init__(self, first, middle, last): # Note that "this" is always passed in as the first argument to every one of the class's functions. This is how you access the class's member functions and variables
>>>>>>> 61d6556a77e51952367bb9a2b8b0207119fcfbb7
self.first = first
self.middle = middle
self.last = last
def get_full_name(self):
return self.first + ' ' + self.middle + ' ' + self.last
- Open the REPL
- Run
from person import Person
- To create a Person object and assign it to the variable
p
, runp = Person('John', 'H', 'Doe')
- Now you can call
p.get_full_name()
- You should see
John H Doe
We can combine this with our say_hello()
function. Just run say_hello(p.get_full_name())
.
Python supports full inheritance.
- Below your
Person
class, type following in yourperson.py
file:
class Doctor(Person):
def get_full_name(self):
return "Dr. " + super().get_full_name()
- Now in the REPL, run
from person import Doctor
- Run
doc = Doctor('Colin', 'H', 'Kelly')
- Try
doc.get_full_name()
- You should see
Dr. Colin H Kelly
There are many useful libraries out there for python. Most, if not all, of these are accessible by using python's built in package manager: pip
. To install a library globally on your python 3 installation, simply run pip3 install library_name
for example, if we wanted to install matplotlib (a useful library for creating visual graphs and charts), you'd run pip3 install matplotlib
. You can uninstall packages by running pip3 uninstall matplotlib
.
Another useful feature of pip is requirements.txt files. If your project includes a bunch of dependencies you want to distribute, you can list them in a requirements.txt file. This allows the people who download your project to quickly install all the dependencies they need to run it. Find out more here.
Aside from pip, another important tool is virtualenv. This tool lets you create separate, isolated, python installations. This allows you to manage the dependencies for each of your projects individually, instead of having to install everything at the global level. It is best practice to use a new virtualenv for each of your projects. See here for more info.