Short Introduction to Programming in Python

Last updated on 2023-06-13 | Edit this page

Estimated time: 35 minutes

Overview

Questions

  • How do I program in Python?
  • How can I represent my data in Python?

Objectives

  • Describe the advantages of using programming vs. completing repetitive tasks by hand.
  • Define the following data types in Python: strings, integers, and floats.
  • Perform mathematical operations in Python using basic operators.
  • Define the following as it relates to Python: lists, tuples, and dictionaries.

Interpreter


Python is an interpreted language which can be used in two ways:

  • “Interactively”: when you use it as an “advanced calculator” executing one command at a time. To start Python in this mode, execute python on the command line:

BASH

$ python

OUTPUT

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

Chevrons >>> indicate an interactive prompt in Python, meaning that it is waiting for your input.

PYTHON

2 + 2

OUTPUT

4

PYTHON

print("Hello World")

OUTPUT

Hello World
  • “Scripting” Mode: executing a series of “commands” saved in text file, usually with a .py extension after the name of your file:

BASH

$ python my_script.py

OUTPUT

Hello World

Introduction to variables in Python


Assigning values to variables

One of the most basic things we can do in Python is assign values to variables:

PYTHON

text = "Data Carpentry"  # An example of assigning a value to a new text variable,
                         # also known as a string data type in Python
number = 42              # An example of assigning a numeric value, or an integer data type
pi_value = 3.1415        # An example of assigning a floating point value (the float data type)

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 interpreter and press Return:

PYTHON

text

OUTPUT

"Data Carpentry"

Everything in Python has a type. To get the type of something, we can pass it to the built-in function type:

PYTHON

type(text)

OUTPUT

<class 'str'>

PYTHON

type(number)

OUTPUT

<class 'int'>

PYTHON

type(pi_value)

OUTPUT

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

PYTHON

print(text)

OUTPUT

Data Carpentry

PYTHON

print(number)

OUTPUT

42

This may seem redundant, but in fact it’s the only way to display output in a script:

example.py

PYTHON

# A Python script file
# Comments in Python start with #
# The next line assigns the string "Data Carpentry" to the variable "text".
text = "Data Carpentry"

# 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

BASH

$ python example.py

OUTPUT

Data Carpentry

Notice that “Data Carpentry” 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.

Tip: When editing scripts like example.py, be careful not to use word processors such as MS Word, as they may introduce extra information that confuses Python. In this lesson we will be using either Jupyter notebooks or the Spyder IDE, and for your everday work you may also choose any text editor such as Notepad++, VSCode, Vim, or Emacs.

Operators

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

PYTHON

2 + 2  # Addition

OUTPUT

4

PYTHON

6 * 7  # Multiplication

OUTPUT

42

PYTHON

2 ** 16  # Power

OUTPUT

65536

PYTHON

13 % 5  # Modulo

OUTPUT

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.

PYTHON

3 > 4

OUTPUT

False

PYTHON

True and True

OUTPUT

True

PYTHON

True or False

OUTPUT

True

PYTHON

True and False

OUTPUT

False

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

PYTHON

numbers = [1, 2, 3]
numbers[0]

OUTPUT

1

A for loop can be used to access the elements in a list or other Python data structure one at a time:

PYTHON

for num in numbers:
    print(num)

OUTPUT

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:

PYTHON

numbers.append(4)
print(numbers)

OUTPUT

[1, 2, 3, 4]

To find out what methods are available for an object, we can use the built-in help command:

OUTPUT

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 ().

PYTHON

# Tuples use parentheses
a_tuple = (1, 2, 3)
another_tuple = ('blue', 'green', 'red')

# Note: lists use square brackets
a_list = [1, 2, 3]

Tuples vs. Lists

  1. What happens when you execute a_list[1] = 5?
  2. What happens when you execute a_tuple[2] = 5?
  3. What does type(a_tuple) tell you about a_tuple?
  4. What information does the built-in function len() provide? Does it provide the same information on both tuples and lists? Does the help() function confirm this?
  1. What happens when you execute a_list[1] = 5?

The second value in a_list is replaced with 5.

  1. What happens when you execute a_tuple[2] = 5?

ERROR

TypeError: 'tuple' object does not support item assignment

As a tuple is immutable, it does not support item assignment. Elements in a list can be altered individually.

  1. What does type(a_tuple) tell you about a_tuple?

OUTPUT

<class 'tuple'>

The function tells you that the variable a_tuple is an object of the class tuple.

  1. What information does the built-in function len() provide? Does it provide the same information on both tuples and lists? Does the help() function confirm this?

PYTHON

len(a_list)

OUTPUT

3

PYTHON

len(a_tuple)

OUTPUT

3

len() tells us the length of an object. It works the same for both lists and tuples, providing us with the number of entries in each case.

PYTHON

help(len)

OUTPUT

Help on built-in function len in module builtins:

len(obj, /)
    Return the number of items in a container.

Lists and tuples are both types of container i.e. objects that can contain multiple items, the key difference being that lists are mutable i.e. they can be modified after they have been created, while tuples are not: their value cannot be modified, only overwritten.

Dictionaries


A dictionary is a container that holds pairs of objects - keys and values.

PYTHON

translation = {'one': 'first', 'two': 'second'}
translation['one']

OUTPUT

'first'

Dictionaries work a lot like lists - except that you index them with keys. You can think about a key as a name or unique identifier for the value it corresponds to.

PYTHON

rev = {'first': 'one', 'second': 'two'}
rev['first']

OUTPUT

'one'

To add an item to the dictionary we assign a value to a new key:

PYTHON

rev['third'] = 'three'
rev

OUTPUT

{'first': 'one', 'second': 'two', 'third': 'three'}

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

PYTHON

for key, value in rev.items():
    print(key, '->', value)

OUTPUT

'first' -> one
'second' -> two
'third' -> three

or

PYTHON

for key in rev.keys():
    print(key, '->', rev[key])

OUTPUT

'first' -> one
'second' -> two
'third' -> three

Changing dictionaries

  1. First, print the value of the rev dictionary to the screen.
  2. Reassign the value that corresponds to the key second so that it no longer reads “two” but instead 2.
  3. Print the value of rev to the screen again to see if the value has changed.

PYTHON

print(rev)

OUTPUT

{'first': 'one', 'second': 'two', 'third': 'three'}
  1. and 3.

PYTHON

rev['second'] = 2
print(rev)

OUTPUT

{'first': 'one', 'second': 2, 'third': 'three'}

It can help to further demonstrate the freedom the user has to define values to keys in a dictionary, by showing another example with a value completely unrelated to the current contents of the dictionary, e.g.

PYTHON

rev[2] = "apple-sauce"
print(rev)

OUTPUT

{1: 'one', 2: 'apple-sauce', 3: 'three'}

Functions


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

PYTHON

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

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

OUTPUT

42

Key Points

  • Python is an interpreted language which can be used interactively (executing one command at a time) or in scripting mode (executing a series of commands saved in file).
  • One can assign a value to a variable in Python. Those variables can be of several types, such as string, integer, floating point and complex numbers.
  • Lists and tuples are similar in that they are ordered lists of elements; they differ in that a tuple is immutable (cannot be changed).
  • Dictionaries are data structures that provide mappings between keys and values.