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

Tutorial 1

Uploaded by

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

Tutorial 1

Uploaded by

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

CHBE 230 - Python Crash Course - Part I

CHBE 230 2019W2


Computational Methods
Python Crash Course - Part I
CHBE 230 - Python Crash Course - Part I

What is programming ?
Programming = organizing tasks that a computer will do on
your behalf
The program tells the computer what to do using a series of
organized instructions
Instructions = computer words
All the instructions that a computer understands form a
language, we talk about programming language
We can ask the computer to perform a task with different
languages as much as we can talk to human beings with
different languages
Different programming languages offer different levels of
abstraction and/or of sophistication
CHBE 230 - Python Crash Course - Part I

What do we need for CHBE230 ?


Most of our programs will be rather simple (scripts, simple
functions, sometimes multiple scripts in a single Jupyter
notebook) and short (in general less than 100 lines, often
20-30 lines)
Basic variable types: scalar, vector, matrix, string
No input/output in files, our outputs are
either plots,
or displayed values of variables
Everything is contained in Anaconda with Python 3.7,
IPython through the Jupyter Console and Jupyter
Notebooks
Jupyter Notebooks are easy to use and enable us to have
text, images, Python codes and results in a single
document
CHBE 230 - Python Crash Course - Part I

Why do we program ?
Because the computer computes billions of times faster
that we do
However, remember the following

The computer is stupid


You are smart and in charge
and obedient
BUT
BUT
you are slow at computing
it is fast at computing
CHBE 230 - Python Crash Course - Part I

Objective of Python Crash Course


CHBE230 is about Computational Methods, not about
programming
However, solid Python skills will be a huge plus to pass
the course with a high mark, as all problems are
expected to be solved with Python
Python programming is certainly the easiest way to
program in science and engineering
It shares similarities with Fortran, C and C++
The 2 first tutorials are about learning the basic principles
of elementary Python programming and how to use
Jupyter
But the key is to practice by yourself, these 2 tutorials are
not enough, improving your Python programming
skills will only happen through practice
CHBE 230 - Python Crash Course - Part I

Python Crash Course - Part I


The Python language and Jupyter Lab
Basic math operations
Variables and data types
Basic output with formatted strings
Lists as well as indexing and slicing them
The Numpy package and how to works with arrays

Python Crash Course - Part II


Booleans and logic
Conditional statements and flow control
Loops and repetitive operations
Functions and modular programming
Basic input and output
Scientific visualization: How to make and customize plots
CHBE 230 - Python Crash Course - Part I

A naive but important few tips to get started

Save your file every 1-3 new lines

Test it every 1-3 new lines if possible (do not program


the whole script/function first and test it after, do it step
by step)
CHBE 230 - Python Crash Course - Part I

Two parts in the tutorial today

1. Following the presentation and practicing with the


console mode

2. Learning how to create Jupyter notebooks and solving 3


simple programming problems
CHBE 230 - Python Crash Course - Part I

Example 1: defining variables


1. Create a new Jupyter notebook and call it Prob1.ipynb
2. Create the 4 variables x=1.234567, y=10.01, z=27 and
today=‘Friday’
3. Print the 4 variables as follows
1. x with 4 digits after the decimal points and ‘x =‘ before the value
2. y without format and with ‘y =‘ before the value
3. z without format and with ‘z =‘ before the value
4. today without format and with ‘Today we are‘ before the value
4. Print the type of these 4 variables using the type
function with ‘type of x=‘ before (change x into the
variable name for the other 3 variables)
5. Overwrite variables x and z as x=‘ubc’ and z=27.
6. Print the value and type of x and z in a single print
instruction for each
CHBE 230 - Python Crash Course - Part I

Example 2: Manipulating lists


1. Create a new Jupyter notebook and call it Prob2.ipynb
2. Create a list of days of the week and print ‘Today we are’
followed by the day of the week as an element of the
created list accessed with the [] operator
3. Set the number of items in the list with the len() operator to
a separate variable and print its value
4. Copy the list days in another list called days2 and sort
days2 alphabetically, print the list days2
5. Insert another ‘Monday’ at the end of list days, count the
number of ‘Monday’ and ‘Friday’ in days and print the
values
6. Remove ‘Saturday’ and ‘Sunday’ from the list days and print
the list
7. Remove the last item in the list days and print the list
CHBE 230 - Python Crash Course - Part I

Example 3: Using numpy


1. Create a new Jupyter notebook and call it Prob3.ipynb
2. Import the numpy package as np
3. Create a row vector v of 5 elements 1.,2.,3.,4.,5. using np.array
and [[]] (double brackets), print the vector, its size and its
shape
4. Change its shape to a column vector, and again print the
vector, its size and its shape
5. Compute the mean and the max of v and print them
6. Create a row vector t of equally spaced valued from 0. to 10.
with a step of 0.5 and print it. Note the last value, this is a round
off error problem !
7. Re-program it such that it includes 10.
8. Overwrite the row vector t with equally spaced valued from 0.
to 10. with 7 intervals. Note that there is no upper bound issue
here.
CHBE 230 - Python Crash Course - Part I

10. Create a 3x3 identity matrix and a 2x3 with 2s on the first row
and 4s on the second row. Print the 2 matrices
11. Extract the 2nd column vector of the 2x3 matrix, reshape it to a
column vector and print it
12. Vertically stack the 3x3 identity matrix on the top of the 2x3
matrix and print it
13. Delete the 2 first rows of the 5x3 matrix to make it square again.
Print the resulting 3x3 matrix
14. Compute the element-wise exponential of the 3x3 matrix using
vectorized computations and print it

You might also like