Meeting 9 Basic Python 1
Meeting 9 Basic Python 1
Science
PERTEMUAN IX
© IBM 2020
Learning Objectives
© IBM 2020
Based on recent surveys, Python was and still
is the MOST popular, high-level, open-source
programming language! It is a powerful, fast
and dynamic programming language that runs
everywhere.
It’s also interactive, object-oriented, and very
Why Python? easy to learn. Anyone can create programs
with it.
It’s particularly great for web development,
and scientific computing.
With its vast libraries, it’s also useful for data
visualization and data analytics.
© IBM 2020
Why Python?
This course covers Python 3 and its many new features. It is comprised of four
modules.
We'll start off with Python basics, writing your first program, and then cover, in detail,
types, expressions, variables, strings and string operations.
In the 2nd module we'll introduce you to Python data structures, namely lists and
tuples, sets and dictionaries.
© IBM 2020
Statement
A statement or expression is an instruction the computer will run or execute. Perhaps the simplest program you
can write is a print statement.
When you run the print statement, Python will simply display the value in the parentheses.
© IBM 2020
Statement
The value in the parentheses is called the argument. If you are using a Jupiter notebook in this course, you
will see a small rectangle with the statement.
This is called a cell. If you select this cell with your mouse, then click the run cell button. The statement will
execute. The result will be displayed beneath the cell.
© IBM 2020
Types
© IBM 2020
Types
The following chart summarizes three data types for the last examples.
The first column indicates the expression. The second column indicates the data type.
© IBM 2020
Types - Integer
We can see the actual data type in Python by using the type command.
We can have int, which stands for an integer, and float that stands for float, essentially a real number. The type
string is a sequence of characters.
Integers can be negative or positive. It should be noted that there is a finite range of integers, but it is quite large.
© IBM 2020
Types - Float
Floats are real numbers; they include the integers but also numbers in between the integers.
Consider the numbers between 0 and 1. We can select numbers in between them; these numbers are
floats.
Similarly, consider the numbers between 0.5 and 0.6.
We can select numbers in-between them; these are floats as well.
© IBM 2020
We can continue the process, zooming in for different
numbers.
Of course, there is a limit, but it is quite small.
For example, if you cast the float 1.1 to 1, you will lose some information.
If a string contains an integer value, you can convert it to int. If we convert a string that contains a non-integer
value, we get an error. Checkout more examples in the lab.
You can convert an int to a string or a float to a string.
© IBM 2020
Types - Boolean
Boolean is another important type in Python. A Boolean can take on two values.
The first value is true, just remember we use an uppercase T.
Boolean values can also be false, with an uppercase F.
Using the type command on a Boolean value, we obtain the term bool, this is short for Boolean.
© IBM 2020
Types - Boolean
© IBM 2020
Types - Boolean
© IBM 2020
Expressions and
Variables
© IBM 2020
Expressions
We call the numbers operands and the maths symbols, in this case addition, are called operators.
© IBM 2020
Expressions
We can perform operations such as subtraction using the subtraction sign. In this case, the result is a
negative number.
We can perform multiplication operations using the asterisk. The result is 25.
© IBM 2020
Expressions
In this case the operands are given by – and *. We can also perform division with the forward
slash.
25 divided by 5 is 5. 25 divided by 6 is approximately 4.167.
© IBM 2020
Expressions
In Python 3, the version we will be using in this course, both will result in a float.
We can use the double slash for integer division, where the result is rounded.
Be aware, in some cases, the results are not the same as regular division.
© IBM 2020
Expressions
© IBM 2020
Expressions
In both cases, Python performs multiplication, then addition to obtain the final result.
There are a lot more operations you can do with Python.
Check the labs for more examples. We will also be covering more complex operations throughout the course.
The expressions in the parentheses are performed first.
We then multiply the result by 60, the result is 1920. Now let’s look at variables.
© IBM 2020
Variables
We can use variables to store values, in this case, we assign a value of 1 to the variable
my_variable using the assignment operator. i.e., the equals sign.
We can then use the value somewhere else in the code by typing the exact name of the
variable.
© IBM 2020
Variables
© IBM 2020
Variables
We can store the results of expressions, for example, we add several values and assign the result to x. x
now stores the result.
We can also perform operations on x and save the result to a new variable y. y now has a value of 2.666
© IBM 2020
Variables
© IBM 2020
List and Tuples
These are called compound data types and are one of the key types of data structures in Python.
© IBM 2020
Tuples
© IBM 2020
Tuples
© IBM 2020
Tuples
The first element can be accessed by the name of the tuple followed by a square bracket with the index
number, in this case, zero.
We can also access the last element.
In Python, we can use negative index. The relationship is as follows.
The corresponding values are shown here.
© IBM 2020
Tuples
We can concatenate or combine tuples by adding them. The result is the following with the following
index.
If we would like multiple elements from a tuple, we could also slice tuples. For example, if we want
the first three elements, we use the following command. The last index is 1 larger than the index you
want.
© IBM 2020
Tuples
Notice how the last index is 1 larger than the length of the tuple. We can use the “len” command to obtain
the length of a tuple.
© IBM 2020
Tuples
As there are 5 elements, the result is five. Tuples are immutable, which means we can't change
them.
To see why this is important, let's see what happens when we set the variable Ratings 1 to ratings.
© IBM 2020
Tuples
Let's use the image to provide a simplified explanation of what’s going on.
Each variable does not contain a tuple, but references the same immutable tuple object.
© IBM 2020
Tuples
© IBM 2020
Tuples
We can assign a different tuple to the Ratings variable. The variable Ratings now references another
tuple.
© IBM 2020
Tuples
As a consequence of immutability, if we would like to manipulate a tuple, we must create a new tuple instead.
For example, if we would like to sort a tuple, we use the function sorted. The input is the original tuple. The
output is a new sorted tuple.
© IBM 2020
Tuples
A tuple can contain other tuples as well as other complex data types; this is called nesting We can access these
elements using the standard indexing methods.
If we select an index with a tuple, the same index convention applies. As such, we can then access values in the
tuple.
For example, we could access the second element. We can apply this indexing directly to the tuple variable NT.
© IBM 2020
Tuples
© IBM 2020
Lists
© IBM 2020
Lists
In many respects lists are like tuples, one key difference is they are mutable. Lists can contain strings,
floats, integers.
We can nest other lists.
We also nest tuples and other data structures; the same indexing conventions apply for nesting.
Like tuples, each element of a list can be accessed via an index.
The following table represents the relationship between the index and the elements in the list.
© IBM 2020
Lists
© IBM 2020
Lists
The first element can be accessed by the name of the list followed by a square bracket with the index number,
in this case zero.
We can access the second element We can also access the last element.
© IBM 2020
Lists
© IBM 2020
Lists
© IBM 2020
Lists
The argument inside the parenthesis is a new list that we are going to concatenate to
the original list.
In this case, instead of creating a new list, L1, the original list L is modified by
addingtwo new elements.
© IBM 2020
Lists
© IBM 2020
Lists
Every time we apply a method, the lists changes. If we apply extend, we add two new elements to the list.
The list L is modified by adding two new elements. If we append the string “A“, we further change the list adding
the string “A”.
© IBM 2020
Lists
© IBM 2020
Lists
We can delete an element of a list using the "del" command; we simply indicate the list item we would like to
remove as an argument.
For example, if we would like to remove the first element, the result becomes 10,1.2.
© IBM 2020
Lists
© IBM 2020
Lists
We can use the split function to separate strings on a specific character, known as a delimiter.
We simply pass the delimiter we would like to split on as an argument, in this case a comma.
The result is a list, each element corresponds to a set of characters that have been separated by a comma.
© IBM 2020
Lists
When we set one variable, B equal to A, both A and B are referencing the same list. Multiple names referring to the
same object is known as aliasing.
We know from the last slide that the first element in B is set as hard rock.
If we change the first element in “A” to “banana” we get a side effect; the value of B will change as a consequence.
© IBM 2020
Lists
"A" and “B” are referencing the same list, therefore if we change "A“, list "B" also changes.
If we check the first element of B after changing list ”A” we get banana instead of hard rock.
© IBM 2020
Sets
© IBM 2020
Sets
When the actual set is created, duplicate items will not be present.
To define a set, you use curly brackets You place the elements of a set within the curly brackets. You
notice there are duplicate items.
When the actual set is created, duplicate items will not be present.
© IBM 2020
Sets
When the actual set is created, duplicate items will not be present.
You can convert a list to a set by using the function set; this is called type-casting. You simply use the list as the
input to the function set The result will be a list converted to a set.
Let's go over an example We start off with a list. We input the list to the function set.
The function set returns a set. Notice how there are no duplicate elements.
© IBM 2020
Sets
Let’s go over Set Operations; these can be used to change the set.
Consider the set “A”. Let's represent this set with a circle. If you are familiar with sets this can be part of a Venn
diagram.
A Venn diagram is a tool that uses shapes usually to represent sets.
We can add an item to a set using the add method. We just put the set name followed by a dot, then the add
method.
© IBM 2020
Sets
The argument is the new element of the set we would like to add, in this case, "NSYNC".
The set "A" now has "NSYNC” as an item. If we add the same item twice, nothing will happen as there can be no
duplicates in a set.
© IBM 2020
Sets
We can also remove an item from a set using the remove method. We can verify if an element is in the set using the
"in" command as follows. The command checks if the item, in this case,
"AC/DC" is in the set. If the item is in the set, it returns true.
© IBM 2020
Sets
If we look for an item that is not in the set, in this case for the item “Who”, as the item is not in the set, we
will get a false.
© IBM 2020
Sets
These are types of mathematical set operations; there are other operations we can do.
There are lots of useful mathematical operations we can do between sets.
Let's define the set "album set 1,“ we can represent it using a red circle or Venn diagram. Similarly, we can define
the set "album set 2”.
We can also represent it using a blue circle or Venn diagram.
The Intersection of two sets is a new set containing elements which are in both of those sets. It's helpful to use
Venn diagrams.
© IBM 2020
Sets
The two circles that represent the sets combine; the overlap represents the new set.
As the overlap is comprised of the red circle and blue circle, we define the intersection in terms of “and.”
© IBM 2020
Sets
© IBM 2020
Sets
After applying the Intersection operation, all the items that are not in both sets disappear.
© IBM 2020
Sets
In Python, we simply just place the ampersand between the two sets.
We see that both "AC/DC" and "Back in Black" are in both sets.
The result is a new set album, set 3, containing all the elements in both album set 1 and album set 2.
© IBM 2020
Sets
Union of two sets is the new set of elements which contain all the items in both sets. We can find the union of the
sets album set 1 and album set 2 as follows.
The result is a new set that has all the elements of album set 1 and album set 2. This new set is represented in
green.
© IBM 2020
Sets
Consider the new album set album set 3; the set contains the elements "AC/DC" and "Back in Black”. We can
represent this with a Venn diagram, as all the elements in album set 3 are in album set 1.
The circle representing album set 1 encapsulates the circle representing album set 3.
We can check if a set is a subset using the is subset method.
As album set 3 is a subset of the album set 1, the result is true.
© IBM 2020
Dictionaries
If you recall, a list has integer indexes. These are like addresses. A list also has elements. A dictionary has keys
and values.
The key is analogous to the index, they are like addresses but they don’t have to be integers. They are usually
characters; the values are similar to the elements in a list and contain information.
© IBM 2020
© IBM 2020
Dictionaries
Consider the following example of a dictionary, the album title is the key and the value is the release data. We can
use yellow to highlight the keys and leave the values in white.
It is helpful to use the table to visualize a dictionary, where the first column represents the keys and the second
column represents the values. We can add a few more examples to the dictionary.
© IBM 2020
Dictionaries
We can also assign the dictionary to a variable. The key is used to look up the value. We use square brackets; the
argument is the key. This outputs the value.
Using the key of "Back in Black," this returns the value of 1980. The key "The Dark Side of the Moon," gives us the
value of 1973.
© IBM 2020
Dictionaries
We can delete an entry as follows. This gets rid of the key "Thriller" and its value. We can verify if an element is in
the dictionary using the in command as follows. The command checks the keys.
© IBM 2020
Dictionaries
© IBM 2020
Dictionaries
In order to see all the keys in a dictionary, we can use the method keys to get the keys. The output is a list like
object with all the keys.
In the same way, we can obtain the values using the method values. Check out the labs for more examples and
info on dictionaries.
© IBM 2020
LAB
© IBM 2020