66
Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for the Vermont Code Camp 2010)

Introduction to Python - Universidade Federal de …Introduction to Python A readable, dynamic, pleasant, flexible, fast and powerful language (Slides created by Nowell Strite for

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

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

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

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
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

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

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

Who Uses Python

• Google

• 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

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

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

.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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Questions?