WORKSHOPS
01 Programming in Python (Introduction)
The Basics of Python
Python is a general purpose programming language that supports rapid development of scripts and applications.
Python’s main advantages:
- Open Source software, supported by Python Software Foundation
- Available on all platforms (ie. Windows, Linux and MacOS)
- It is a general-purpose programming language
- Supports multiple programming paradigms
- Very large community with a rich ecosystem of third-party packages
Interpreter
Python is an interpreted language which can be used in two ways:
- “Interactive” Mode: It functions like an “advanced calculator” Executing one command at a time:
user:host:~$ python
Python 3.5.1 (default, Oct 23 2015, 18:05:06)
[GCC 4.8.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 2
4
>>> print("Hello World")
Hello World
- “Scripting” Mode: Executing a series of “commands” saved in text file,
usually with a
.py
extension after the name of your file:
user:host:~$ python my_script.py
Hello World
Introduction to Python built-in data types
Strings, integers and floats
One of the most basic things we can do in Python is assign values to variables:
text = "Data Fluency" # An example of a string
number = 42 # An example of an integer
pi_value = 3.1415 # An example of a float
Here we’ve assigned data to the variables text
, number
and pi_value
,
using the assignment operator =
. To review the value of a variable, we
can type the name of the variable into the Jupyter notebook and press Shift and Enter:
text
## Which Returns
"Data Fluency"
Everything in Python has a type. To get the type of something, we can pass it
to the built-in function type
:
type(text)
str
type(number)
int
type(6.02)
float
The variable text
is of type str
, short for “string”. Strings hold
sequences of characters, which can be letters, numbers, punctuation
or more exotic forms of text (even emoji!).
We can also see the value of something using another built-in function, print
:
print(text)
Data Fluency
print(11)
11
This may seem redundant, but in fact it’s the only way to display output in a script:
example.py
# A Python script file
# Comments in Python start with #
# The next line assigns the string "Data Carpentry" to the variable "text".
text = "Data Fluency"
# The next line does nothing!
text
# The next line uses the print function to print out the value we assigned to "text"
print(text)
Running the script
$ python example.py
Data Fluency
Notice that “Data Fluency” is printed only once.
Tip: print
and type
are built-in functions in Python. Later in this
lesson, we will introduce methods and user-defined functions. The Python
documentation is excellent for reference on the differences between them.
help(print)
Will give the output
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Operators
We can perform mathematical calculations in Python using the basic operators
+, -, /, *, %
:
>>> 2 + 2 # Addition
4
>>> 6 * 7 # Multiplication
42
>>> 2 ** 16 # Power
65536
>>> 13 % 5 # Modulo
3
We can also use comparison and logic operators:
<, >, ==, !=, <=, >=
and statements of identity such as
and, or, not
. The data type returned by this is
called a boolean.
>>> 3 > 4
False
>>> True and True
True
>>> True or False
True
Sequential types: Lists and Tuples
Lists
Lists are a common data structure to hold an ordered sequence of elements. Each element can be accessed by an index. Note that Python indexes start with 0 instead of 1:
>>> numbers = [1, 2, 3]
>>> numbers[0]
1
A for
loop can be used to access the elements in a list or other Python data structure one at a time. We will learn about loops in other lesson.
>>> for num in numbers:
... print(num)
...
1
2
3
Indentation is very important in Python. Note that the second line in the
example above is indented. Just like three chevrons >>>
indicate an
interactive prompt in Python, the three dots ...
are Python’s prompt for
multiple lines. This is Python’s way of marking a block of code. [Note: you
do not type >>>
or ...
.]
To add elements to the end of a list, we can use the append
method. Methods
are a way to interact with an object (a list, for example). We can invoke a
method using the dot .
followed by the method name and a list of arguments
in parentheses. Let’s look at an example using append
:
>>> numbers.append(4)
>>> print(numbers)
[1, 2, 3, 4]
>>>
To find out what methods are available for an
object, we can use the built-in help
command:
help(numbers)
Help on list object:
class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
...
Tuples
A tuple is similar to a list in that it’s an ordered sequence of elements.
However, tuples can not be changed once created (they are “immutable”). Tuples
are created by placing comma-separated values inside parentheses ()
.
# Tuples use parentheses
a_tuple= (1, 2, 3)
another_tuple = ('blue', 'green', 'red')
# Note: lists use square brackets
a_list = [1, 2, 3]
Challenge - Tuples
- What happens when you type
a_tuple[2]=5
vsa_list[1]=5
? - Type
type(a_tuple)
into python - what is the object type?
Dictionaries
A dictionary is a container that holds pairs of objects - keys and values.
>>> translation = {'one': 1, 'two': 2}
>>> translation['one']
1
Dictionaries work a lot like lists - except that you index them with keys. You can think about a key as a name for or a unique identifier for a set of values in the dictionary. Keys can only have particular types - they have to be “hashable”. Strings and numeric types are acceptable, but lists aren’t.
>>> rev = {1: 'one', 2: 'two'}
>>> rev[1]
'one'
>>> bad = {[1, 2, 3]: 3}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
In Python, a “Traceback” is an multi-line error block printed out for the user.
To add an item to the dictionary we assign a value to a new key:
>>> rev = {1: 'one', 2: 'two'}
>>> rev[3] = 'three'
>>> rev
{1: 'one', 2: 'two', 3: 'three'}
Challenge - Can you do reassignment in a dictionary?
First check what
rev
is right now (rememberrev
is the name of our dictionary). Type: revTry to reassign the second value (in the key value pair) so that it no longer reads “two” but instead reads “apple-sauce”.
Now display
rev
again to see if it has changed.
It is important to note that dictionaries are “unordered” and do not remember the sequence of their items (i.e. the order in which key:value pairs were added to the dictionary). Because of this, the order in which items are returned from loops over dictionaries might appear random and can even change with time.