INTRODUCTION TO PYTHON 3
Sakis Kasampalis
Full-Stack Developer
Pycom
@SKasampalis
sakisk.me
My Python background (1/2)
> R&D Software Engineer (2011)
> python-patterns (2012)
> Technical Reviewer (2013)
My Python background (2/2)
> Technical Reviewer (2014)
> Author (2015)
> Full-Stack developer (2016)
What is Python?
> General purpose (not just scripting!)
> Multiplatform (GNU/Linux, Windows,
macOS, microcontrollers, …)
> Multiparadigm (procedural, OOP,
functional)
> Focuses on readability and getting
things done
> Batteries included (SQLite, JSON, e-
mail, …)
Let’s start exploring
> Go to python.org (REPL introduction)
> Dynamically (but strongly) typed
> Just 3 data types (int, float, str)
> Install PyCharm (jetbrains.com)
> Play with user input
bit.ly/python-tmc
Lists
> Generic (any datatype)
> Mutable (append vs extend vs insert)
> Stack compatible (append, pop)
> Ordered
Tuples
> Generic (any datatype)
> Immutable
> Faster (and safer) than lists
> Ordered
Sets
> Generic (any datatype)
> Mutable
> Unordered
> Unique items
> All common set operations supported
(union, intersection, difference, …)
Dictionaries
> Generic (any datatype)
> Mutable
> Unordered
> Key-Value pairs
> Fast lookup
Uniform operations - Existence
> Check if an element exists
> Use the in keyword
> Works on all structures (also strings)
Uniform operations - Counting
> Get the number of elements
> Use the len function
> Works on all structures (also strings)
Semi-uniform operations - Indexing
> Access individual elements
> Works on all ordered structures (also
strings)
> For dicts use the keys function
> For sets use the in keyword
Semi-uniform operations - Slicing
> Access multiple sequential elements
> Works on all ordered structures (also
strings)
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
> Syntax [start:end:step]
> Default: start = 0, step = 1
Semi-uniform operations - Deleting
> Remove a specific element
> Use the del operator
> Works on all mutable structures but sets
(beware: strings are immutable)
> For sets use the remove function
> For strings use slicing
A last uniform operation - Comprehensions
> Write more compact code
> Apply an expression on all elements
> Works on all structures (also strings)
> [expr for … for … if …]
> Readability matters (use responsibly)
I/O and files
> Introduction to str.format
> Be explicit about the mode and
encoding
> Use the with statement when possible
Exception handling
> Use the try-except-else idiom
> Exception hierarchy list
> Explore try-except
Object Oriented Programming (OOP)
> self is a convention (but please use it)
> No encapsulation (we are all adults)
> A different OOP notion
> Polymorphism using duck typing
Your turn
> Download and print the number of
items
> Install beautiful soup version 4 (bs4)
> Help: beatiful soup documentation
> Find some content that you want to download
(e.g. xkcd “Python”, flickr “sunset”)
> My example: bit.ly/flickrfetch
References
> docs.python.org
> Mark Pilgrim, Dive Into Python 3
(diveintopython3.net)
> pythonprogramminglanguage.com
> Steven F. Lott, Mastering Object-
Oriented Python
> Al Sweigart, Automate the boring stuff
with Python (automatetheboringstuff.com)

Introduction to Python 3

  • 1.
    INTRODUCTION TO PYTHON3 Sakis Kasampalis Full-Stack Developer Pycom @SKasampalis sakisk.me
  • 2.
    My Python background(1/2) > R&D Software Engineer (2011) > python-patterns (2012) > Technical Reviewer (2013)
  • 3.
    My Python background(2/2) > Technical Reviewer (2014) > Author (2015) > Full-Stack developer (2016)
  • 4.
    What is Python? >General purpose (not just scripting!) > Multiplatform (GNU/Linux, Windows, macOS, microcontrollers, …) > Multiparadigm (procedural, OOP, functional) > Focuses on readability and getting things done > Batteries included (SQLite, JSON, e- mail, …)
  • 5.
    Let’s start exploring >Go to python.org (REPL introduction) > Dynamically (but strongly) typed > Just 3 data types (int, float, str) > Install PyCharm (jetbrains.com) > Play with user input bit.ly/python-tmc
  • 6.
    Lists > Generic (anydatatype) > Mutable (append vs extend vs insert) > Stack compatible (append, pop) > Ordered
  • 7.
    Tuples > Generic (anydatatype) > Immutable > Faster (and safer) than lists > Ordered
  • 8.
    Sets > Generic (anydatatype) > Mutable > Unordered > Unique items > All common set operations supported (union, intersection, difference, …)
  • 9.
    Dictionaries > Generic (anydatatype) > Mutable > Unordered > Key-Value pairs > Fast lookup
  • 10.
    Uniform operations -Existence > Check if an element exists > Use the in keyword > Works on all structures (also strings)
  • 11.
    Uniform operations -Counting > Get the number of elements > Use the len function > Works on all structures (also strings)
  • 12.
    Semi-uniform operations -Indexing > Access individual elements > Works on all ordered structures (also strings) > For dicts use the keys function > For sets use the in keyword
  • 13.
    Semi-uniform operations -Slicing > Access multiple sequential elements > Works on all ordered structures (also strings) +---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1 > Syntax [start:end:step] > Default: start = 0, step = 1
  • 14.
    Semi-uniform operations -Deleting > Remove a specific element > Use the del operator > Works on all mutable structures but sets (beware: strings are immutable) > For sets use the remove function > For strings use slicing
  • 15.
    A last uniformoperation - Comprehensions > Write more compact code > Apply an expression on all elements > Works on all structures (also strings) > [expr for … for … if …] > Readability matters (use responsibly)
  • 16.
    I/O and files >Introduction to str.format > Be explicit about the mode and encoding > Use the with statement when possible
  • 17.
    Exception handling > Usethe try-except-else idiom > Exception hierarchy list > Explore try-except
  • 18.
    Object Oriented Programming(OOP) > self is a convention (but please use it) > No encapsulation (we are all adults) > A different OOP notion > Polymorphism using duck typing
  • 19.
    Your turn > Downloadand print the number of items > Install beautiful soup version 4 (bs4) > Help: beatiful soup documentation > Find some content that you want to download (e.g. xkcd “Python”, flickr “sunset”) > My example: bit.ly/flickrfetch
  • 20.
    References > docs.python.org > MarkPilgrim, Dive Into Python 3 (diveintopython3.net) > pythonprogramminglanguage.com > Steven F. Lott, Mastering Object- Oriented Python > Al Sweigart, Automate the boring stuff with Python (automatetheboringstuff.com)