2.4. Lecture 3: Python 1¶
Before this class you should:
Read Think Python:
Preface;
Chapter 1: The way of the program;
Chapter 2: Variables, expressions and statements; and
Chapter 3: Functions
Before next class you should:
Read Think Python:
Chapter 5: Conditionals and recursion;
Chapter 6: Fruitful functions; and
Chapter 7: Iteration
Note taker: Zach McMurray
2.4.1. Python Review Class Overview¶
Setting up JupyterHub using the school VPN
Working through Python 1a
Focus: Operations, variable types/names, and comments
Working through Python 1b
Focus: Functions
2.4.2. VPN Setup¶
Visit
vpn.uoguelph.caLog in using Single Sign-On
Select Split Tunnel
Enable the VPN
2.4.3. JupyterHub¶
Access JupyterHub at
engg3130.soe.uoguelph.caThe system syncs with GitHub every 15 minutes
JupyterHub contains:
Lecture notes
Homework details
The Public folder is read-only
Copy files before starting work
Open a new terminal with
File -> New -> TerminalEnter:
cp -r public/homework/homework## work
2.4.4. Python Concepts¶
Work through the following notebook: Python 1a.ipynb
Note: All students should complete this notebook.
Jupyter Notebook Basics
Jupyter notebooks are divided into two types of cells:
Markdown – text, links, and math
Code – Python code
Two modes for cells:
Edit Mode
Used to modify the contents of a cell
Indicated by a green border
Press
Enterto enter Edit Mode
Command Mode
Used to run cells or change cell types
Indicated by a blue border
Press
Escto enter Command ModeCommon shortcuts:
Shift + Enter: Run cell and move to nextCtrl + Enter: Run cell and stay in place
Basic Python Operations
Python supports standard arithmetic operators:
Addition:
+Subtraction:
-Multiplication:
*Division:
/Exponentiation:
**
Common Variable Types
int:number = 5
float:number = 5.3
str:text = "engg3130"
bool:value = True
Use type() to check a variable’s type.
Variables and Naming Rules
Variable names:
Must start with a letter or underscore
Cannot start with a number
Cannot be Python keywords (e.g.,
if,for,while)Are case-sensitive (
valueandValueare different)
The left-hand side of an assignment must always be a variable name, not an expression.
Strings are a unique type of variable with their own operations.
Example:
course = "ENGG3410"
String operations:
Concatenation using
+:"Hello" + " World"
Repetition using
*:"A" * 5
Indexing (starting at 0):
course[0] # 'E' course[1] # 'N'
Interactive Mode vs Script Mode
Interactive mode:
Evaluates and displays the result of the last line
Script mode:
Can be called in other cells
Does not automatically display results
Comments
#is used for comments:# This is a comment
Work through the following notebook: Python 1b.ipynb
Note: All students should complete this notebook.
A function is a reusable block of code that performs a specific task.
Why Use Functions?
Reduce repeated code
Improve readability
Break complex problems into smaller pieces
New functions are defined using the def keyword.
Basic Function Structure
Functions have three main parts:
Function name
Arguments (parameters)
Return value (optional)
General syntax:
def function_name(arguments):
function_body
return value
A function is executed by calling it using its name followed by parentheses:
function_name(arguments)
Important Ideas for Functions
Use
importto access additional functionsFunction headers must end with a colon (
:)Code inside a function must be indented
Functions can call other functions
Variables created inside a function exist only within that function
Types of Functions
Fruitful functions
Return a value using
return
Void functions
Perform an action but return no value