Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic,...
Transcript of Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic,...
![Page 1: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/1.jpg)
Introduction to PythonA readable, dynamic, pleasant,
flexible, fast and powerful language
(Slides created by Nowell Strite for the Vermont Code Camp 2010)
![Page 2: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/2.jpg)
Overview• Background
• Syntax
• Types / Operators / Control Flow
• Functions
• Classes
• Tools
• Let’s code together!
![Page 3: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/3.jpg)
![Page 4: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/4.jpg)
What is Python
• Multi-purpose (Web, GUI, Scripting, etc.)
• Object Oriented
• Interpreted
• Strongly and dynamically typed
• Focus on readability and productivity
![Page 5: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/5.jpg)
Features
• Everything is an Object
• Interactive Shell
• Cross Platform
• CPython, Jython, IronPython, PyPy
![Page 6: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/6.jpg)
Who Uses Python
• PBS
• NASA
• Library of Congress
• the ONION
• ...the list goes on...
![Page 7: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/7.jpg)
Releases
• Created in 1989 by Guido Van Rossum
• Python 1.0 released in 1994
• Python 2.0 released in 2000
• Python 3.0 released in 2008
• Python 2.7 is the recommended version
• 3.0 adoption will take a few years
![Page 8: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/8.jpg)
Python Interpreter
![Page 9: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/9.jpg)
.py .pyc
Python’s interpreter automatically deals with the whole process
Python Code
Python ”Byte-code”
Program Execution
![Page 10: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/10.jpg)
Syntax
![Page 11: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/11.jpg)
Hello World
hello_world.py
![Page 12: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/12.jpg)
Indentation
• Most languages don’t care about indentation
• I do
• As humans, we naturally tend to group similar things together
![Page 13: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/13.jpg)
Indentation
The else here actually belongs to the 2nd if statement
![Page 14: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/14.jpg)
Indentation
The else here actually belongs to the 2nd if statement
![Page 15: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/15.jpg)
Indentation
I knew a coder like this
![Page 16: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/16.jpg)
Indentation
You should always be explicit
![Page 17: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/17.jpg)
Indentation
Text
Python embraces indentation
![Page 18: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/18.jpg)
Comments
![Page 19: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/19.jpg)
Types
![Page 20: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/20.jpg)
Strings
![Page 21: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/21.jpg)
Numbers
![Page 22: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/22.jpg)
Null
![Page 23: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/23.jpg)
Lists
![Page 24: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/24.jpg)
Lists
![Page 25: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/25.jpg)
Dictionaries
![Page 26: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/26.jpg)
Dictionary Methods
![Page 27: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/27.jpg)
Booleans
![Page 28: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/28.jpg)
Operators
![Page 29: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/29.jpg)
Arithmetic
![Page 30: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/30.jpg)
String Manipulation
![Page 31: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/31.jpg)
Logical Comparison
![Page 32: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/32.jpg)
Identity Comparison
![Page 33: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/33.jpg)
Arithmetic Comparison
![Page 34: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/34.jpg)
Control Flow
![Page 35: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/35.jpg)
Conditionals
![Page 36: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/36.jpg)
For Loop
![Page 37: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/37.jpg)
Expanded For Loop
![Page 38: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/38.jpg)
While Loop
![Page 39: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/39.jpg)
List Comprehensions
• Very useful for replacing simple for-loops.
![Page 40: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/40.jpg)
Functions
![Page 41: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/41.jpg)
Basic Function
![Page 42: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/42.jpg)
Function Arguments
![Page 43: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/43.jpg)
Arbitrary Arguments
![Page 44: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/44.jpg)
Fibonacci
![Page 45: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/45.jpg)
Fibonacci Generator
![Page 46: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/46.jpg)
Classes
![Page 47: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/47.jpg)
Class Declaration
![Page 48: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/48.jpg)
Class Attributes
• Attributes assigned at class declaration should always be immutable
![Page 49: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/49.jpg)
Class Methods
![Page 50: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/50.jpg)
Class Instantiation & Attribute Access
![Page 51: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/51.jpg)
Class Inheritance
![Page 52: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/52.jpg)
Python’s Way
• No interfaces
• No real private attributes/functions
• Private attributes start (but do not end) with double underscores.
• Special class methods start and end with double underscores.
• __init__, __doc__, __cmp__, __str__
![Page 53: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/53.jpg)
Imports
• Allows code isolation and re-use
• Adds references to variables/classes/functions/etc. into current namespace
![Page 54: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/54.jpg)
Imports
![Page 55: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/55.jpg)
More Imports
![Page 56: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/56.jpg)
Error Handling
![Page 57: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/57.jpg)
Documentation
![Page 58: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/58.jpg)
Docstrings
![Page 59: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/59.jpg)
Tools
![Page 60: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/60.jpg)
Web Frameworks
• Django
• Flask
• Pylons
• TurboGears
• Zope
• Grok
![Page 61: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/61.jpg)
IDEs
• PyCharm
• Emacs
• Vim
• Komodo
• Eclipse (PyDev)
![Page 62: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/62.jpg)
Package Management
![Page 63: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/63.jpg)
Resources
• http://python.org/
• http://diveintopython.org/
• http://djangoproject.com/
![Page 64: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/64.jpg)
Example
![Page 65: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/65.jpg)
Going Further
• Decorators
• Context Managers
• Lambda functions
• Generators
• ...
![Page 66: Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for](https://reader030.fdocuments.in/reader030/viewer/2022040415/5f30a73dd8a8e83b7f56eda7/html5/thumbnails/66.jpg)
Questions?