Variables and Types
Using variables to store values
Variables are names which refer to stored values.
In Python, the
=
symbol assigns the value on the right to the name on the left. The variable is created when a value is assigned to it.
Here, Python assigns an age to a variable age
and a name in quotes to a variable first_name
.age = 42 first_name = 'Ahmed'
- Variable names in Python
- can only contain letters, digits, and underscore
_
(typically used to separate words in long variable names) - cannot start with a digit
- are case sensitive (
age
,Age
andAGE
are three different variables)
- Variable names starting with underscores like
__alistairs_real_age
have special meaning so we won't do that until we understand the convention.
Use print
to display values
Python has a built-in function called
print
that produces a printout as text to the terminal.
Like any function, we can call print (i.e., tell Python to run it) by using its name while providing values to the function (i.e., the things to print) in parentheses.
To add a string to the printout, wrap the string in single or double quotes
The values passed to the function are called arguments (or args for short).print(first_name, 'is', age, 'years old')
Ahmed is 42 years old
print
automatically puts a single space between items to separate them
and wraps around to a new line at the end.Variables must be created before they are used
If a variable doesn't exist yet, or if the name has been mis-spelled, Python reports an error (unlike some languages, which "guess" a default value)
print(last_name)
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'last_name' is not defined
- The last line of an error message is usually the most informative
- We will look at error messages in detail later
Variables can be used in calculations
- We can use variables in calculations just as if they were values.
- Remember, we assigned the value
42
toage
a few lines ago.
print('Age in three years:', age + 3)
Age in three years: 45
Variables can be replaced
- We can replace the value associated with a variable by assigning it a new one.
- Replacing a variable permanently deletes the old value.
age = age + 3 print('Age in three years:', age)
Age in three years: 45
Use an index to get a single character from a string
- An item in a list is called an element. Whenever we treat a string as if it were a list, the string's elements are the individual characters.
- The characters (individual letters, numbers, and so on) in a string are ordered. For example, the string
'AB'
is not the same as'BA'
. Because of this ordering, we can treat the string as a list of characters.- We will look at lists in python more generally later
- Each position in the string (first, second, etc.) is given a number. This number is called an index or sometimes a subscript.
- Indices are numbered from 0.
- Use the position's index in square brackets to get the character at that position.
atom_name = 'helium' print(atom_name[0])
h
Use a slice to get a substring
A slice is subpart of a string (or, more generally, any list-like thing).
When a slice is taken of a string, this is called a substring.
These substrings can be as short as a single character.
We can take a slice with
[start:stop]
, where start
is the index of the first element we want to select and stop
is the index of the element after the last element.
Mathematically, you might say that a slice selects [start:stop)
or that the bounds are inclusive on the left and exclusive on the right.
The difference between stop
and start
is the slice's length.
Taking a slice does not change the contents of the original string,
instead, the slice is a copy of part of the original string.atom_name = 'sodium' print(atom_name[0:3])
sod
Use the built-in function len
to find the length of a string
print(len('helium'))
6
Here we have nested two function calls,
len
and print
,
nested functions are evaluated from the inside out, like in mathematics.Python is case-sensitive
Python treats upper- and lower-case characters as distinct:
Name
andname
are different variables.
There are conventions for when to use upper-case letters at the start of variable names; we will stick to strictly lower-case characters for now.
Use meaningful variable names
Python doesn't care what you call variables as long as they obey the rules (alphanumeric characters and the underscore).
flabadab = 42 ewr_422_yY = 'Ahmed' print(ewr_422_yY, 'is', flabadab, 'years old')
However, code is supposed to be read by other humans:
- Use meaningful variable names to help other people understand what the program does.
- The most important "other person" is your future self.
Swapping Values
Fill the table showing the values of the variables in this program _after* each statement is executed.
# Command # Value of x # Value of y # Value of swap # x = 1.0 # # # # y = 3.0 # # # # swap = x # # # # x = y # # # # y = swap # # # #
Slicing practice
What does the following program print?
atom_name = 'carbon' print('atom_name[1:3] is:', atom_name[1:3])
Slicing concepts
- What does
thing[low:high]
do? - What does
thing[low:]
(without a value after the colon) do? - What does
thing[:high]
(without a value before the colon) do? - What does
thing[:]
(just a colon) do? - What does
thing[number:some-negative-number]
do? - What happens when you choose a
high
value which is out of range? (i.e., tryatom_name[0:15]
)
Every value has a type
In programming a "type" is a method of categorising like data which share characteristics, representations, common operations, etc.
Every value in a program has a specific type:
- Integer (
int
): represents positive or negative whole numbers like 3 or -512. - Floating point number (
float
): represents real numbers like 3.14159 or -2.5. - Character string (usually called "string",
str
): text.- Written in either single quotes or double quotes (as long as they match).
- The quote marks aren't printed when the string is displayed.
Use the built-in function type
to find the type of a value
We can use the built-in function
type
to find out what type a value has, this works on variables as well.- Remember: it is the value which has a type --- the variable name is just a label.
print(type(52))
<class 'int'>
fitness = 'average' print(type(fitness))
<class 'str'>
Types control what operations (or methods) can be performed on a given value
A value's type determines what the program can do to it.
print(5 - 3)
2
print('hello' - 'h')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-2-67f5626a1e07> in <module>() ----> 1 print('hello' - 'h') TypeError: unsupported operand type(s) for -: 'str' and 'str'
You can use the "+" and "*" operators on strings
"Adding" character strings concatenates them.
full_name = 'Ahmed' + ' ' + 'Walsh' print(full_name)
Ahmed Walsh
Multiplying a character string by an integer N creates a new string that consists of that character string repeated N times. Since multiplication is repeated addition.
separator = '=' * 10 print(separator)
==========
Strings have a length (but numbers don't)
The built-in function
len
counts the number of characters in a string.print(len(full_name))
11
But numbers don't have a length (not even zero).
print(len(52))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-f769e8e8097d> in <module>() ----> 1 print(len(52)) TypeError: object of type 'int' has no len()
Must convert numbers to strings or vice versa when operating on them
Cannot add numbers and strings.
print(1 + '2')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-fe4f54a023c6> in <module>() ----> 1 print(1 + '2') TypeError: unsupported operand type(s) for +: 'int' and 'str'
Is not allowed in python because it is ambiguous: should
1 + '2'
be 3
or '12'
? Some types can be converted to other types by using the type name as a function.print(1 + int('2')) print(str(1) + '2')
3 12
Can mix integers and floats freely in operations
Integers and floating-point numbers can be mixed in arithmetic. Python 3 automatically converts integers to floats as needed.
print('half is', 1 / 2.0) print('three squared is', 3.0 ** 2)
half is 0.5 three squared is 9.0
Variables only update on assignment
If we make one cell in a spreadsheet depend on another, and update the latter, the former updates automatically. This does not happen in programming languages.
first = 1 second = 5 * first first = 2 print('first is', first, 'and second is', second)
first is 2 and second is 5
The computer reads the value of
first
when doing the multiplication, creates a new value, and assigns it to second
.
After that, second
does not remember where it came from.Automatic Type Conversion
What type of value is 3.25 + 4?
Choose a Type
What type of value (integer, floating point number, or character string) would you use to represent each of the following? Try to come up with more than one good answer for each problem. For example, in # 1, when would counting days with a floating point variable make more sense than using an integer?
- Number of days since the start of the year.
- Time elapsed from the start of the year until now in days.
- Serial number of a piece of lab equipment.
- A lab specimen's age.
- Current population of a city.
- Average population of a city over time.
Division Types
In Python 3, the
//
operator performs integer (whole-number) floor division, the /
operator performs floating-point
division, and the %
(or modulo) operator calculates and returns the remainder from integer division:print('5 // 3:', 5 // 3) print('5 / 3:', 5 / 3) print('5 % 3:', 5 % 3)
5 // 3: 1 5 / 3: 1.6666666666666667 5 % 3: 2
If
num_subjects
is the number of subjects taking part in a study, and num_per_survey
is the number that can take part in a single survey, write an expression that calculates the number of surveys needed to reach everyone once.