Introduction to Python
Part 1
v0.7
Research Computing Services
Information Services & Technology
RCS Team and Expertise
Our Team Consulting Focus:
Scientific Programmers Bioinformatics
Systems Administrators Data Analysis / Statistics
Graphics/Visualization Specialists Molecular modeling
Account/Project Managers Geographic Information Systems
Special Initiatives (Grants) Scientific / Engineering Simulation
Maintains and administers the Shared Visualization
Computing Cluster
Located in Holyoke, MA
~19,000 CPUs running Linux
CONTACT US:
[email protected]SCC Python Tutorials
Intermediate/Advanced Topics
Introduction to Python Introduction to PyTorch
Numerical Python Multi-PyTorch
Introduction to Python Pandas Python Optimization
Pretty Python Coding Python Parallelization
About You
Working with Python already?
Have you used any other programming languages?
Why do you want to learn Python?
Python on the SCC
There is documentation for using Python on the SCC on the RCS website.
Use the module system to find Python:
Getting Python for Yourself: Anaconda
The most popular setup for personal computers
https://www.anaconda.com/download/
Anaconda is a packaged set of programs including the Python language,
a huge number of libraries, and several tools.
These include the Spyder development environment and Jupyter notebooks.
Anaconda can be used on the SCC, with some setup required.
Python 2 vs. 3
Python 2: released in 2000, Python 3 released in 2008
Python 2 is in “end-of-life” mode – no new releases or patches after Dec 31, 2019
No library support (e.g. pandas, numpy, etc.) after that date
For more: https://www.python.org/doc/sunset-python-2/
Py3 is not completely compatible with Py2
For learning Python these differences are almost negligible
Which one to learn?
Python 3
If your research group / advisor / boss / friends all use one version that’s probably the best one
for you to choose.
If you have a compelling reason to focus on one vs the other
Spyder – a Python development environment
Pros:
Faster development
Easier debugging!
Helps organize code
Increased efficiency
Cons
Learning curve
Can add complexity to smaller
problems
The Spyder IDE
Variable and file explorer
file editor
Python console
Tutorial Outline – Part 1
What is Python?
Operators
Variables
Functions
Classes
If / Else
Tutorial Outline – Part 2
Lists
Tuples and dictionaries
Modules
numpy and matplotlib modules
Script setup
Development notes
Tutorial Outline – Part 1
What is Python?
Operators
Variables
Functions
Classes
If / Else
What is Python?
Python…
…is a general purpose interpreted programming language.
…is a language that supports multiple approaches to software design,
principally structured and object-oriented programming.
…provides automatic memory management and garbage collection
…is extensible
…is dynamically typed.
By the end of the tutorial you will understand all of these terms.
Some History
“Over six years ago, in December 1989, I was looking for a "hobby"
programming project that would keep me occupied during the week
around Christmas…I chose Python as a working title for the project, being
in a slightly irreverent mood (and a big fan of Monty Python's Flying
Circus).”
–Python creator Guido Van Rossum, from the foreward to Programming Python (1st ed.)
Goals:
An easy and intuitive language just as powerful as major competitors
Open source, so anyone can contribute to its development
Code that is as understandable as plain English
Suitability for everyday tasks, allowing for short development times
Compiled Languages (ex. C++ or Fortran)
Interpreted Languages (ex. Python or R)
bytecode Python interpreter:
Source code files
Python interpreter follows bytecode
prog.py conversion
math.py instructions
python prog.py
A lot less work is done to get a program to start running compared with compiled
languages!
Python programs start running immediately – no waiting for the compiler to finish.
Bytecodes are an internal representation of the text program that can be efficiently run by
the Python interpreter.
The interpreter itself is written in C and is a compiled program.
Comparison
Interpreted (Python, R, Matlab…) Compiled (C++, Julia, Fortran…)
Faster development Longer development
Easier debugging Edit / compile / test cycle is longer!
Debugging can stop anywhere, swap in Harder to debug
new code, more control over state of Usually requires a special compilation
program
(almost always) takes more code to
(almost always) takes less code to get things done
get things done
Faster
Slower programs Compiled code runs directly on CPU
Sometimes as fast as compiled, rarely Compiler can optimize more
faster extensively
Less control over program behavior More control over program behavior
17
The Python Prompt
The standard Python prompt looks like this:
The IPython prompt in Spyder looks like this:
IPython adds some handy behavior around the standard Python prompt.
Tutorial Outline – Part 1
What is Python?
Operators
Variables
Functions
Classes
If / Else
Operators
Python supports a wide variety of operators which act like functions, i.e.
they do something and return a value:
Arithmetic: + - * / % **
Logical: and or not
Comparison: > < >= <= != ==
Assignment: =
Bitwise: & | ~ ^ >> <<
Identity: is is not
Membership: in not in
Try Python as a calculator
Go to the Python prompt.
Try out some arithmetic operators:
+ - * / % ** == ( )
Can you identify what they all do?
Try Python as a calculator
Go to the Python prompt.
Try out some arithmetic operators:
+ - * / % ** == ()
Operator Function
+ Addition
- Subtraction
* Multiplication
/ Division (Note: 3 / 4 is 0.75!)
% Remainder (aka modulus)
** Exponentiation
== Equals
More Operators
Try some comparisons and Boolean operators. True and False are the
keywords indicating those values:
Comments
# is the Python comment character. On
any line everything after the # character
is ignored by Python.
There is no multi-line comment
character as in C or C++.
An editor like Spyder makes it very easy
to comment blocks of code or vice-
versa. Check the Edit menu
Tutorial Outline – Part 1
What is Python?
Operators
Variables
Functions
Classes
If / Else
Variables
Variables are assigned values using the = operator
In the Python console, typing the name of a variable
prints its value
Not true in a script!
Visualize Assignment
Variables can be reassigned at any time
Variable type is not specified
Types can be changed with a reassignment
Variables cont’d
Variables refer to a value stored in memory and are created when first
assigned
Variable names:
Must begin with a letter (a - z, A - Z) or underscore _
Other characters can be letters, numbers or _
Are case sensitive: capitalization counts!
Can be any reasonable length
Assignment can be done en masse:
x = y = z = 1
Try these out!
Multiple assignments can be done on one line:
x, y, z = 1, 2.39, 'cat'
Variable Data Types
Python determines data types for variables based on the context
The type is identified when the program runs, using dynamic typing
Compare with compiled languages like C++ or Fortran, where types are identified by
the programmer and by the compiler before the program is run.
Run-time typing is very convenient and helps with rapid code development
But… it requires the programmer to do more code testing for reliability.
Variable Data Types
Available basic types:
Numbers: Integers and floating point (64-bit)
Complex numbers: x = complex(3,1) or x = 3+1j
Strings, using double or single quotes: "cat" 'dog'
Boolean: True and False
Lists, dictionaries, sets, and tuples
These hold collections of variables
Specialty types: files, network connections, objects
Custom types can be defined using Python classes.
Variable modifying operators
Some additional arithmetic operators that modify variable values:
Operator Effect Equivalent to…
x += y Add the value of y to x x=x+y
x -= y Subtract the value of y x =x-y
from x
x *= y Multiply the value of x x =x*y
by y
x /= y Divide the value of x by x =x/y
y
The += operator is by far the most commonly used of these.
Check a type
A built-in function, type(), returns the
type of the data assigned to a variable.
It’s unusual to need to use this in a program, but it’s
available if you need it.
Try this out in Python – do some
assignments and reassignments and
see what type() returns.
31
Strings
Strings are a basic data type in Python.
Indicated using pairs of single '' or
double "" quotes.
Multiline strings use a triple set of
quotes (single or double) to start and
end them.
32
Tutorial Outline – Part 1
What is Python?
Operators
Variables
Functions
Classes
If / Else
Functions
Functions are used to create pieces of code that can be used in a
program or in many programs.
The use of functions is to logically separate a program into discrete
computational steps.
Programs that make heavy use of function definitions tend to be easier to:
develop
debug
maintain
understand
Function name
Python functions Optional comma-separated
list of arguments (incoming
variables)
Keyword def
A code block
Optional return statement
The return value can be any Python type
If the return statement is omitted a special None value is still returned.
The arguments are optional but the parentheses are required!
Functions must be defined before they can be called.
Write a Function
In Spyder’s editor:
Define a function called mathcalc that takes 3 numbers as arguments and returns
their sum divided by their product.
Save the file, run it, and then call it from the console:
mathcalc(1,2,3) returns 1.0
mathcalc(4, -2.5, 3.0) returns -0.15
Which code sample is easier to read?
float avg(int a, int b, int c)
float avg(int a, int b, int c){ {
C: float sum = a + b + c ; or float sum = a + b + c ;
return sum / 3.0 ;} return sum / 3.0 ;
}
Matlab: function a = avg(x,y,z) function a = avg(x,y,z)
a = x + y + z ; or a = x + y + z ;
a = a / 3.0 ; a = a / 3.0 ;
end end
Which code sample is easier to read?
C
float avg(int a, int b, int c)
Most languages use special characters ({ } {
float sum = a + b + c ;
pairs) or keywords (end, endif) to indicate return sum / 3.0 ;
sections of code that belong to: }
Functions
Control statements like if
Loops like for or while Matlab
function a = avg(x,y,z)
a = x + y + z ;
Python instead uses the indentation that a = a / 3.0 ;
programmers use anyway for readability. end
The Use of Indentation
Python uses whitespace (spaces or tabs) to define code blocks.
Code blocks are logical groupings of commands. They are always
preceded by a colon :
def avg(x,y,z): A code block
all_sum = x + y + z
return all_sum / 3.0
def mean(x,y,z):
Another code block return (x + y + z) / 3.0
This pattern is consistently repeated throughout Python syntax.
Spaces or tabs can be mixed in a file but not within a code block.
Function Return Values
A function can return any Python value.
Function call syntax:
A = some_func() # some_func returns a value
Another_func() # ignore return value or nothing returned
b,c = multiple_vals(x,y,z) # return multiple values
Open function_calls.py for some examples
Function arguments
Function arguments can be required or optional.
Optional arguments are given a default value
def my_func(a,b,c=10,d=-1):
…some code…
To call a function with optional arguments:
Optional arguments can be used in the order they’re declared or out of
order if their name is used.
my_func(x,y,z) # a=x, b=y, c=z, d=-1
my_func(x,y) # a=x, b=y, c=10, d=-1
my_func(x,y,d=w,c=z) # a=x, b=y, c=z, d=w
Garbage collection
Variables defined in a function no longer have any “live” references to
them once the function returns.
These variables become garbage – memory that can no longer be used.
Python’s garbage collection operates to return that memory to the
operating system so it can be re-used.
There is no need to explicitly destroy or release most variables.
Some complex data types provide .close(), .clean(), etc. type functions. Use these where
available.
When does garbage collection occur?
It happens when Python thinks it should.
Part of the process is based on heuristics, i.e. tuned parameters in Python.
For the great majority of programs this is not an issue.
Programs that use plenty of small functions where intermediate results
are calculated tend to use less memory overall.
The takeaway: use functions, and plenty of them.
If you are having trouble with memory usage contact RCS for help or take
the Python Optimization tutorial to learn more.
Tutorial Outline – Part 1
What is Python?
Operators
Variables
Functions
Classes
If / Else
Classes
OOP: Object Oriented Programming
In OOP a class is a data structure that combines data with functions that
operate on that data.
An object is a variable whose type is a class
Also called an instance of a class
Classes provide a lot of power to help organize a program and can
improve your ability to re-use your own code.
“Class Car”
Object-oriented programming
Classes can contain data and methods
public interface
(internal functions).
Methods can call other code inside the
class to implement complex behavior.
This is a highly effective way of modeling
real world problems inside of a computer
program.
internal data and methods
Object-oriented programming Track via separate variables
boston_pop = 685094
boston_sq_km = 232.1
boston_num_colleges = 35
Python is a fully object oriented
programming (OOP) language. Boston
Population 685094
Some familiarity with OOP is needed to Area (km2) 232.1
understand Python data structures and # of colleges 35
libraries.
You can write your own Python classes
to define custom data types. A class lets you bundle
these into one variable
class Student:
def __init__(self, name, buid, gpa):
Writing Your Own self.name = name
self.buid = buid
Classes self.gpa = gpa
def has_4_0(self):
return self.gpa==4.0
me = Student("RCS Instructor","U0000000",2.9)
print(me.has_4_0())
Your own classes can be as simple or as complex as you need.
Define your own Python classes to:
Bundle together logically related pieces of data
Write functions that work on specific types of data
Improve code re-use
Organize your code to more closely resemble the problem it is solving.
Syntax for using Python classes
# Open a file. This returns a file object.
Create an object, which is a file = open('some_file.txt')
variable whose type is a
Python class. # Read a line from the text file.
line = file.readline()
Created by a call to the class # Get the filename
file.name # --> some_file.txt
or returned from a function.
Call a method for this object:
object_name.method_name(args…) Access internal data for this object:
object_name.data_name
Classes bundle data and functions
In Python, calculate the area of some shapes after defining some functions.
radius = 14.0
width_square = 14.0
a1 = AreaOfCircle(radius) # ok
a2 = AreaOfSquare(width_square) # ok
a3 = AreaOfCircle(width_square) # !! OOPS
If we defined Circle and Rectangle classes with their own area() methods…it is not
possible to miscalculate.
# create a Circle object with the radius value
c1 = Circle(radius)
r1 = Square(width_square)
a1 = c1.area()
a2 = r1.area()
When to use your own class
A class works best when you’ve done some planning and design work
before starting your program.
This is a topic that is best tackled after you’re comfortable with solving
programming problems with Python.
Some tutorials on using Python classes:
W3Schools: https://www.w3schools.com/python/python_classes.asp
Python tutorial: https://docs.python.org/3.6/tutorial/classes.html
Strings in Python
Python defines a string class – all strings in Python are objects.
This means strings have:
Their own internal (hidden) memory management to handle storage of the characters.
A variety of methods (functions) accessible once you have a string object in memory.
You can’t access string functions without a string – in Python the string
provides its own functions.
No “strcat” / “strcmp” / etc as in C
No “strlength” / “isletter” / etc as in Matlab
No “nchar” / “toupper” /etc as in R
String functions
In the Python console, create a string variable
called mystr
type: dir(mystr) mystr = 'Hello!'
mystr.upper()
Try out some functions: mystr.title()
mystr.isdecimal()
Need help? Try: help(mystr.isdecimal)
help(mystr.title)
The len() function
The len() function is not a string specific function.
It’ll return the length of any Python object that contains any
countable thing.
len(mystr) 6
In the case of strings it is the number of characters in the
string.
String operators
Try using the + and += operators with strings in the
Python console.
+ concatenates strings.
+= appends strings.
These are defined in the string class as functions that
operate on strings.
Index strings using square brackets, starting at 0.
String operators
Changing elements of a string by an index is not allowed:
Python strings are immutable, i.e. they can’t be changed.
%s means sub in
String Substitutions value
variable name
Python provides an easy way comes after a %
to stick variable values into
strings called substitutions
Variables are listed in the
substitution order inside ()
Syntax for one variable:
For more than one:
Printing: print('x: %s, y: %s, z:%s' % (xval,yval,2.0))
f-string Substitutions
f-strings are a more name = 'Boston'
school = f'{name} University'
contemporary way to format
strings.
Use a lowercase f before the first
quote. result = f'{mathcalc(1,2,3)}'
Put the names of variables, or
function calls, in pairs of { } inside
the strings.
Tutorial Outline – Part 1
What is Python?
Operators
Variables
Functions
Classes
If / Else
If / Else
If, elif, and else statements are used to implement conditional program
behavior
Syntax: if Boolean_value:
…some code
elif Boolean_value:
…some other code
else:
…more code
elif and else are not required – use them to chain together multiple
conditional statements or provide a default case.
Try out something like this in the Spyder
editor.
Do you get any error messages in the
console?
Try using an elif or else statement by
itself without a preceding if. What error
message comes up?
If / Else code blocks
Python knows a code block has
ended when the indentation is
removed.
Code blocks can be nested
inside others therefore if-elif-else
statements can be freely nested
within others.
Or used in functions…
File vs. Console Code Blocks
Python knows a code block has ended when the indentation is removed.
EXCEPT when typing code into the Python console. There an empty line
indicates the end of a code block.
This sometimes causes confusion when pasting code into the console.
Solution: try to avoid pasting more than a few lines into the Python console.