Python I: Operators and Variable types
Object-oriented programming
Python is an object-oriented programming (OOP) language. Simply put, this means that data structures (e.g. variables) have certain properties, in particular attributes and methods. Attributes are usually descriptive features of the object, and methods are certain actions (functions) you can perform on the object itself. For example, consider a book. Books are physical objects with certain attributes, such as number of pages, number of words, dimensions (width, height, depth), cover art, etc. Similarly, there are several actions (methods) that we can do with books like reading, writing, throwing at people we hate, and sharing with people we love. A given realization of an object is called an instance. To continue with the book metaphor, “The Count of Monte Christo” and “Harry Potter” are each instances of book objects.
While these concepts might seem abstract, python syntax and behavior will make much more sense in light of the OOP paradigm upon which the language is based. In particular, attributes and methods are accessed in systematic ways, as follows:
As you see, attributes and methods are called after a `. at the end of the instance name. Unlike attributes, however, methods end with parentheses. Sometimes, methods can take particular arguments which relate to their function, for example when we threw the book at Voldemort.
Operators
Mathematical operators
First, we have the standard operators for addition (+
), subtraction (-
), multiplication (*
), and division (/
). Additional mathematical operators include,
- Modulus operator
%
, which gives the remainder, e.g.12 % 5
will result in 2. - Exponent operator,
**
, e.g.5**2
will result in 25.
Logical operators
In addition to symbols used for basic calculations, Python (like other languages!) has a series of symbols used to compare statements in a True/False context. Such statements are called logical statements, and the symbols we used to compare them are called logical operators. The most commonly used logical operators include,
Symbol | What it does | Example |
---|---|---|
== | Equals | 5 == 5 results in True |
!= | Not equals | 5 != 5 results in False |
> | Greater than | 5 > 6 results in False 11 > 6.23 results in True |
>= | Greater than or equal to | 5 >= 4 results in True |
< | Less than | 4 < 5 results in True |
<= | Less than or equal to | 4 <= 5 results in True |
And above all, it is very important to remember that the equals logical comparison requires a double equals sign (==
) - a single equals signs indicates variable assignment (see below).
We can also perform multiple comparisons at once, using the keywords and
and/or or
.
For example,
Variables
We assign values to a variable using the equals sign, =
.
All variables have a certain type. The variable type deterines what can be done with the variable itself. Standard python types include the following,
Variable Type | Description | Defining | Casting |
---|---|---|---|
integer | whole number | int() | int() |
float | decimal number | float() | int() |
string | ordered, immutable character container | ” ” or ‘ ‘ | str() |
list | ordered, mutable container | [ ] | list() |
dictionary | unordered, mutable container | { } | dict() |
tuple | ordered, immutable container | ( ) | tuple() |
Remember, every time you create a variable, you are creating an instance of that particular variable type. As a consequence, there are certain properties (attributes and methods!) associated with each type of variable.
Integers and floats
Integers and floats are python’s primary types for dealing with numbers.
Integers are whole numbers only, but floats include decimal places. Whether a variable is an integer or float turns out to matter a lot - if you perform an operation with integers, the result will be an integer (even if the “real” answer is actually a float!)
Strings
In python, a string is an immutable container of characters. By “immutable”, we mean that it can’t be changed - that is, once you create a string, you can’t rewrite certain parts of it. It’s an all-or-nothing thing. By characters, we basically mean “not numbers” - consequently, no mathematical operations can be done on strings.
Strings are also ordered. This means we can index characters in a string, using brackets [ ]
.
Python indexes starting at 0, meaning the first item in a given string is the 0th entry.
Here are some useful string methods:
Lists
Lists are defined using brackets [ ]
, and each list item can be any variable type.
Python lists are incredibly flexible. Like strings, they are ordered, so they support indexing.
However, unlike strings, lists are mutable, meaning they can be changed! List items can be removed, changed, and new list items can even be added after they are defined.
In other words, lists can be changed in place without needed to reassign the variable with an =.
Here are some useful list methods:
Dictionaries
Dictionaries are defined using braces { }
, and they are essentially unordered lists of key:value pairs, and they are python’s version of “associative arrays.” Keys and values can be any type, although typically keys are either integers, floats, or strings. Dictionaries are incredibly useful for storing information; all keys must be unique, but values may be repeated.
Because dictionaries are unordered, we cannot index them using the standard brackets. Instead, we index dictionaries using keys. The key:value pairs are fixed, but there is no specific order to the key:value pairs within the dictionary.
Tuples
Tuples are essentially immutable (i.e. unchangable) lists, created with parentheses ( )
. This variable type will become very important when we learn about functions in a few weeks.
Indexing
In general, indexing follows the paradigm container[x:y:z]
, where x is the starting index, y is the ending index, and z is the step. However, you do not need to provide all of these value to index.
Most importantly, the starting index is inclusive but the ending index is exclusive. See below for examples.
Other useful functions
The len()
function returns the length of a container (list, dictionary, string, tuple)
The range()
function creates an arithmetic list, by default starting from zero and with a step of 1
The type()
function tells you the type (e.g. list, int) of a certain variable. This function can be very useful for error checking.
The dir()
function tells you what types of methods or actions you can perform on a given variable (ignore the __action__
formatted output - this is not relevant for now!).
The print function
Printing information from a script to “stdout” is a critically important compontent of scripting and programming. Only by printing can you determine if your code is actually doing what you think it is. Most of the time, your code will have some issues and will need to be “debugged.” Printing to screen is one of the best and easiest strategies for ensuring that your code is working as intended.
You can more or less print anything to screen, and importantly you can print multiple things in the same statement!
Exercises
Remember that printing is the only way to confirm if your code is behaving as expected. Print print print! We recommend that you perform these exercises in Python scripts (not in an active interpreter session).
- Write a script to count the number of each nucleotide (A, C, G, T) in a nucleotide sequence. You should define a variable containing a sequence (hint: this variable should be a string!), and you should use the method
.count()
to count each nucleotide. Your script should print out the calculated nucleotide counts, for example:
-
Modify your code from the previous exercise to compute the percentages (rather than counts) of each nucleotide in given sequence. As before, print out the calculated percentages.
- Define a variable called
mystring
, which contains a lengthy string of some kind (random letters, your address, song lyrics, a haiku, whatever). Perform the following tasks with this variable:- Use the
len()
function to determine how many characters are in your string. - Without changing your variable itself, replace the all occurrences of a letter of your choice with the number 6. Use the string method
.replace()
for this task. - Redefine mystring such that only the first 3 occurrences of this letter are replaced, again using the string method
.replace()
. (Hint: not sure how to do this? Enterhelp(replace)
in a Python interpretter session, or google how to use thereplace
method!). - Try to use indexing to replace the character in the 5th position of mystring with the letter “X”. Did this work? Can you figure out why or why not?
- Use the
.upper()
method to re-define your string variable to be entirely uppercase. Make sure your variable has been redefined by printing it out! - Use the
.split()
method to split your string into a list. Do this two times, the first time using a character that is in your string and the second time using a character that is not present in your string. Think about why the resulting output looks the way it does.
- Use the
- Create a variable called
mylist
, which is a list of numbers (integers or floats, whatever you choose!). Perform the following tasks with this variable:- Use the
len()
function to determine how many entries are in your list. - Use the
.append()
method to add another number to the end of your list. For example, if you started with[1, 2, 6, 7]
, you might now have[1, 2, 6, 7, 3.3]
. - Re-assign the 2nd entry (hint: which index is this?) of your list to the opposite sign of that entry (i.e. 4 would now be -4, -9 would now be 9).
- Re-assign the 1st entry of your list to be a new list. For example, if you started with
[1, 2, 6, 7]
, you might now have[ [1,2,3], 2, 6, 7]
. Use thelen()
function to determine how long the list is now. Is it what you expected? Think about why or why not. - Use the
len()
function to determine the length of this first entry within your list (hint: lots of indexing!).
- Use the
- Create a variable called
mydict
, which contains this dictionary (you can copy/paste!):{"dna": "nucleotides", "rna": "nucleotides", "protein": "amino acids"}
. Perform the following tasks with this variable:- Use dictionary indexing to print out the value associated with the key “dna”.
- Add this new key:value pair to the dictionary: “molecule” is the key, and “atoms” is the value.
- Add this other new key:value pair to the dictionary: “dna”:”ACGT”. Print out the dictionary. What do you notice? What does the output tell you about dictionary keys and values?
- Create a new list variable which contains the keys in your dictionary. For this exercise, do not type out the keys - use the
.keys()
method. - Determine the length of the value associated with the dictionary key “dna”. Do this procedure for each key in the dictionary, and calculate the average value length. Print this value to screen.