week1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Week 1

Introduction and overview.


Variables, Data Types and Input.
Course Information

• Course Management System:


• Moodle
• Course Hours:
• 2 hours lecture / 2 hours lab
• Lectures and labs are face-to-face.
• Assessments:
• 1 midterm 40%
• 1 final 40%
• 10 lab assignments 20%

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

• There are 2 required textbooks for this course:

1. Python Data Science Handbook, Jake VanderPlas, O’Reilly. [download]


2. Fundamentals of Python Programming, Richard L. Halterman [download]

• Both books are open source and can be downloaded for free from the links given above.

5
What is Data Science?

• Data science is the study of data.


• It involves developing methods of recording, storing, and analyzing data to
effectively extract useful information.
• The goal of data science is to gain insights and knowledge from data.
• All fields use data science to analyze and interpret large sets of data.

6
Why Learn Programming?

• Social scientists spend a lot of time analyzing large data sets.


• This involves writing relatively simple programs (scripts) that execute a
series of commands (storing data, merging datasets, parsing text documents,
etc.) to convert their data to a form they can analyze.
• Social scientists are generally interested in learning to use existing tools
effectively, rather than developing new tools.

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.

• Python packages: For scientific computing and computational modelling, we need


additional libraries (packages) that are not part of the Python standard library.
These allow us to create plots, operate on matrices, analyze data and use
specialized numerical methods. Eg. numpy, pyplot, PANDAS etc.

• SPYDER: default development environment used to write and execute Python


programs.

• Jupyter Notebook: another tool to develop Python programs.


9
How to Download Anaconda

• 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

All data in Python is stored as objects and programs manipulate objects.


Every object has a type that defines the kinds of things programs can do with it.
For example:
• The value 5 is an integer and it can be multiplied, added, etc.

• ‘Hello world’ is a string that can be searched, sliced.

There are two general types of objects:


• scalar (cannot be subdivided)

• non-scalar (have internal structure that can be accessed)

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()

The type of an object/value is sometimes called its class.


To find the type of a value (how Python will interpret the value) we can use the type()statement.
Example:

Note: Python recognizes the dot(.) as the decimal


point. Don’t use a comma, the interpreter does not
interpret it as a decimal.

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.

Variables are named locations that store values used in a program.

Variables have a name, a value and a type.

name of the variable a

value of the variable 2

type of the variable: int

23
Naming Variables

Python has strict rules for naming variables (identifiers).


Rules are:
• Identifiers must contain at least one character.
• The first character of an identifier must be an alphabetic letter (upper or lowercase) or an
underscore
• The remaining characters (if any) may be alphabetic characters (upper or lowercase), the
underscore, or a digit.
• No other characters (including spaces) are permitted in identifiers.
• A reserved word cannot be used as an identifier. A reserved word is a word with a special meaning,
such as int, float, etc.
• If you accidentally use a reserved word as a variable name, python will give an error.

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.

• Name your variables using lowercase letters.

• If your name includes multiple words, separate the words with an underscore.

• Name should not be too long or too short (meaningful!)

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

You might also like