DATA 301¶
Slide formats:
Projects in DATA 301¶
You will have a choice between doing an individual project vs. a group project
Group sizes will be set at 3 (no exceptions)
Expectations will be different for individual vs. group projects
I will assign groups for those that want to do group projects
Time zones, skills/experience will be taken into account
The group will be a mix to pair those new to programming, with others
Python Math Expressions¶
Operation |
Syntax |
Example |
Output |
---|---|---|---|
Add |
+ |
5+3 |
8 |
Subtract |
- |
10 - 2 |
8 |
Multiply |
* |
5 * 3 |
15 |
Divide |
/ |
8/4 |
2 |
Modulus |
% |
9%4 |
1 |
Exponent |
** |
5 ** 2 |
25 |
Expressions - Operator Precedence¶
Each operator has its own priority similar to their priority in regular math expressions:
Any expression in parentheses is evaluated first starting with the inner most nesting of parentheses.
Exponents
Multiplication and division (*, /, %)
Addition and subtraction (+,-) Recall:BEDMAS Brackets Exponents Division, Multiplication, (modulus), Addition and Subtraction Example: 20 - ((4 + 5) - (3 * (6 - 2))) * 4 = 32
Python Expression Question¶
HINT: Modulo is executed after multiplication and division; more on this here.
A) 69 B) 65 C) 36 D) 16 E) 70
Python Expression Question¶
HINT: Modulo is executed after multiplication and division; more on this here.
A) 69 B) 65 C) 36 D) 16 E) 70 I think it’s good practice to be explicit with brackets. I.e., I might have written the above as:
Try it: Python Variables and Expressions¶
Rules for Stings in Python¶
Strings are sequences of characters that must be surrounded by single or double quotes. Any number of characters is allowed. The minimum number of characters is zero “”, which is called the empty string. Strings can contain most characters except enter, backspace, tab, and backslash. These special characters must be escaped by using the escape character: \
Example: new line \n single quote ‘ backslash \ double quote ‘’
Strings¶
As mentioned previously, we can use triple quotes “”” for a strings that contain single/double quotes and/or line breaks. In addition, double quoted strings can contain single quoted strings and vice versa. Example:
Python String Indexing¶
Individual characters of a string can be accessed using square brackets ([]); the first character indexed at 0. - Example:
Read all more about strings here.
Python String Functions and Methods¶
Suppose: st = “Hello” st2 = “Goodbye”
Operation |
Syntax |
Example |
Output |
---|---|---|---|
Length |
len() |
len(st) |
5 |
Upper case |
upper() |
st.upper() |
HELLO |
Lower case |
lower() |
st.lower() |
hello |
Convert to a string |
str() |
str(9) |
“9” |
Concatenation |
+ |
st1 + st2 |
HelloGoodbye |
Substring |
[] |
st[0:3] st[1:] |
Hel ello |
String to int |
int() |
int(“99”) |
99 |
Dot Notation¶
Like VBA, you will notice that python uses the dot operator to perform methods on objects (read more here).
Every constant, variable, or function in Python is actually a object with a type and associated attributes and methods.
A method is a function that is attached to an object (read more about this here)
Here are some more examples of string methods.
String Operators: Concatenation¶
The concatenation operator is used to combine two strings into a single string. The notation is a plus sign “+”. - Example:
String Operators: Concatenation¶
Note that we must hard code spaces if we want them:
N.B. we can mix types in the print() function, i.e. without concatenation. Notice how print() inserts spaces between inputs:
String Operators: Deleting objects¶
If you have been following along with me, you will find that the code on the previous slide does not work. This is because str is no longer treated as a function because I assigned “Hello” to this object on slide ??. To delete this object we can type:
Now we are free to use the function str() as desired.
Substrings (slicing)¶
The substring function will return a range of characters from a string. The general syntax is st[start:end]
Example:
Split¶
The split function will divide a string based on a separator. Without any arguments, it splits on whitespace,
[‘Awesome coding’, ‘ Very good’, ‘’]
Split¶
This is very useful when we have, for example, comma separated values (csv):
Note that the returned object is a Python list (more on these later).
Print Formatting¶
One of the most obvious changes between Python 2 and Python 3 is how they use print: print “Hello” and Python 3: print(“Hello”) In Python3, the print method can accept parameters for formatting. See some examples on the next slide …
Print Formatting¶
We can think of {} as placeholder arguments for the inputs given in format
By default, these inputs will be added in the string in order (the input0 will appear in the first place holder, input1 in the second place holder, etc.
If we want to read input1 before input0, we need to refer to it by its integer index via {1} or by its name if we have provided one {age}
List Overview¶
A list is a collection of data items that are referenced by index. I Lists in Python are similar to arrays in other programming languages
A list allows multiple data items to be referenced by one name and retrieved by index.
I Python list:
Retrieving Items from a list¶
Items are retrieved by index (starting from 0) using square brackets:
You can create an empty list using:
List manipulation¶
We can modify a single value by use of indices and the assignment operator =
We can also modify multiple values at a time in the following way:
Appending to a list¶
I Notice that when we try to add a value to the end of the list, we get an error:
To add an item to the end of the list, use append().
Appending to a list¶
Notice how append()doesn’t return a new list; rather it modifies the original list.
I Alternatively we could have used
List in loops¶
We can iterate over a list in a for loop.
colours = [‘red’, ‘yellow’, ‘green’, ‘blue’] for colour in colours: print(colour)
Thefollowingcodeappendsthevaluesinione-by-onetothe empty list j.
List append vs extend¶
append() adds its argument as a single element (eg. a number, a string or a another list) to the end of an existing list.
Notice how the length of the list increases by one.
If we want to add the elements one-by-one to the end of x we could either use a loop as we did in the previous slide of use the extend() function.
List extend¶
extend() iterates over its argument and adds each element to the list thereby extending the list.
The length of the list increases by number of elements in it’s argument.
Alternatively, we could have used
List extend vs append example¶
Read more about the difference between the two here
where x[3] returns [4, 5] and x[4] returns an error.
List Operations¶
If data = [1, 2, 3, 5] and lst = []
Operation |
Syntax |
Example |
Output |
---|---|---|---|
Add item |
|
data.append(1) |
[1, 2, 3, 5, 1] |
Insert item |
|
data.insert(3,4) |
[1, 2, 3, 4, 5] |
Remove item |
|
data.remove(5) |
[1, 2, 3] |
Update item |
list[idx]=val |
data[0]=10 |
[10, 2, 3, 5] |
Length of list |
len( |
len(data) |
4 |
Slice of list |
list[x:y] |
data[0:3] |
[1, 2, 3] |
Find index |
|
data.index(5) |
3 |
Sort list |
|
data.sort() |
[1, 2,3,5] |
Add |
lst = [] |
lst.append(1) |
[1] |
See more here
List details¶
It was mentioned already but its worth repeating…
For loops that are used to iterate though items in a list:
List details¶
Note that this is not restricted to numbers:
prints apples, bananas, oranges (each on a separate line).
We could even iterate through characters in a string:
prints b, a, n, a, n, a, s (each letter on a separate line)
List details¶
If we want to iterate through both index and value, we could use the enumerate() function.
If we want our index to start at 1 rather than 0, we could specify that as the second argument: enumerate() function.
Advanced: Python List Comprehensions¶
List comprehensions build a list using values that satisfy a criteria.
Example:
Equivalent to:
Advanced: Python List Slicing¶
List slicing allows for using range notation to retrieve only certain elements in the list by index.
Syntax:
list[start:end:step]
Example:
A) 0 B) 1 C) 2 D) 3E) not there
Lists¶
In the previous example we use the list() function to create our list instead of the square brackets.
This constructs a list using the single input. This could be
a sequence (eg. string, tuples) or
a collection (set, dictionary) or
a iterator object (like the objects iterated over in our for loops)
If no parameters are passed, it creates an empty list.
Tuples¶
A tuple is a collection which is ordered and unchangeable. To create tuples we use round brackets ().
Elements in a tuple are referenced the same way as lists:
Tuples are useful for storing some related information that belong together.
Unlike list, once a tuple is created, values can not be changed.
Notice that tuples are also iterable, meaning we can traverse through all the values. eg,
Python Sets¶
A set (like a mathematical set) is a collection which is unordered and unindexed.
Setsarewrittenwithcurlybrackets{}.
Since sets are unordered, the items will appear in a random order and elements cannot be reference by index.
Againwecaniteratethougheachitemusingaforloop.
Read more about these here.
Python Dictionary¶
A dictionary is a collection which is unordered, changeable and indexed. We create them with curly brackets and specify their keys and values.
N.B Keys should be unique. If you create different values with the same key, Python will just overwrite the value of the duplicate keys.
Python Dictionary¶
We can now reference elements by a given name (i.e key) rather than the standard integers index.
Referencing by index won’t work (remember these are unordered)
Don’t get confused at try to do indexing using the values rather than the keys! For instance the following would produce an error:
Python Dictionary¶
If we wanted to, we could always use integers for the keys (in that case we don’t need quotes around them when indexing using square brackets).
We can add/delete and view keys/values using the following:
Python Dictionary¶
To access both keys and values use:
Summary of Python Structures¶
Lists can be altered and hold different data types:
Tuples are immutable (we can’t change the values):
Sets do not hold duplicate values and are unordered.
Dictionaries hold key-value pairs (just like real life dictionaries hold word-meaning pairs).
A) 7 B) 0 C) 10 D) 6 E) error
Python Modules¶
A Python module or library is code written by others for a specific purpose. Whenever coding, make sure to look for modules that are already written for you to make your development faster! Modules are imported using the import command:
Useful modules for data analytics:
Biopython (bioinformatics),
NumPy (scientific computing/linear algebra),
scikit-learn (machine learning), pandas (data structures),
BeautifulSoup (HTML/Web)
Python Date and Time¶
Python supports date and time data types and functions. To use, import the datetime module using the following:
The general syntax to import the module named mymodule is:
You can choose to import only parts from a module, by using the from keyword. For example, if we just want the object person1 from mymodule type:
Read more about this here.
Python Date and Time¶
We may choose to only import the datetime object from the datetime module (which happens to be the same name) using the following:
The datetime object has a method for formatting date objects into readable strings. Read more here.
Python Date and Time¶
Methods:
Python Clock¶
The time module, is another useful module for handle time-related tasks.
The time() function for example, returns the current time in seconds.
On Linux machines, this is an integer counting the number of seconds passed since January 1, 1970, 00:00:00 (recall from Lecture 2).
This function can be useful when we want to time how long a process takes within our program. See an example on the next slide.
Python Clock¶
Example:
Python Input¶
To read from the keyboard (standard input), use the method input:1 - Example: name = input(“What’s your name?”) print(name) age = input(“What’s your age?”) print(age)
Try it: Python Input, Output, and Dates¶
Comparisons¶
A comparison operator compares two values.
Examples:
Comparison Operators in Python:
Syntax |
Description |
---|---|
> |
Greater than |
>= |
Greater than or equal |
< |
Less than |
<= |
Less than or equal |
== |
Equal (Note: Not “=” which is used for assignment!) |
!= |
Not equal |
The result of a comparison is a Boolean value which is either True or False.¶
Conditions with and, or, not A condition is an expression that is either True or False and may contain one or more comparisons. Conditions may be combined using: and, or, not. Order of evaluation: not, and, or. May change order with parentheses.
True if: |
Syntax |
Examples |
Outpu |
---|---|---|---|
both are true |
and |
True and True False and True |
True False |
either or both are T |
or |
True or True False or True False or False |
True True False |
false |
not |
not True not False |
False True |
Condition Examples¶
Order of Operations¶
Table: The order of operations for logicals. See complete list here
Syntax |
Description |
---|---|
() |
brackets |
** |
exponents |
* / % MOD |
Multiplication, division, modulo |
+ - |
Addition and subtraction |
< <= > >= |
Comparisons: less-than and greater-than |
== != |
Comparisons: equal and not equal |
and |
and |
or |
or |
nots always bind to the condition immediate next to it |
Python Flow Control
Decisions¶
Decisions are used in programming to specify one or more conditions to be tested, along with statement(s) to execute if the condition is true.
Acondit ion is an expression that is either True or False.
These conditions control the flow of you program and different statements will be carried out depending on the outcome of these conditions.
To build conditional statements we need to be able to write Boolean expressions.
Boolean Expressions¶
A Boolean expression is an expression that evaluates to a Boolean value 1 .
A Boolean value is either True or False. Boolean values Boolean values are not strings. The Python type for storing True and False values is called bool.
The name comes from George Boole, who first defined an algebraic system of logic in the mid 19th century
Boolean Expressions¶
We can create Boolean expressions using: Relational operators/Comparisons: used to compare two values
Examples:
Logical operators: the logical operators and, or and not are used to combine relational operators.
Example:
The result these expressions are a Boolean value which is either True or False
Comparisons¶
A condition is a Boolean expression that is either True or False and may contain one or more comparisons.
The comparison operators in Python are summarized below:
Syntax |
Description |
---|---|
> |
Greater than |
>= |
Greater than or equal |
< |
Less than |
<= |
Less than or equal |
== |
Equal (Note: Not “=” which is used for assignment!) |
!= |
Not equal |
Conditions with and, or, not¶
Conditions may be combined using the relational operators and, or, not.
Syntax |
Description |
Examples |
Output |
---|---|---|---|
both are true |
and |
True and True |
True |
either or both are T |
or |
True or True |
True <brTrue |
false |
not |
not True |
False |
Condition Examples¶
Order of Operations¶
Table: The order of operations; see complete list here.
() |
brackets |
Answer:
How many of the following conditions are TRUE?
1. True and False = True or False
2. not True or not False = (not True) or (not False)
3. 3+2==5or5>3and4!=4
= (5 == 5) or (5 > 3) and (4 != 4) =(5==5)or((5>3)and(4!=4))# and first
4. (1<2or3>5)and(2==2and4!=5) (1 < 2 or 3 > 5) and (2 == 2 and 4 != 5) (1 < 2 or 3 > 5) and (2 == 2 and 4 != 5)
5. not (True or False) or True and (not False) not (True or False) or True and (not False) not (True or False) or True and (not False)
A) 1 B) 2 C) 3 D) 4 E) 5
Decisions¶
In Python decision syntax:
put an image here¶
Thestatement(s)aftertheifconditionisonlyperformedif the condition (i.e. Boolean expression) returns True.
Anystatement(s)followingthe(optional)else:conditionis only performed if the condition is False.
Decision Block Syntax¶
Statementslistedafteranif/elif/esleclausearenotonly indented for readability.
These indentation is also how Python knows which statements are part of the group of statements to be executed.
Statements with the same indentation belong to the same group called a suite.
Be consistent with either using tabs or spaces (no mixing)
Decisions if/elif Syntax¶
If there are more than two choices, use if/elif/else statements.
N.B. once a condition is met, no subsequent conditions are checked
if, elif, else¶
need a flow chart¶
Decisions Multiple if statements¶
I As mentioned previously, once a condition is met in an if/elif statement, no subsequent conditions are checked. I If we want all conditions to be checked we could use multiple if statements:
n=3 if n>0: print(“positive number”) if n==3: print(“three”) if n<10: print(“single digit”)
nothing
one
two
three
5. error (missing colon)
A)nothing B) one four C) three D) three four E) error
Example 20
What is the output of the following code?
n=0
if n < 1:
print("one")
print("five")
elif n == 0:
print("zero")
else:
print("three")
print("four")
A)nothing
B) one
four
C) one
four
five
D) one
five
zero
four
E) error