Skip to content

Latest commit

 

History

History
267 lines (206 loc) · 5.61 KB

00-short-introduction-to-Python.md

File metadata and controls

267 lines (206 loc) · 5.61 KB
layout root title
lesson
.
Short Introduction to Programming in Python

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
  • "Batteries Included" philosophy - libraries for common tasks available in standard installation
  • Supports multiple programming paradigms
  • Very large community

Interpreter

Python is an interpreted language. As a consequence, we can use it in two ways:

  • Using interpreter as an "advanced calculator" in interactive mode:
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
  • Executing programs/scripts saved as a text file, usually with *.py extension:
user:host:~$ python my_script.py
Hello World

Introduction to Python built-in data types

Strings, integers and floats

The most basic data types in Python are strings, integers and floats:

text = "Data Carpentry"
number = 42
pi_value = 3.1415

Here we've assigned data to variables, namely text, number and pi_value, using the assignment operator =. The variable called text is a string which means it can contain letters and numbers. We could reassign the variable text to an integer too - but be careful reassigning variables as this can get confusing.

To print out the value stored in a variable we can simply type the name of the variable into the interpreter:

>>> text
"Data Carpentry"

however, in scripts we must use the print function:

# Comments start with #
# Next line will print out text
print(text)
"Data Carpentry"

Operators

We can perform mathematical calculations in Python using the basic operators +, -, /, *, %:

>>> 2 + 2
4
>>> 6 * 7
42
>>> 2 ** 16  # power
65536
>>> 13 % 5  # modulo
3

We can also use comparison and logic operators: <, >, ==, !=, <=, >= etc. and, or, not

>>> 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 a sequence of elements. Each element can be accessed by an index:

>>> 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:

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. This is Python's way of marking a block of code. We will discuss this in more detail later.

To add elements to the list, we can use the append method:

>>> numbers.append(4)
>>> print(numbers)
[1,2,3,4]

Methods are a way to interact with an object - like a list. We can use or apply a method to a variable or element using the dot .. To find out what methods are available, 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
 ...

We can also access a list of methods using dir. Some methods names are surrounded by double underscores. Those methods are called "special", and usually we access them in a different way. For example __add__ method is responsible for the + operator.

dir(numbers)
>>> dir(numbers)
['__add__', '__class__', '__contains__', ...]

Tuples

A tuple is similar to a list in that it's a 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 paratheses
ATuple= (1,2,3)
anotherTuple = ('blue','green','red')
# notes lists uses square brackets
AList = [1,2,3]

Challenge

  1. What happens when you type ATuple[2]=5 vs AList[1]=5 ?
  2. Type type(ATuple) 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}
...
TypeError: unhashable type: 'list'

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'}

Using for loops with dictionaries is a little more complicated. We can do this in two ways:

>>> for key, value in rev.items():
...     print(key, "->", value)
...
1 -> one
2 -> two
3 -> three

or

>>> for key in rev.keys():
...     print(key, "->", rev[key])
...
1 -> one
2 -> two
3 -> three
>>>

Functions

Defining part of a program in Python as a function is done using the def keyword. For example a function that takes two arguments and returns their sum can be defined as:

def add_function(a, b):
    result = a + b
    return result

z = add_function(20, 22)
print(z)
42

Key points here:

  • definition starts with def
  • function body is indented
  • return keyword precedes returned value