Class - 1 - Master - Jupyter Notebook
Class - 1 - Master - Jupyter Notebook
Python 1 - Overview
Bootcamp will cover Python fundamentals while making a music playlist program
Jupyter Notebook
This is a web-based application (runs in the browser) that is used to interpret Python code.
To add more code cells (or blocks) click on the '+' button in the top left corner
There are 3 cell types in Jupyter:
Code: Used to write Python code
Markdown: Used to write texts (can be used to write explanations and other key
information)
NBConvert: Used convert Jupyter (.ipynb) files to other formats (HTML, LaTex, etc.)
To run Python code in a specific cell, you can click on the 'Run' button at the top or press
Shift + Enter
The number sign (#) is used to insert comments when coding to leave messages for yourself
or others. These comments will not be interpreted as code and are overlooked by the program
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 1/12
1/21/2021 Class_1_Master - Jupyter Notebook
Data Types
Four primitive types in Python
1. Integers
2. Booleans
3. Floats
4. Strings
Types may be changed using int(), str(), float(), and bool() methods
In [1]: # The type() function will return the data type of the data passed to it
type("Hello!")
Out[1]: str
In [2]: type(True)
Out[2]: bool
In [3]: type(3.14)
Out[3]: float
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 2/12
1/21/2021 Class_1_Master - Jupyter Notebook
In [4]: type(3)
Out[4]: int
<class 'float'>
3
False
False
Variables
May consist of letters, numbers, and underscores, but not spaces.
Cannot start with a number.
Avoid using Python keywords (for, if, and, or, etc.)
Be careful when using 1s and lower case ls, as well as 0s and Os.
Keep it short.
Example: phone_num = 647606
In Jupyter, we need to be very careful when running cells when we use variables!
In [8]: # In the code below, the variable `hours_worked` has been assigned
# an integer value of 10.
hours_worked = 10
In [9]: print(hours_worked)
10
Math Operators
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 3/12
1/21/2021 Class_1_Master - Jupyter Notebook
Addition, Subtration, Multiplication and Division may be done using basic math operators (+ , -
, * , /,%).
Python will also try to interpret your code with other data types
(+) may be used with strings!
However, the usability is limited, instead we will introduce functions
In [10]: # Create two variables, price1 and price2 that have float values representing the
price1 = 3.40
price2 = 2.51
5.91
Functions
We cannot perform all math on strings, as it sometimes doesn't make sense
Instead we use functions, prewritten sets of instructions
To use a function on a string, we use the dot operator
The general form will be string_variable.function_name(function_arguments)
We'll talk more about functions later in the session
print(employment.index("work"))
print(employment.split(" "))
print(employment.replace("python", "Finance"))
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 4/12
1/21/2021 Class_1_Master - Jupyter Notebook
In [13]: # A boolean can only have one of two values. Either they are "True" or "False".
# Variables "yes" and "no" have been assigned boolean variables of "True" and "Fa
yes = True
no = False
In [14]: # Boolean variables are generally used for conditional statements such as an if s
# The below lines of code uses boolean variables to determine whether or not the
if yes:
print("True Statement!")
if no:
print("Will not print")
True Statement!
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 5/12
1/21/2021 Class_1_Master - Jupyter Notebook
In [16]: # if else statments can also be used with math or anything really (like strings)!
Small Department: 10
In [17]: # While loops will keep running a loop of code until the intial condition is no l
# It is important to always have a breaking condition to stop the loop so it does
limit = 10
dept_size = 0
while dept_size < limit:
print(dept_size)
dept_size += 1
if dept_size == 8:
break # The 'break' statement in Python is used to close/end a loop
0
1
2
3
4
5
6
7
Lists
Collection of items in a particular order
They are used to store data and can be assigned to variables just like integers and strings
Indexing (order) starts from 0
Accessing items in a list can be done with square brackets ([ ])
Items can be easily added to lists using append() and insert() methods
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 6/12
1/21/2021 Class_1_Master - Jupyter Notebook
In [18]: # Lists are a collection of data. List numberings always start from 0.
RBC
BMO
['RBC', 'CIBC', 'TD']
['RBC']
['TD', 'BMO']
BMO
['Scotiabank', 'CIBC', 'TD', 'BMO']
In [20]: # add value to the start of a list - First Nations Bank of Canada
banks.insert(0, "FNBC")
print(banks)
Out[20]: 6
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 7/12
1/21/2021 Class_1_Master - Jupyter Notebook
Dictionaries
Collection of key-value pairs
No positions as with lists, values stored at specific key
keys can be of any data type
Accessing values in a dictionary can still be done with square brackets ([ ])
Declared using braces ({ })
In [22]: # collection of "data" which is unordered, changeable, and not indexed. They have
employee = { "name": "Peter", "employee_num": 314425, "workplace": "RBC"}
# Here, 'name', 'employee_num', and 'department' are keys, and 'Peter', '314425',
print(employee)
Out[23]: 'Peter'
TD
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 8/12
1/21/2021 Class_1_Master - Jupyter Notebook
For Loops
Execute a block of code once for each item in collection (List/Dictionary)
Declare temporary variable to iterate through collection
Can be used in combination with IF statements
FNBC
Scotiabank
CIBC
TD
CWB
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 9/12
1/21/2021 Class_1_Master - Jupyter Notebook
name
employee_num
workplace
name: Peter
employee_num: 314425
workplace: TD
0
1
2
3
4
1
3
5
7
9
11
13
15
17
19
21
Functions
Named blocks of code that do one specific job
Functions are also referred to as methods
Prevents rewriting of code that accomplishes the same task
Keyword def used to declare functions
Variables may be passed to functions
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 10/12
1/21/2021 Class_1_Master - Jupyter Notebook
In [33]: # In this function 'name', 'employee_num', and 'department' are required values t
def description(name, employee_num, workplace):
print(f"{name} - Employee Number: {employee_num} - works at: {workplace}")
Classes
NOTE: We cover classes in the next section, but here's a primer if you're interested
Object-orientated programming approach popular and efficient
Define classes of real-world things or situations (can be thought of as creating your own data
type)
Attributes of various data types
Functions inside of a class are the same except called methods
Methods may be accessed using the dot operator
Instanciate objects of your classes
__init()__ method used to prefill attributes
Capitalize class names
User Input
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 11/12
1/21/2021 Class_1_Master - Jupyter Notebook
OPTIONAL: Not covred during the bootcamp. Just some extra content if you're interested!
Pauses your program and waits for the user to enter some text
Variable used with Input() will be a string even if user inputs an integer
Will need to make use of type casting.
In [ ]:
localhost:8888/notebooks/BCHPython1/Python1/Class_1_Master.ipynb# 12/12