introduction to pythonmkang.faculty.unlv.edu/.../python_tutorial_slides.pdf · introduction to...

64
Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite Manager of Tech Solutions @ PBS [email protected]

Upload: others

Post on 02-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Introduction to

PythonA readable, dynamic, pleasant,

flexible, fast and powerful language

Nowell Strite

Manager of Tech Solutions @ PBS

[email protected]

Page 2: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Overview

• Background

• Syntax

• Types / Operators / Control Flow

• Functions

• Classes

• Tools

Page 3: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

What is Python

• Multi-purpose (Web, GUI, Scripting,

etc.)

• Object Oriented

• Interpreted

• Strongly typed and Dynamically typed

• Focus on readability and productivity

Page 4: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Features

• Batteries Included

• Everything is an Object

• Interactive Shell

• Strong Introspection

• Cross Platform

• CPython, Jython, IronPython, PyPy

Page 5: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Who Uses Python

• Google

• PBS

• NASA

• Library of Congress

• the ONION

• ...the list goes on...

Page 6: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

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 7: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Syntax

Page 8: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Hello World

hello_world.py

Page 9: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Indentation

• Most languages don’t care about

indentation

• Most humans do

• We tend to group similar things together

Page 10: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Indentation

The else here actually belongs to the 2nd if

statement

Page 11: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Indentation

The else here actually belongs to the 2nd if

statement

Page 12: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Indentation

I knew a coder like this

Page 13: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Indentation

You should always be explicit

Page 14: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Indentation

Text

Python embraces indentation

Page 15: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Comments

Page 16: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Types

Page 17: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Strings

Page 18: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Numbers

Page 19: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Null

Page 20: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Lists

Page 21: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Lists

Page 22: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Dictionaries

Page 23: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Dictionary Methods

Page 24: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Booleans

Page 25: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Operators

Page 26: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Arithmetic

Page 27: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

String Manipulation

Page 28: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Logical Comparison

Page 29: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Identity Comparison

Page 30: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Arithmetic

Comparison

Page 31: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Control Flow

Page 32: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Conditionals

Page 33: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

For Loop

Page 34: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Expanded For Loop

Page 35: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

While Loop

Page 36: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

List Comprehensions

• Useful for replacing simple for-loops.

Page 37: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Functions

Page 38: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Basic Function

Page 39: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Function Arguments

Page 40: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Arbitrary Arguments

Page 41: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Fibonacci

Page 42: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Fibonacci Generator

Page 43: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Classes

Page 44: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Class Declaration

Page 45: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Class Attributes

• Attributes assigned at class declaration

should always be immutable

Page 46: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Class Methods

Page 47: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Class Instantiation &

Attribute Access

Page 48: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Class Inheritance

Page 49: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

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 50: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Imports

• Allows code isolation and re-use

• Adds references to

variables/classes/functions/etc. into

current namespace

Page 51: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Imports

Page 52: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

More Imports

Page 53: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Error Handling

Page 54: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Documentation

Page 55: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Docstrings

Page 56: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Tools

Page 57: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Web Frameworks

• Django

• Flask

• Pylons

• TurboGears

• Zope

• Grok

Page 58: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

IDEs

• Emacs

• Vim

• Komodo

• PyCharm

• Eclipse (PyDev)

Page 59: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Package

Management

Page 60: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Resources

• http://python.org/

• http://diveintopython.org/

• http://djangoproject.com/

Page 61: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Example

Page 62: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Going Further

• Decorators

• Context Managers

• Lambda functions

• Generators

• ...

Page 63: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Questions?

Page 64: Introduction to Pythonmkang.faculty.unlv.edu/.../Python_Tutorial_Slides.pdf · Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language Nowell Strite

Thanks!