Python Basics#
Here are some quick examples to refresh your understanding of Python code.
This is not meant to be a comprehensive tutorial.
We encourage you to explore ProjectPythia for additional Python resources and tutorials.
Please run these examples demonstrated today yourself!#
Press Shift+Enter to execute each code cell!
Feel free to make changes and explore!
Starting off - Hello World!#
print("Hello World!")
print('Hello Students!')
Loops#
for
loops are a staple of scientifc programming.
for i in range(10):
print(i)
Things to Note:#
Python defaults to counting starting from 0 rather than 1.
Function calls (such as
range
) always use parentheses.for
loops require colons as punctuation to start.Code blocks are indicated through indentations.
NOTE indentations need to be similar for each block of code.
print('start of loop')
for i in range(10):
print('in loop')
print('outside of loop')
Program Control - Conditional Statements#
For logical decisions, Python has an if
statement
if i > 2:
print("i is greater than 2!")
else:
print("i is less than 2!")
QUESTION Why is i
greater than 2? What value is i
? If you do not know, how could you determine the value?
Python also defines True
and False
logical conditions
i < 8
Python also includes compound statements and
and or
for i in range(10):
if i > 2 and i <= 8:
print(f"this is iteration number {i}.")
if i > 9 or i < 0:
print('this condition is never met')
Things to Note:#
if
statements can also use less than or equal to notation<=
The
and
statement uses formatted output
Basic Python Data Types#
A key strength of Python are the flexible data types, the core of which are noted below
QUESTION What data type is i
?
print(type(i))
Floating Point Numbers float
#
a = 10.58
b = 1e-9
print(a, type(a))
print(b, type(b))
Things to Note:#
print
statements can take multiple inputsfloat
numbers are include scientific notation
Character strings str
#
As we have been doing, you can use either single quotes ''
or double quotes ""
to denote strings
c = 'Argonne National Laboratory'
d = "Atmospheric Sciences"
e = "cloudy, hot, humid"
print(c)
print(d)
print(e)
print(type(c), type(d), type(e))
Lists#
A ordered container of objects denoted by square brackets
Note that objects can be any data type
mylist = [10, 20, 30, 40]
print(mylist, type(mylist))
Things to Note:
you can also iterate through lists
lists do not need to contain the same type of data!
newlist = [10, 20.5, 1e9, 'Python']
for var in newlist:
print(var, type(var))
Since Lists are ordered we can access items by index
newlist[1]
What Now?#
Now that the Python basics have been reintroduced, let’s take a look a specific libraries that are useful in scientific applications
Matplotlib - Scientific Data Visualization#
From the Matplotlib Documentation:
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible.
Create publication quality plots.
Make interactive figures that can zoom, pan, update.
Cus tomize visual style and layout.
Export to many file formats.
Embed in JupyterLab and Graphical User Interfaces.
Use a rich array of third-party packages built on Matplotlib.
Additional Tutorials:
# Data we wish to display
varA = [0, 1, 2, 3, 4]
varB = [0, 1, 2, 3, 4]
varC = [0, 1, 4, 9, 16]
import matplotlib.pyplot as plt # Convention is to import and rename. Best to stick with convention
# Create a Figure
fig = plt.figure()
# Add an axis to display data in
ax = fig.add_subplot(1, 1, 1)
# Plot the first set of data
ax.plot(varA, varB, label='y = x') # include a label!
# Plot the second set of data
ax.plot(varA, varC, label=r'y = x$^2$')
# Label your axes
ax.set_ylabel('Y Values [#]')
ax.set_xlabel('X Values [#]')
# Add a title
ax.set_title('First Matplotlib Plot')
# Add a legend
ax.legend(loc='upper left')
# Display!
plt.show()