Python by Example

From Beginner to Professional
4.0 (5 ratings) Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
108 students enrolled
$19
$25
24% off
Take This Course
  • Lectures 35
  • Length 6.5 hours
  • Skill Level Beginner Level
  • Languages English
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works

Discover

Find online courses made by experts from around the world.

Learn

Take your courses with you and learn anywhere, anytime.

Master

Learn and practice real-world skills and achieve your goals.

About This Course

Published 3/2016 English

Course Description

Python By Example will help you learn the Python programming language from the ground up. We will be using Python 3 in this course. It is currently split into four parts. In part one, you will learn how the basic building blocks of Python, such as strings, lists, dictionaries and tuples. You will also learn how to handle exceptions, create list comprehensions, make functions, classes, etc. 

In part two, we take an abbreviated tour of the Python Standard Library. This set of screencasts cover handpicked modules from the library that I have found useful in my every day programming life.

For part three, we move into intermediate level material such as lambdas, decorators, debugging, profiling and testing.

Finally in part four we jump into 3rd party modules and packages. In this section, you will learn how to install these modules from the Python Package Index (PyPI) as well as how to use some of these modules. For example, you will learn how to use SQLAlchemy, virtualenv and pylint among others.

I hope this has piqued your interest. I am looking forward to helping you learn Python too!

What are the requirements?

  • Download Python 3 if your OS doesn't already have it pre-installed

What am I going to get from this course?

  • Know the basics of Python
  • Take an abbreviated tour of the standard library
  • Learn intermediate topics (lambda, decorators, code profiling, testing)
  • Install 3rd Party modules / packages
  • Tutorials using SQLAlchemy, virtualenv, lxml, pylint, and more

What is the target audience?

  • You will get the most out of this course if you have some programming experience
  • This course is aimed at beginners or intermediate users of Python

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.

Curriculum

Section 1: Learning the Basics
08:29

Learn how to use Python's included code editor, IDLE. You will learn the following:

  • How to use IDLE's interpreter interface
  • What the Path Browser is
  • What the Class Browser is used for
  • How to configure IDLE
  • What Code Context is for
  • and more!
14:29

There are several data types in Python. The main data types that you'll probably see the most are string, integer, float, list, dict and tuple. In this episode, we'll cover the string data type. You'll be surprised how many things you can do with strings in Python right out of the box. There's also a string module that you can import to access even more functionality, but we won't be looking at that in this lecture. Instead, we will be covering the following topics:

  • How to create strings
  • String concatenation
  • String methods
  • String slicing
  • String substitution
12:29

Python has several other important data types that you'll probably use every day. They are called lists, tuples and dictionaries. This lecture's aim is to get you acquainted with each of these data types. They are not particularly complicated, so I expect that you will find learning how to use them very straight forward. Once you have mastered these three data types plus the string data type from the previous lecture, you will be quite a ways along in your education of Python. You'll be using these four building blocks in 99% of all the applications you will write.

18:16

Every computer language I have ever used has had at least one conditional statement. Most of the time that statement is the if/elif/else structure. This is what Python has. Other languages also include the case/switch statement which I personally enjoy, however Python does not include it. You can make your own if you really want to, but this lecture series is focused on learning Python fundamentals, so we're going to be only focusing on what's included with Python in this chapter.

The conditional statement checks to see if a statement is True or False. That's really all it does. However we will also be looking at the following Boolean operations: and, or, and not. These operations can change the behavior of the conditional in simple and complex ways, depending on your project.

12:38

Every programming language I have tried has some kind of looping construct. Most have more than one. The Python world has two types of loops:

  • the for loop and
  • the while loop

You will find that the for loop is by far the most popular of the two. Loops are used when you want to do something many times. Usually you will find that you need to do some operation or a set of operations on a piece of data over and over. This is where loops come in. They make it really easy to apply this sort of logic to your data.

11:06

The Python language has a couple of methods for creating lists and dictionaries that are known as comprehensions. There is also a third type of comprehension for creating a Python set. In this lecture we will learn how to use each type of comprehension. You will find that the comprehension constructs build on the knowledge you have acquired from the previous lectures as they contain loops and conditionals themselves.

09:09

What do you do when something bad happens in your program? Let's say you try to open a file, but you typed in the wrong path or you ask the user for information and they type in some garbage. You don't want your program to crash, so you implement exception handling. In Python, the construct is usually wrapped in what is know as a try/except. We will be looking at the following topics in this lecture:

  • Common exception types
  • Handling exceptions with try/except
  • Learn how try/except/finally works
  • Discover how the else statement works in conjunction with the try/except
16:50

This lecture introduces the topic of reading and writing data to files on your hard drive.

06:47

Python comes with lots of pre-made code baked in. These pieces of code are known as modules and packages. A module is a single importable Python file whereas a package is made up of two or more modules. A package can be imported the same way a module is. Whenever you save a Python script of your own, you have created a module. It may not be a very useful module, but that's what it is. In this lecture, we will learn how to import modules using several different methods.

13:23

A function is a structure that you define. You get to decide if they have arguments or not. You can add keyword arguments and default arguments too. A function is a block of code that starts with the def keyword, a name for the function and a colon. It's really easier to just show you an example, so I recommend watching the lecture!

13:21

Everything in Python is an object. That's a very vague statement unless you've taken a computer programming class or two. What this means is that every thing in Python has methods and values. The reason is that everything is based on a class. A class is the blueprint of an object.

05:30

Whether you're new to Python, been using it for a few years or you're an expert, knowing how to use Python's introspection capabilities can help your understanding of your code and that new package you just downloaded with the crappy documentation. Introspection is a fancy word that means to observe oneself and ponder one's thoughts, senses, and desires. In Python world, introspection is actually kind of similar. Introspection in this case is to use Python to figure out Python.

Section 2: Part II - Learning from the Library
15:48
The csv module gives the Python programmer the ability to parse CSV (Comma Separated Values) files. A CSV file is a human readable text file where each line has a number of fields, separated by commas or some other delimiter. You can think of each line as a row and each field as a column. The CSV format has no standard, but they are similar enough that the csv module will be able to read the vast majority of CSV files. You can also write CSV files using the csv module.
11:59

Configuration files are used by both users and programmers. They are usually used for storing your application's settings or even your operating system's settings. Python's core library includes a module called configparser that you can use for creating and interacting with configuration files.

16:40

Python provides a very powerful logging library in its standard library. A lot of programmers use print
statements for debugging (myself included), but you can also use logging to do this. It's actually cleaner to
use logging as you won't have to go through all your code to remove the print statements.

In this lecture we'll cover the following topics:

• Creating a simple logger
• How to log from multiple modules
• Log formatting
• Log configuration

16:37

The os module has many uses. We won't be covering everything that it can do. Instead, we will get an
overview of its uses and we'll also take a look at one of its sub-modules, known as os.path. Specifically, we
will be covering the following:

• os.name
• os.environ
• os.chdir()
• os.getcwd()
• os.getenv()
• os.putenv()
• os.mkdir()
• os.makedirs()
• os.remove()
• os.rename()
• os.rmdir()
• os.startfile()
• os.walk()
• os.path

That looks like a lot to cover, but there is at least ten times as many other actions that the os module can do.

19:47

Python provides a couple of really nice modules that we can use to craft emails with. They are the email and smtplib modules. Instead of going over various methods in these two modules, we'll spend some time learning how to actually use these modules. Specifically, we'll be covering the following:

• The basics of emailing
• How to send to multiple addresses at once
• How to send email using the TO, CC and BCC lines
• How to add an attachment and a body using the email module

14:50

SQLite is a self-contained, server-less, config-free transactional SQL database engine. Python gained the sqlite3 module all the way back in version 2.5 which means that you can create SQLite database with anycurrent Python without downloading any additional dependencies. Mozilla uses SQLite databases for its popular Firefox browser to store bookmarks and other various pieces of information. In this lecture you will learn the following:

• How to create a SQLite database
• How to insert data into a table
• How to edit the data
• How to delete the data
• Basic SQL queries

In other words, rather than covering bits and pieces of the sqlite3 module, we'll go through how to actually use it.

04:17

The subprocess module gives the developer the ability to start processes or programs from Python. In other words, you can start applications and pass arguments to them using the subprocess module. The subprocess module was added way back in Python 2.4 to replace the os modules set of os.popen, os.spawn and os.system calls as well as replace popen2 and the old commands module. We will be looking at the following aspects of the subprocess module:

• the call function
• the Popen class
• how to communicate with a spawned process

08:10

The sys module provides system specific parameters and functions. We will be narrowing our study down to the following:

• sys.argv
• sys.executable
• sys.exit
• sys.modules
• sys.path
• sys.platform
• sys.stdin/stdout/stderr

09:32

Python has a number of different concurrency constructs such as threading, queues and multiprocessing. The threading module used to be the primary way of accomplishing concurrency. A few years ago, the multiprocessing module was added to the Python suite of standard libraries. This lecture will be focused on how to use threads and queues.

14:25

Python gives the developer several tools for working with dates and time. In this lecture, we will be looking at the datetime and time modules. We will study how they work and some common uses for them.

08:02

Python has built-in XML parsing capabilities that you can access via its xml module. In this article, we will be focusing on two of the xml module's sub-modules:

• minidom
• ElementTree

We'll start with minidom simply because this used to be the de-facto method of XML parsing. Then we will
look at how to use ElementTree instead.

Section 3: Part III - Intermediate Odds and Ends
07:50

Python comes with its own debugger module that is named pdb. This module provides an interactive source code debugger for your Python programs. You can set breakpoints, step through your code, inspect stack frames and more. We will look at the following aspects of the module:

• How to start the debugger
• Stepping through your code
• Setting breakpoints

10:10

Python decorators are really cool, but they can be a little hard to understand at first. A decorator in Python is a function that accepts another function as an argument. The decorator will usually modify or enhance the function it accepted and return the modified function. This means that when you call a decorated function, you will get a function that may be a little different but that may have additional features compared with the base definition. But let's back up a bit. We should probably review the basic building block of a decorator, namely, the function.

14:17

The Python lambda statement is an anonymous or unbound function and a pretty limited function at that. Let's take a look at a few typical examples and see if we can find a use case for it. The typical examples that one normally sees for teaching the lambda are some sort of boring doubling function. Just to be contrary, our simple example will show how to find the square root. First we'll show a normal function and then the lambda equivalent.

09:55

Code profiling is an attempt to find bottlenecks in your code. Profiling is supposed to find what parts of your code take the longest. Once you know that, then you can look at those pieces of your code and try to find ways to optimize it. Python comes with three profilers built in: cProfile, profile and hotshot.

According to the Python documentation, hotshot "no longer maintained and may be dropped in a future version of Python". The profile module is a pure Python module, but adds a lot of overhead to profiled programs. Thus we will be focusing on cProfile, which has an interface that mimics the profile module.

13:31

Python includes a couple of built-in modules for testing your code. They two methods are called doctest and unittest. We will start with doctest and then use some Test Driven Development techniques to learn how to use unittest.

Section 4: Part IV - Tips, Tricks and Tutorials
07:52

When you're first starting out as a Python programmer, you don't think about how you might need to install an external package or module. But when that need appears, you'll want to know how to in a hurry! Python packages can be found all over the internet. Most of the popular ones can be found on the Python Package Index (PyPI). You will also find a lot of Python packages on github, bitbucket, and Google code. In this lecture, we will be covering the following methods of installing Python packages:

• Install from source
• easy_install
• pip
• Other ways to install packages

08:30

Python comes with a handy module called ConfigParser. It's good for creating and reading configuration files (aka INI files). However, Michael Foord (author of IronPython in Action) and Nicola Larosa decided to write their own configuration module called ConfigObj. In many ways, it is an improvement over the standard library's module. For example, it will return a dictionary-like object when it reads a config file. ConfigObj can also understand some Python types. Another neat feature is that you can create a configuration spec that ConfigObj will use to validate the config file.

10:50

In Part I, we looked at some of Python's built-in XML parsers. In this lecture, we will look at the fun third-party package, lxml from codespeak. It uses the ElementTree API, among other things. The lxml package has XPath and XSLT support, includes an API for SAX and a C-level API for compatibility with C/Pyrex modules.

Here is what we will cover:

• How to Parse XML with lxml
• A Refactoring example
• How to Parse XML with lxml.objectify
• How to Create XML with lxml.objectify

10:12

Python code analysis can be a heavy subject, but it can be very helpful in making your programs better. There are several Python code analyzers that you can use to check your code and see if they conform to standards. pylint is probably the most popular. It's very configurable, customizable and pluggable too. It also checks your code to see if it conforms to PEP8, the official style guide of Python Core and it looks for programming errors too.

Note that pylint checks your code against most, but not all of PEP8's standards. We will spend a little time learning about another code analysis package that is called pyflakes.

04:21

The requests package is a more Pythonic replacement for Python's own urllib. You will find that requests package's API is quite a bit simpler to work with. You can install the requests library by using pip or easy_install or from source.

15:00

SQLAlchemy is usually referred to as an Object Relational Mapper (ORM), although it is much more full featured than any of the other Python ORMs that I've used, such as SqlObject or the one that's built into Django. SQLAlchemy was created by a fellow named Michael Bayer. Since I'm a music nut, we'll be creating a simple database to store album information. A database isn't a database without some relationships, so we'll create two tables and connect them. Here are a few other things we'll be learning:

• Adding data to each table
• Modifying data
• Deleting data
• Basic queries

08:30

Virtual environments can be really handy for testing software. That's true in programming circles too. Ian Bicking created the virtualenv project, which is a tool for creating isolated Python environments. You can use these environments to test out new versions of your software, new versions of packages you depend on or just as a sandbox for trying out some new package in general. You can also use virtualenv as a workspace when you can't copy files into site-packages because it's on a shared host. When you create a virtual environment with virtualenv, it creates a folder and copies Python into it along with a site-packages folder and a couple others. It also installs pip. Once your virtual environment is active, it's just like using your normal Python. And when you're done, you can just delete the folder to cleanup. No muss, no fuss. Alternatively, you can keep on using it for development.

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

Michael Driscoll, Python Professional

My name is Mike Driscoll. I am a computer programmer by trade and use Python almost exclusively to make my living. I have been a technical reviewer for Packt Publishing since late 2009. I blog about Python and ended up writing an introductory book about the language called Python 101. It is available on Amazon, Lulu and many other locations. I am currently working on its sequel, Python 201: Intermediate Python which will be available September 2016.

I also co-authored The Essential Core Python Cheat Sheet for DZone.

Ready to start learning?
Take This Course