Intro To Python Part 2
Intro To Python Part 2
Part 2
v0.7
Lists
Tuples and dictionaries
Modules
numpy and matplotlib modules
Script setup
Development notes
Lists
A Python list is a general purpose 1-dimensional container for variables.
i.e. it is a row, column, or vector of things
Variables in a list can be of any type at any location, including other lists.
Try dir(list_1)
Lists can have their elements overwritten or deleted (with the del) command.
y = x[:] or y=list(x)
Lists
Tuples and dictionaries
Modules
numpy and matplotlib modules
Script setup
Development notes
Tuples
Dictionaries are another basic Python data type that are tremendously
useful.
Keys can be primitive types (numbers), strings, tuples, and some custom
data types
Basically, any data type that is immutable
Lists and dictionaries cannot be keys but they can stored as values.
Lists
Tuples and dictionaries
Modules
numpy and matplotlib modules
Script setup
Development notes
Modules
A tool, pip, can be used to install packages from it into your Python setup.
Anaconda provides a similar tool called conda
You should always do your due diligence when using software from a
place like PyPI. Make sure it does what you think it’s doing!
Python Modules on the SCC
Python modules should not be confused with the SCC module command.
For the SCC there are instructions on how to install Python software for
your account or project.
# in another Python
# script __name__ myfuncs
import myfuncs (i.e. the file name)
myfuncs.py
# called directly
__name__ __main__
python myfuncs.py
myfuncs.py
The __name__ attribute def get_odds(lst):
''' Gets the odd numbers in a list.
Lists
Tuples and dictionaries
Modules
numpy and matplotlib modules
Script setup
Development notes
A brief into to numpy and matplotlib
Open numpy_matplotlib_fft.py
Lists
Tuples and dictionaries
Modules
numpy and matplotlib modules
Script setup
Development notes
Writing Quality Pythonic Code
Cultivating good coding habits pays off in many ways:
Easier and faster to write
Easier and faster to edit, change, and update your code
Other people can understand your work
Lists
Tuples and dictionaries
Modules
numpy and matplotlib modules
Script setup
Development notes
Function, class, and variable naming
An IDE (like Spyder) will help you fill in longer names so there’s no extra
typing anyway.
Give your functions and variables names that reflect their meaning.
Once a program is finished it’s easy to forget what does what where
An example development process
Work to develop your program.
Do some flowcharts, work out algorithms, and so on.
Write some Python to try out a few ideas.
Get organized.
Write a “1st draft” version that gets most of what’s needed done.
Once the code is testing well add command line arguments and remove hard-
coded values
Finally (e.g. to run as an SCC batch job) test run from the command line.
Spyder command line arguments
Click on the Run menu and choose
Configuration per file
After a Python module is loaded just type python followed by the script
name followed by script arguments.
Where to get help…