week1
week1
week1
2
Lab Assignments
• Students will attend weekly hands-on exercises during the lab session.
• There are 10 lab assignments to be completed during the weekly lab sessions.
• You will be graded for your performance and submission of your lab solutions.
• You must attend the lab session and upload your solution by the end of the
session to receive a grade.
• There are no make-ups for lab assignments. Special permission and medical
reports will not be accepted.
• You may not attend a lab other than the one to which you are registered.
3
Lab Grading and Attendance
• If you do not attend a lab, your grade for that lab will be zero.
• There are no make-up assignments for lab sessions, however in calculating your
lab average your lowest lab score will be discarded and your lab average will be
calculated using your top 9 lab scores.
• Students who do not attend the weekly lectures cannot receive full points for
the lab sessions.
• In order to receive full points for the lab assignments each week, you must
attend the lecture.
• Students who DO NOT attend the lecture but DO attend the lab session may
receive AT MOST 50% for the lab assignment.
4
Textbook
• Both books are open source and can be downloaded for free from the links given above.
5
What is Data Science?
6
Why Learn Programming?
7
What is Python
• A programming language is like any other language, it has a vocabulary and set of
grammatical rules for instructing a computer or computing device to perform
specific tasks.
• Python is a general-purpose programming language that can be used for a wide
variety of applications.
• Python is popular because there are many ready-made tools that are available
for data analysis.
8
Software
• For the course, we will be using the Anaconda Python 3.9 Distribution.
• Anaconda contains:
• Python: a programming language in which we write computer programs.
• We recommend that you install the Anaconda Python 3.9 distribution using the
instructions found at the following link: Anaconda Python
• When you download Anaconda, you will have access to the tools used in the
course: Python, Spyder and Jupyter.
10
Online Development Environment
• During the lab sessions you should use Spyder to complete your work.
• However, for practice outside the labs, if you are away from your
computer and want to practice Python, we recommend the online
tool, replit.com.
• Replit requires you to create an account however it is free to use.
11
First Python Commands
• A Python program is made up of one or more statements/commands, which are instructions
telling the computer to do something.
• Like in any language, statements must be meaningful and follow certain rules.
• When we type a command, the Python interpreter has to decide how to carry out the
command.
• If our command is not written correctly, the interpreter cannot understand what we are
asking, and will display an error message.
• Errors in the commands must be corrected for our statements to run (execute) correctly.
12
First Commands: Examples
• Step 1: Open the development environment.
• Step 2: Type the command(s).
• Step 3: Evaluate the results.
Try the following:
13
Things to Notice
• Whitespace characters:
• Include spaces, newlines, tabs and they make programs and commands easier to read.
• Notice in the examples, we put spaces before and after the arithmetic operators ( 3 + 5 )
• This is an example of using whitespace to improve readability.
• The python interpreter ignores these extra characters, as they are for the reader of the
program (us!) rather than the interpreter.
• With the spaces removed, the program would behave in the same way.
• Also notice that for multiplication, we use the ‘*’ symbol instead of 3 x 5.
• These are examples of the syntax rules of python, which we will discuss in
detail next week.
14
print() statement
• Python provides functions to carry out common tasks, the print
function is an example of this.
• The print() statement allows us to print messages to the console
window in our programs.
• print() can echo text strings, numbers or calculations.
• Text strings should be between single or double quotes.
• If we would like to output multiple values, we can separate them with
a comma.
15
First Commands: print()
• Try the following:
16
Printing Special Characters
• Sometimes we want to include characters in our output that have
special meaning.
• This includes characters such as quotes, newlines, etc.
• The table below shows a number of special characters in python.
Special Meaning
Character
\n New line character
\t Tab character
\\ Backslash character
\’ Single quote
\” Double quote
17
Printing Special Characters - Examples
Example 1:
Example 2:
18
Objects
19
Numeric Data Types – int, float
There are two numeric data types in Python that store integer and floating-point values.
int is the data type of objects that store integer values. Examples are: 4, 11, 73, 1243.
float is the data type that stores floating point values. Examples are: 44.2, 79.435, 2.6.
Numeric objects (int, float) are used in arithmetic operations.
int and float objects are called scalar types, because they store a single value.
20
Text/String Data Type – str
Text values such as ‘CS125’, ‘Hello World!’, ‘4’ are called strings in python.
The data type determines what we can do with the object: strings can be joined,
but we cannot subtract one string from another.
Any values inside double or single quotes are interpreted as strings and not
numbers.
We cannot use ‘4’ in the same way we use 4.
21
Data Types – type()
22
Variables
Our programs (scripts) store data that will be used and manipulated.
In order to access the data, we should store it somewhere and give it a name.
23
Naming Variables
24
Valid Variable Names
Which of the following names are valid variable names:
• first_name
• sub-total
• first entry
• Section1
• 4all
• *2
• classSize
• LOCATION
• int
25
Valid Variable Names
Which of the following names are valid variable names:
• first_name (VALID)
• sub-total (INVALID - dash is not a legal symbol in an identifier)
• first entry (INVALID - space is not a legal symbol in an identifier)
• Section1 (VALID)
• 4all (INVALID - begins with a digit)
• *2 (INVALID - the asterisk is not a legal symbol in an identifier)
• classSize (VALID)
• LOCATION (VALID)
• int (INVALID - int is a reserved word)
26
Naming Guidelines
To make your programs more understandable, it is recommended you use the following
guidelines:
• Give your variables meaningful names, which indicate the purpose of the data stored.
• If your name includes multiple words, separate the words with an underscore.
27
Assignment – binding values to variables
Variables store values. To store a value with a given name, we assign the value to the variable
using the assignment operator (=).
An assignment statement associates a value with a variable.
Note: The meaning of the assignment operator is different from equality in mathematics.
In Python, = sign assigns the value of the expression on the right to the variable on the left.
28
Assignment
The values stored in variables may change throughout the execution of a program.
If a variable is reassigned with a new value, the new value overwrites any existing
value.
If the new value is of a different type, the type of a variable may also change during
the execution of a program.
29
Type Casting – Changing the Type of a Value
Sometimes we need to convert a value from one type to another.
Each type has a special statement we can use (called functions) to convert a
value to that type.
For example, if we want to convert the string value ‘5’ to an integer value, we
used the int() statement.
30
Type Casting – Changing the Type of a Value
Use caution when type casting, each value can only be cast to a suitable type.
For example, the string value ‘5’ has the integer representation of 5, or the float representation of
5.0.
If we try to convert the string value ‘abc’ to an int, it has no integer representation, so a type error
will occur.
IMPORTANT: If we convert float values to an integer, the float value will be truncated, meaning the
value is NOT rounded, any decimals are dropped.
Examples:
int(5.2) -> 5
int(5.8) -> 5
float(3) -> 3.0
str(5) -> ‘5’
str(3.7) ->’3.7’
int(‘abc’) -> ERROR 31