Python Language Rules: Modules 1 and
2
Introduction to Python and How to Design Functions
This page provides a summary of the programming language used in this module. In the Introduction
to Python module we will only learn a subset of the Python language. (In fact, in this entire course
we'll just learn a subset of the Python language.) As we move through the course, the different
language rules in this module show see the growing subset of the Python language that we have
learned.
Values
Numbers ( float or int in Python) 0 , -12 , 3.4 , 9.88
"hello", "Once upon a time..." , 'UBC' (strings may use single
Strings ( str in Python)
or double quotes)
Booleans ( bool in Python) True , False
None (special type used for missing
None (this is the only value of this type)
data values)
Variables
A variable is a name that refers to a value. As programmers, we want to choose meaningful variable
names so that anyone who reads our program can easily understand what value the variable refers
to. We'll see example variables in the Forming Statements section below.
Primitive Operators
Primitive operators are operators that are built in to the Python programming language.
Operators that operate on numbers:
Operator Explanation Examples
+ addition 2 + 3 evaluates to 5
- subtraction 10 - 6 evaluates to 4
* multiplication 2 * 3 evaluates to 6
/ division 6 / 2 evaluates to 3
remainder 15 % 4 evaluates to 3 because the remainder when dividing 15 by 4
%
("modulo") is 3
** exponentiation 2 ** 3 evaluates to 8 (2 to the power of 3)
> greater than 3 > 2 evaluates to True , 2 > 3 evalutaes to False
< less than 2 < 3 evaluates to True , 3 < 2 evaluates to False
greater than or 3 >= 2 evaluates to True , 3 >= 3 evaluates to True , 2 >=
>=
equal 3 evaluates to False
2 <= 3 evaluates to True , 2 <= 2 evaluates to True , 3 <=
<= less than or equal
2 evaluates to False
== equality 2 == 2 evaluates to True , 2 == 3 evaluates to False
!= inequality 2 != 3 evaluates to True , 2 != 2 evaluates to False
Operators that operate on strings:
Operator Explanation Examples
evaluates to 'Hello, UBC' (NOTE: the
'Hello, ' + 'UBC'
+ concatenates strings space at the end of the first string is still present after
concatenation)
* repetition 'hello'*2 evalutes to 'hellohello'
access expression: evaluates 'hello'[0] evaluates to 'h' , 'hello'[1] evaluates
[] to the character at the given to 'e' , and 'hello'[-1] evaluates to 'o' (last
index character in the string)
[a:b:c] access expression: evaluates
to the substring that starts at
the first index and ends before [a:b:c]means count in increments of c by starting a
the second index and going up to, but not including, b. You do not always
need a c value.
For example:
' hello'[0:2] evaluates
to 'he' and 'hello'[1:4] evaluates to 'ell' .
' hello world'[0:10:2] evaluates
to 'hlowr' and 'hello world'[1:8:3] evaluates
to 'eoo' .
If c is negative, you will increment through the string
backwards. For example:
' hello world'[::-1] evaluates to 'dlrow olleh'
'hello world'[10:0:-1] evaluates to 'dlrow olle'
'hello world'[10:0:-2] evaluates to 'drwol'
membership: evaluates
'el' in 'hello' evalutes to True and 'elp' in
in to True if the first string is a
'hello' evaluates to False
substring of the second
membership: evaluates
'el' not in 'hello' evalutes to False and 'elp' not in
not in to True if the first string is not a
'hello' evaluates to True
substring of the second
Operators that operate on booleans:
Operator Explanation Examples
True and True evaluates to True
True and False evaluates to False
and evalutes to True if both operands are True
False and True evaluates to False
False and False evaluates to False
True or True evaluates to True
True or False evaluates to True
or evalutes to True if either operand is True
False or True evaluates to True
False or False evaluates to False
not True evaluates to False
not evaluates to the negation of the operand
not False evaluates to True
There is also a useful primitive function that returns the length of a string. (In fact, we'll see other
ways to use this useful function later in the term.) len returns the length of a string For
example, len('hi') returns 2, len('UBC') returns 3, len("") returns 0
Operators for None:
Use x is None to test if x has the value None or x is not None to test if it does not. (None is
None evaluates to True and anything else compared to None with is evaluates to False.)
Primitive Functions
Primitive functions are functions that are built in to the Python programming language. There are
many such functions, so we just highlight a few common ones here.
Function Explanation Examples
len returns the length of its input len("UBC") evaluates to 3
max returns the largest item from its input max(1,2) returns 2 , max(10.2, -1) returns 10.2
min returns the smallest item from its input min(1,2) returns 1 , min(10.2, -1) returns -1
Forming Expressions
Expressions evaluate to a single value. They may contain operations (e.g. addition, subtraction,
string concatentation, any of the boolean operations).
Forming Statements
A statement is a piece of code that can be executed by the Python interpreter. There many are
different kinds of statements.
An expression statement contains an expression (as defined above).
Examples:
2
"Hi"
2 + 8
An assignment statement is used to set (or reset) the value that a variable refers to.
Examples:
# defines a variable named n and sets its value to 2
n = 2
# defines a variable named location and sets its value to "Vancouver"
location = "Vancouver"
# resets the value of location to "4th and Alma"
location = "4th and Alma"
An if statement is a statement that will execute one of two blocks of code, depending on the
value of the given condition.
Example:
individual_cost = 5
discount = 0
if n > 10:
discount = 5
else:
discount = 0
cost = (n * individual_cost) - discount
A return statement returns the value (or None) from a function call. Return statements may
only be used inside function definitions. When a return statement is executed in Python, it
returns to the function's caller immediately and no further lines of code in the function
are executed.
Example:
def prepend_hi(s):
return "Hi, " + s
# Anything after the return executes will not be executed!