Back to Module 1: Running Code

Python by Example

Textbook chapters 1, 2

Expressions, statements, evaluation

Expressions instruct the program to perform an instruction.

Arithmetic operators act as you would expect. They require two numbers.

>>> 16 + 17
33
>>> 3 - 8
-5
>>> 2 * -3
-6
>>> 14 / 5
2.8

Expressions can be chained together. One or more expressions on a line is called a statement.

Arithmetic statements follow the order-of-operations you would expect, complete with parenthesis for grouping.

>>> 2 + 3 + 4 + 5
14
>>> 2 * (3 + 5)
16
>>> -2 * 3 + 4
-2

Python also has operators for things other than numbers. A string is a sequence of characters, identified by quotes ("" or '') around them. We use the + operator to instruct the program to concatenates two strings.

>>> "Hi" + "there" + "bob"
'Hitherebob'
>>> "Hi " + "there " + "bob"
'Hi there bob'

Attempting to call an operation on mismatching data causes an error:

>>> "Hello" + 45
Traceback (most recent call last):
  File "<pyshell#65>", line 1, in <module>
    "Hello" + 45
TypeError: Can't convert 'int' object to str implicitly

In all these examples, we say that each expression evaluates to a value. 5 + 7 evaluates to 12. 2 + 3 + 4 evaluates 2 + 3 first, then 5 + 4.

Variables and assignment

Variables store data. They are like named boxes. We call the contents of the box its value

You tell Python to store a value in a variable (and create it if necessary) using an assignment statement.

Variable names are made of letters, numbers and the underscore _. They are case-sensitive. The name should reflect what you plan to store.

>>> name = "emmett"
>>> age = 42
>>> cost = 16.45
>>> number_of_people = 76
>>> universityName = 'UT Rio Grande Valley'

In spite of using the = sign, assignment is not like equality in math. You can reassign a variable multiple times and it replaces the value.

>>> age = 9
>>> age = 10
>>> age = 11

Variables evaluate to their value. This means that they can be used in expressions anywhere you would use data. We call that non-variable data we were using literal data.

>>> universityName
'UT Rio Grande Valley'
>>> age
42
>>> 7 + age
49
>>> cost * 4
65.8
>>> count = 5
>>> count = count + 1
>>> count = count + 1
>>> count
7

Trying to use a variable that hasn't been created yet causes an error.

>>> bad_variable
Traceback (most recent call last):
  File "<pyshell#75>", line 1, in <module>
    bad_variable
NameError: name 'bad_variable' is not defined

Functions, parameters and return values

Operators are one way to name actions that Python can execute. Functions are another. Functions are called by name with parenthesis () after.

>>> print()

Sometime functions need to be given more information, like what to print. We call these pieces of information parameters. We say that a parameter is passed into a function.

Multiple parameters for a function are separated by commas. What a function does with its parameters is up to that function. print just prints them all, separated by commas.

Expressions can be used as parameters too - they are evaluated and the resulting value is passed into the function.

Variables can be used as parameters too - they evaluate to their value, which is passed into the function.

>>> print("Hi there")
Hi there
>>> print(17)
17
>>> print(17, 18, 19)
17 18 19
>>> print(17 + 18 + 19)
54
>>> print(age)
42
>>> print(age + 5)
47
>>> print("I am", 42, "years old")
I am 42 years old

The input() function tells Python to wait for the user to type a sequence of characters and hit return.

>>> input()
This is what I type
'This is what I type'

Note that after I typed, the string that I typed appeared again. This is because functions can evaluate to a value just like operations and variables. 7 + 2 evaluates to 9. input() evaluates to whatever the user typed. Notice the same mechanics at work:

>>> height = 6.3
>>> print('The height is:', height)
The height is: 6.3
>>> user_input = input()
this is me the user typing a bunch of stuff
>>> print('The input is:', user_input)
The input is: this is me the user typing a bunch of stuff

Data types and conversion functions

Data in Python has a type. 6 is an whole number, which computers store as an int. 6.3 is a real number, which computers store a floating point or float. 'Emmett' is a string. These are all different things to the machine.

>>> 56 + 32
88
>>> '56' + '32'
'5632'

There are functions to convert from one type to another. Each function take data of one type and evaluates to the equivalent data of another type.

>>> str(56)
'56'
>>> int('56')
56
>>> int(5.6)
5
>>> float(45)
45.0

At the moment this is relevant to you because input() returns a string (sequence of characters), which you may have to convert to a number.

>>> number = input()
33.1
>>> number + 2
Traceback (most recent call last):
  File "<pyshell#84>", line 1, in <module>
    number + 2
TypeError: Can't convert 'int' object to str implicitly
>>> number = input()
33.1
>>> number = float(number) + 2
>>> print(number)
35.1