This document provides an introduction to the Python 3 programming language. It discusses Python's main features, including being general purpose, multiplatform, and supporting procedural, object-oriented, and functional programming. It also covers Python's core data types like lists, tuples, sets, and dictionaries. The document demonstrates some uniform operations that can be performed on these data types, such as checking for existence, counting elements, indexing, slicing, and deleting. It concludes by discussing input/output, exception handling, object-oriented programming in Python, and provides some references for further learning.
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 (any datatype)
> Mutable (append vs extend vs insert)
> Stack compatible (append, pop)
> Ordered
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 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)
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
> Use the 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
> 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
20. 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)