Biocomplexity Institute
Indiana University
Bloomington, IN 47405
USA
�Generous funding by:�NIH U24 EB028887, NIH R01 GM122424, NIH R01 GM123032, NIH P41 GM109824, NSF 1720625
The workshop will begin at 10:00 EDT
Screensharing and microphones have been disabled
Please submit questions / concerns vis zoom chat
User support will be done in zoom breakout rooms.
CompuCell3D User Training Workshop�Introduction to Python Programming
CompuCell3D User Training Workshop�Introduction to Python Programming
Biocomplexity Institute
Indiana University
Bloomington, IN 47405
USA
�Generous funding by:�NIH U24 EB028887, NIH R01 GM122424, NIH R01 GM123032, NIH P41 GM109824, NSF 1720625
Instructor: Dr. Bobby Madamanchi,
Moderator: Mr. Hayden Fennell
Why Learn Python?
Topics
Keys to Success
In software, there is always something new to learn
At least once today, we’ll come across something that I don’t know… and that’s ok!
A coder’s best friends: manuals, other coders (e.g., Stack Overflow, open source software), and a trusty debugger
Focus on what you want to do
Experiment with different ways of how you do it
Feeling overwhelmed? Stop, take a breath, and then break up a task into a set of simpler, more manageable sub-tasks
nanoHUB Environment
Logging in to nanoHUB
Go to nanohub.org
Hit Login
Create a nanoHUB Account
Go to https://nanohub.org
From ‘Menu’, select ‘sign up’
Choose to sign up with your university of choice (if it is a US university) or sign in with Google.
Choose an appropriate username / password / user info
If its your first time signing up you will need to provide some biographical information
Logging in to nanoHUB
Sign in using your preferred method
Launch The Jupyter Notebook App on nanoHUB
https://nanohub.org/tools/jupyter70
Click the Collect button to add Jupyter to your favorites & then click Launch Tool
Jupyter Notebook (202105)
Opening a New Notebooks
Click New and select Python3 to launch a new notebook
Using Jupyter in nanoHUB
Saving Notebooks
Exercise: calculating distance
��Problem
Given: Two positive integers a and b,
Return: The integer corresponding to the sum of a and b.
You’ve just used Scalar Types
Scalar Types
Type Conversion
E.g., truncate a float to an integer
int(5.1)
>>> 5
E.g., convert an integer to float
float(5)
>>> 5.0
EXPRESSIONS
<object> <operator> <object>
OPERATORS ON ints and floats
→ the sum
→ the difference
→ the product
→ division
→ the remainder when i is divided by j
if both are ints, result is int
if either or both are floats, result is float
result is float
Exercises
How to do math calculations
Exercises
Assigning values to variables
pi =
3.14159
pi_approx = 22/7
Can change the values stored in variables
pi
radius
area
3.14
2.2
15.1976
3.2
pi = 3.14
radius = 2.2
area = pi*(radius**2) radius = radius+1
Exercises
STRINGS
hi = "hello there"
name = "ana"
greet = hi + name
greeting = hi + " " + name
silly = hi + " " + name * 3
OUTPUT: print
x = 1 | | |
print(x) | ||
x_str = str(x) | ||
print("my fav num is", | x, ".", | "x =", x) |
print("my fav num is " | + x_str | + ". " + "x = " + x_str) |
COMPARISON OPERATORS ON �int, float, string
i > j
i >= j
j > i
i <= j
i == j ⇾ equality test, True if i is the same as j
i != j ⇾ inequality test, True if i not the same as j
LOGIC OPERATORS ON bools
True if a is False False if a is True
not a ⇾
a and b ⇾
a or b ⇾
True if both are True
True if either or both are True
A | B | A and B | A or B |
True | True | True | True |
True | False | False | True |
False | True | False | True |
False | False | False | False |
CONTROL FLOW - BRANCHING
if <condition>:
<expression>
<expression>
...
if <condition>:
<expression>
<expression>
...
else:
<expression>
<expression>
...
if <condition>:
<expression>
<expression>
...
elif <condition>:
<expression>
<expression>
...
else:
<expression>
<expression>
...
BLOCKS / INDENTATION
print("x
and y are
equal"
if y != 0
print("therefore
, x / y is",
x/y
print("x
is
smaller"
else:
print("thanks!")
if x == y:
print("x and y are equal")
if y != 0:
print("therefore, x / y is", x/y) elif x < y:
print("x is smaller")
print("y is smaller")
Assignment and Comparison: �= vs ==
if x == y:
print("x and y are equal") if y != 0:
print("therefore, x / y is", x/y) elif x < y:
print("x is smaller") else:
print("y is smaller") print("thanks!")
Exercise: determine if a number is even or odd
Choose a number. Depending on whether the number is even or odd, print out an appropriate message to the user.
Hint: how does an even / odd number react differently when divided by 2?
Extras:
CONTROL FLOW:
while LOOPS
while <condition>:
<expression>
<expression>
...
evaluates to a Boolean
is False
CONTROL FLOW:
while and for LOOPS
# more complicated with while loop n = 0
while n < 5:
print(n) n = n+1
# shortcut with for loop for n in range(5):
print(n)
for LOOPS
for <variable> in range(<some_num>):
<expression>
<expression>
...
takes a value
gets the next value in the sequence
range(start,stop,step)
mysum = 0
for i in range(7, 10): mysum += i
print(mysum)
mysum = 0
for i in range(5, 11, 2): mysum += i
print(mysum)
Exercise: Calculate the sum of number
Problem
Given: Two positive integers a and b (a<b<10000).
Return: The sum of all odd integers from a through b, inclusively.
Sample Dataset
100 200
Sample Output
7500
break STATEMENT
while <condition_1>: while <condition_2>:
<expression_a>
break
<expression_b>
<expression_c>
break STATEMENT
mysum
+=
i
if
mysum
== 5
break
mysum += 1 print(mysum)
mysum = 0
for i in range(5, 11, 2): mysum
if mysum == 5:
break
for VS while LOOPS
while loops
for loops
break
using a while loop
Module Questionnaire
Please take a minute or two to let us know about your experience with this module by filling out the brief zoom survey
Feel free to provide additional comments and suggestions in the slack or by email to us as well (hfennel@iu.edu)
Funding Sources: NIH U24 EB028887, NSF 2120200, 2000281, 1720625
Previous Funding : NIH R01 GM122424