0% found this document useful (0 votes)
19 views

Meeting 9 Basic Python 1

This document provides an overview of the key concepts that will be covered in a Python course for data science, including: - The course will teach Python basics, data structures, files, and the pandas library for data analysis - Python is a popular, high-level, open-source language useful for web development, scientific computing, and data visualization - The course covers Python types like integers, floats, Booleans, strings, lists, tuples, and dictionaries as well as expressions, variables, and functions

Uploaded by

johanr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Meeting 9 Basic Python 1

This document provides an overview of the key concepts that will be covered in a Python course for data science, including: - The course will teach Python basics, data structures, files, and the pandas library for data analysis - Python is a popular, high-level, open-source language useful for web development, scientific computing, and data visualization - The course covers Python types like integers, floats, Booleans, strings, lists, tuples, and dictionaries as well as expressions, variables, and functions

Uploaded by

johanr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Basic Python: Python for Data

Science
PERTEMUAN IX

© IBM 2020
Learning Objectives

In this course you will learn about:


• What is Python and why is it useful
• The application of Python
• How to define variables
• Sets and conditional statements in Python
• The purpose of having functions in Python
• How to operate on files to read and write data in Python
• How to use pandas, a must have package for anyone attempting data analysis in Python.

© 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

A type is how Python represents


different types of data.
In this video, we will discuss
some widely used types in
Python.
You can have different types in
Python.
They can be integers like 11,
real numbers like 21.213.
They can even be words.
Integers, real numbers and
words can be expressed as
different data 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.

You can change the type of the expression in Python; this


is called type-casting. You can convert an int to a float.
Types - Float

For example, you can convert or cast the integer 2 to a


float 2.
Nothing really changes. If you cast a float to an integer,
you must be careful.
© IBM 2020
Types - Float

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

If we cast a Boolean true to an integer or float, we will get a 1.


If we cast a Boolean false to an integer or float, we get a zero. If you cast a 1 to a Boolean, you get a true.

© IBM 2020
Types - Boolean

Similarly, if you cast a 0 to a Boolean, you get a false.

© IBM 2020
Expressions and
Variables

Expressions describe a type of operation that computers perform.


Expressions are operations that Python performs. For example, basic arithmetic operations like adding
multiple numbers. The result, in this case, is 160.

© 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

Python follows mathematical conventions when performing mathematical expressions. The


following operations are in a different order.

© 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

We will use a colon do denote the value of the variable.


We can assign a new value to my_variable using the assignment operator.
We assign a value of 10. The variable now has a value of 10. The old value of the
variable is not important.

© 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

We can also perform operations on x and assign the value x.


The variable x now has a value 2.666. As before, the old value of x is not important. We
can use the type command in variables as well.

© 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

In Python, there are different types: strings, integer, float.


They can all be contained in a tuple, but the type of the variable is
tuple.

© IBM 2020
Tuples

Each element of a tuple can be accessed via an index.


The following table represents the relationship between the index and the elements in the tuple.

© 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

Let's say we want to change the element at index 2.


Because tuples are immutable, we can't.
Therefore, Ratings 1 will not be affected by a change in Rating because the tuple is immutable i.e., we can't
change it.

© 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

It is helpful to visualize this as a tree.


We can visualize this nesting as a tree. The tuple has the following indexes.
If we consider indexes with other tuples, we see the tuple at index 2 contains a tuple with two
elements.
We can access those two indexes. The same convention applies to index 3.
We can access the elements in those tuples as well. We can continue the process. We can even
access deeper levels of the tree by adding another square bracket.

© IBM 2020
Lists

Lists are also a popular data structure in Python.

© 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

• The following table represents the relationship between the index


and the elements in the list.

© 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

The relationship is as follows. The corresponding indexes are as follows.


We can also perform slicing in lists. For example, if we want the last two elements in this list we use
the following command.
Notice how the last index is 1 larger than the length of the list. The index conventions for lists and
tuples are identical.

© IBM 2020
Lists

Lists are mutable, therefore, we can change them.


For example, we apply the method extends by adding a "dot" followed by the name of the method, then
parenthesis.

© 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

If we apply append instead of extended we add one element to the list.


If we look at the index, there is only one more element. Index 3 contains the list we appended.

© 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

As lists are mutable, we can change them.


For example, we can change the first element as follows The list now becomes: “hard rock”, 10, 1.2.

© 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

We can convert a string to a list using split.


For example, the method split converts every group of characters separated by a space into an element of a list.

© 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

Let’s cover sets; they are also a type of collection.

© 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

In Python, we use the ampersand to find the union of two sets.


If we overlay the values of the set over the circle placing the common elements in the overlapping area, we see the
correspondence.

© 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

If they are in the dictionary, they return a true.


If we try the same command with a key that is not in the dictionary, we get a false.

© 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

Please continue to lab Pyhton for Data Science:


- Module 1 – Phyton Basics
https://courses.cognitiveclass.ai/courses/course-
v1:Cognitiveclass+PY0101EN+v2/courseware/407a9f86565c44189740699636b4f
b85/e5eb94bcfa6c4af7a0a378bcf08365f2/
- Module 2 – Phyton Data Structures
https://courses.cognitiveclass.ai/courses/course-
v1:Cognitiveclass+PY0101EN+v2/courseware/76d637cbe8024e509dc445df847e6
c3a/da5e402e18d64a14b8c00a843bd07b75/

© IBM 2020

You might also like