Introduction To Python
Introduction To Python
Why’N’How 3/19/2020
Jay Patel
Outline
• What is python?
• Why use python?
• How to use python?
• IDE
• Basic types
• Containers
• Basic control flow
• Scientific Python
• Questions?
Outline
• What is python?
• Why use python?
• How to use python?
• IDE
• Basic types
• Containers
• Basic control flow
• Scientific Python
• Questions?
What is Python?
• Interpreted (i.e. non-compiled), high-level programming language
• Compiler translates to source code to machine code before executing script
• Interpreter executes source code directly without prior compilation
• Open-source (free) and community driven
Outline
• What is python?
• Why use python?
• How to use python?
• IDE
• Basic types
• Containers
• Basic control flow
• Scientific Python
• Questions?
Why use Python?
• PROs:
• Designed to be intuitive and easy to program in (without sacrificing power)
• Open source, with a large community of packages and resources
• One of the most commonly used programming languages in the world
• “Tried and True” language that has been in development for decades
• High quality visualizations
• Runs on most operating systems and platforms
• CONs:
• Slower than “pure” (i.e. compiled) languages like C++
• Smaller/specialized packages might not be well tested / maintained
Outline
• What is python?
• Why use python?
• How to use python?
• IDE
• Basic types
• Containers
• Basic control flow
• Scientific Python
• Questions?
How to use Python
• Install python 3 distribution for your system
• Note: Python 2.7 is no longer maintained and you should do your best to
transition all old code to python 3!
• Install useful dependencies
• pip install numpy, matplotlib, scipy, nibabel, pandas, sklearn, …
• Download an IDE of your choice
• Visual Studio Code
• https://stackoverflow.com/questions/81584/what-ide-to-use-for-python
• Or run interactively in a Jupyter notebook
IDE
Outline
• What is python?
• Why use python?
• How to use python?
• IDE
• Basic types
• Containers
• Basic control flow
• Scientific Python
• Questions?
Types Basic Operations
• Numbers • Operators (non-exhaustive list)
• Integer • + #addition
• - #subtraction
• Float • * #multiplication
• Complex • ** # power
• Boolean • % # modulus
• “in” # check if element is in container
• String • Functions
• (Custom) operations that take one or more
pieces of data as arguments
• len(‘world’)
• Methods
• Functions called directly off data using the “.”
operator
• ‘Hello World”.split()
Examples: Numerical types
Examples: Booleans and Strings
Variable Assignment
Outline
• What is python?
• Why use python?
• How to use python?
• IDE
• Basic types
• Containers
• Basic control flow
• Scientific Python
• Questions?
Data Containers (aka Objects)
• Lists (mutable set of objects)
• var = ['one', 1, 1.0]
• Tuples (immutable set of objects)
• var = ('one', 1, 1.0)
• Dictionaries (hashing arbitrary key names to values)
• var = {'one': 1, 'two': 2, 1: 'one', 2: 'two’}
• Etc.
• Each of the above has its own set of methods
When to use one container over another
• Lists
• If you need to append/insert/remove data from a collection of (arbitrary
typed) data
• Tuples
• If you are defining a constant set of values (and then not change it), iterating
over a tuple is faster than iterating over a list
• Dictionaries
• If you need a key:value pairing structure for your dataset (i.e. searching for a
persons name (a key) will provide their phone number (a value))
Indexing through Containers
Scripting vs Functions vs Object Oriented
Approach
Outline
• What is python?
• Why use python?
• How to use python?
• IDE
• Basic types
• Containers
• Basic control flow
• Scientific Python
• Questions?
Basic Control Flow: Conditional Statements
• Use if-elif-else statements to perform certain actions only if they meet
the specified condition
Basic Control Flow: Loops
• Use to iterate over a container
List Comprehension
• Pythonic way to compress loops into a single line
• Slight speed gain to using list comprehension
• Normal loop syntax:
• For item in list:
if conditional:
expression
• List comprehension syntax:
• [expression for item in list if conditional]
List Comprehension
Variable Naming Conventions
• Very important to name your variables properly
• Helps others read your code (and helps you read your own code too!)
• Will help mitigate issues with variable overwriting/overloading
Style Conventions
• PEP8 – Style Guide for Python Code
• https://www.python.org/dev/peps/pep-0008/
• Extremely thorough resource on how to standardize your coding style
• Covers:
• Proper indentation, variable naming, commenting, documentation, maximum
line lengths, imports, etc.
Outline
• What is python?
• Why use python?
• How to use python?
• IDE
• Basic types
• Containers
• Basic control flow
• Scientific Python
• Questions?
Scientific Python
• For high efficiency, scientific computation and visualization, need to
install external packages
• NumPy
• Pandas
• Matplotlib
• SciPy
• These packages (among countless others like sympy, scikit-image,
scikit-learn, h5py, nibabel, etc.) will enable you to process high
dimensional data much more efficiently than possible using base
python
NumPy: N-dimensional arrays
• Specifically designed for efficient/fast computation
• Should be used in lieu of lists/arrays if working with entirely numeric
data
• Matlab users -> https://
docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html
• Extremely comprehensive resource comparing matlab syntax to numpy/scipy
NumPy: creating arrays
NumPy: Copies vs. Views
• Views are created by slicing through an array
• This does not create a new array in memory
• Instead, the same memory address is shared by the original array and new
sliced view
• Changing data on the view will change the original array!
• Use the copy function to copy an array to a completely new location
in memory
NumPy: Copies vs. Views
NumPy: reductions across specific
dimensions