SlideShare ist ein Scribd-Unternehmen logo
1 von 53
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
• Integer
• Float
• Complex
• Boolean
• String
• Operators (non-exhaustive list)
• + #addition
• - #subtraction
• * #multiplication
• ** # power
• % # modulus
• “in” # check if element is in container
• 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
• Same applies for most numpy functions
• np.mean, np.argmax, np.argmin, np.min, np.max, np.cumsum, np.sort, etc.
Other useful Scientific Python packages
• Pandas
• Powerful data analysis and manipulation tool
• Matplotlib
• Matlab style plotting
• Scipy
• Works with NumPy to offer highly efficient matrix processing functions (i.e.
signal processing, morphologic operations, statistics, linear algebra, etc.)
• Nibabel
• Efficient loading/saving of NifTI format volumes
• …
Making Pandas DataFrame
Filtering DataFrame
Visualizations in Python
• Matplotlib is the standard
plotting library and works
very similarly to Matlab
Visualizations in Python
Visualizations in Python
Visualizations in Python: Pandas DataFrame
Visualizations in Python: Pandas DataFrame
Visualizations in Python: Pandas DataFrame
Normal overlapping histogram Stacked histogram for easier viewing
NifTI volume processing
• NifTI is a very common medical imaging format
• NifTI strips away all patient information usually in dicom header making it an
excellent format for data processing
• NiBabel package to load/save as nifti
NifTI volume processing
NifTI volume processing
Parallelizing Operations
• If processing of patients can be done independently of each other,
want to parallelize operation across CPUs to maximize efficiency
Viewing 3D NumPy arrays in Python
Simple machine learning with scikit-learn
• Sklearn contains machine learning implementations
• Regressions (linear, logistic)
• Classification (Random Forest, SVM)
• Dimensionality reduction (PCA)
• Clustering (k-means)
• Etc.
Linear Regression to predict Diabetes
Linear Regression to predict Diabetes
Outline
• What is python?
• Why use python?
• How to use python?
• IDE
• Basic types
• Containers
• Basic control flow
• Scientific Python
• Questions?
Thanks for listening!
• Questions?

Weitere ähnliche Inhalte

Ähnlich wie Introduction_to_Python.pptx

Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
Chetan Giridhar
 

Ähnlich wie Introduction_to_Python.pptx (20)

Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, Bihar
 
Pa2 session 1
Pa2 session 1Pa2 session 1
Pa2 session 1
 
Python
PythonPython
Python
 
Introduction to Python and Django
Introduction to Python and DjangoIntroduction to Python and Django
Introduction to Python and Django
 
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
Introduction to Jupyter notebook and MS Azure Machine Learning StudioIntroduction to Jupyter notebook and MS Azure Machine Learning Studio
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
 
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
Introduction to Jupyter notebook and MS Azure Machine Learning StudioIntroduction to Jupyter notebook and MS Azure Machine Learning Studio
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
 
Intro to Python for C# Developers
Intro to Python for C# DevelopersIntro to Python for C# Developers
Intro to Python for C# Developers
 
Basics of python programming
Basics of python programmingBasics of python programming
Basics of python programming
 
Python-Basics.pptx
Python-Basics.pptxPython-Basics.pptx
Python-Basics.pptx
 
Raspberry using Python Session 1
Raspberry using Python Session 1Raspberry using Python Session 1
Raspberry using Python Session 1
 
Python ml
Python mlPython ml
Python ml
 
prakash ppt (2).pdf
prakash ppt (2).pdfprakash ppt (2).pdf
prakash ppt (2).pdf
 
Tutorial on-python-programming
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
 
Performance and Abstractions
Performance and AbstractionsPerformance and Abstractions
Performance and Abstractions
 
Python programming lab 23
Python programming lab 23Python programming lab 23
Python programming lab 23
 
Travis Oliphant "Python for Speed, Scale, and Science"
Travis Oliphant "Python for Speed, Scale, and Science"Travis Oliphant "Python for Speed, Scale, and Science"
Travis Oliphant "Python for Speed, Scale, and Science"
 
Austin Python Learners Meetup - Everything you need to know about programming...
Austin Python Learners Meetup - Everything you need to know about programming...Austin Python Learners Meetup - Everything you need to know about programming...
Austin Python Learners Meetup - Everything you need to know about programming...
 
Programming with Python: Week 1
Programming with Python: Week 1Programming with Python: Week 1
Programming with Python: Week 1
 
Introduction about Python by JanBask Training
Introduction about Python by JanBask TrainingIntroduction about Python by JanBask Training
Introduction about Python by JanBask Training
 
Introduction to Python Programming
Introduction to Python Programming Introduction to Python Programming
Introduction to Python Programming
 

Kürzlich hochgeladen

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

Introduction_to_Python.pptx

  • 2. Outline • What is python? • Why use python? • How to use python? • IDE • Basic types • Containers • Basic control flow • Scientific Python • Questions?
  • 3. Outline • What is python? • Why use python? • How to use python? • IDE • Basic types • Containers • Basic control flow • Scientific Python • Questions?
  • 4. 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
  • 5. Outline • What is python? • Why use python? • How to use python? • IDE • Basic types • Containers • Basic control flow • Scientific Python • Questions?
  • 6. 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
  • 7. Outline • What is python? • Why use python? • How to use python? • IDE • Basic types • Containers • Basic control flow • Scientific Python • Questions?
  • 8. 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
  • 9. IDE
  • 10. Outline • What is python? • Why use python? • How to use python? • IDE • Basic types • Containers • Basic control flow • Scientific Python • Questions?
  • 11. Types Basic Operations • Numbers • Integer • Float • Complex • Boolean • String • Operators (non-exhaustive list) • + #addition • - #subtraction • * #multiplication • ** # power • % # modulus • “in” # check if element is in container • 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()
  • 15. Outline • What is python? • Why use python? • How to use python? • IDE • Basic types • Containers • Basic control flow • Scientific Python • Questions?
  • 16. 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
  • 17. 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))
  • 19. Scripting vs Functions vs Object Oriented Approach
  • 20. Outline • What is python? • Why use python? • How to use python? • IDE • Basic types • Containers • Basic control flow • Scientific Python • Questions?
  • 21. Basic Control Flow: Conditional Statements • Use if-elif-else statements to perform certain actions only if they meet the specified condition
  • 22. Basic Control Flow: Loops • Use to iterate over a container
  • 23. 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]
  • 25. 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
  • 26. 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.
  • 27. Outline • What is python? • Why use python? • How to use python? • IDE • Basic types • Containers • Basic control flow • Scientific Python • Questions?
  • 28. 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
  • 29. 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
  • 31. 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
  • 33. NumPy: reductions across specific dimensions • Same applies for most numpy functions • np.mean, np.argmax, np.argmin, np.min, np.max, np.cumsum, np.sort, etc.
  • 34. Other useful Scientific Python packages • Pandas • Powerful data analysis and manipulation tool • Matplotlib • Matlab style plotting • Scipy • Works with NumPy to offer highly efficient matrix processing functions (i.e. signal processing, morphologic operations, statistics, linear algebra, etc.) • Nibabel • Efficient loading/saving of NifTI format volumes • …
  • 37. Visualizations in Python • Matplotlib is the standard plotting library and works very similarly to Matlab
  • 40. Visualizations in Python: Pandas DataFrame
  • 41. Visualizations in Python: Pandas DataFrame
  • 42. Visualizations in Python: Pandas DataFrame Normal overlapping histogram Stacked histogram for easier viewing
  • 43. NifTI volume processing • NifTI is a very common medical imaging format • NifTI strips away all patient information usually in dicom header making it an excellent format for data processing • NiBabel package to load/save as nifti
  • 46. Parallelizing Operations • If processing of patients can be done independently of each other, want to parallelize operation across CPUs to maximize efficiency
  • 47. Viewing 3D NumPy arrays in Python
  • 48. Simple machine learning with scikit-learn • Sklearn contains machine learning implementations • Regressions (linear, logistic) • Classification (Random Forest, SVM) • Dimensionality reduction (PCA) • Clustering (k-means) • Etc.
  • 49.
  • 50. Linear Regression to predict Diabetes
  • 51. Linear Regression to predict Diabetes
  • 52. Outline • What is python? • Why use python? • How to use python? • IDE • Basic types • Containers • Basic control flow • Scientific Python • Questions?

Hinweis der Redaktion

  1. This will create a gui that lets your slice through your array. However, much easier to load into an external platform such as Slicer3D