Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Latest commit

 

History

History
272 lines (193 loc) · 9.56 KB

README.md

File metadata and controls

272 lines (193 loc) · 9.56 KB

Practical Python

This is a hands-on workshop which aims to teach practical Python programming skills. The workshop does not intend to be a comprehensive guide or a Python reference. Rather, it aims to provide people who have little to no Python experience the essentials for making useful things with Python, with references to more elaborate material on specific topics from time to time.

By the end of this workshop you will have written two real-world projects in Python:

  1. An AWS Lambda function which uses the AWS Python SDK (boto)
  2. A command-line utility

TODO - explain what the projects do.

To give you the understanding and skills required for writing real-world projects, we will go through the following topics:

  • What is Python?
  • 2 or 3?
  • Setting up Your Work Environment
  • Using the Python Interpreter
  • Indentation!
  • Data Types
  • Flow Control
  • Data Structures
  • I/O
  • Functions
  • Importing Modules
  • Dependency Management
  • Error Handling
  • Logging
  • Testing

NOTE: This repository was created for an internal Python workshop at AllCloud.

What is Python?

monty python

Fun fact! Python is named after Monty Python's Flying Circus. The official Python docs are full of Monty Python references, jokes and easter eggs. By the way, if you don't know who Monty Python are, please stop reading this guide right away as you obviously have more important things to learn.

OK, back to reality. From the official Python documentation:

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.

Let's break this down.

Python is Interpreted

When you write code you are doing nothing more than editing simple text files. These text files cannot be executed by the CPU inside your computer. The CPU speaks a very specific, low-level language ("machine language") which is extremely difficult to work with for humans. Moreover, every CPU type speaks a different machine language, so the Intel CPU inside your laptop cannot run the same code as the ARM CPU inside your phone.

When you write code in C (or Go or Rust for that matter), you feed your code to a compiler, which is simply a program that can read text files and generate files with machine language, or binary files, from them.

When you write code in Python, on the other hand, your code is fed to an interpreter, which is a program that executes the code instead of generating a binary file from it.

NOTE: The above is not always true in the case of Python. I am presenting the most common case here on purpose to help convey the message.

Python is Object-Oriented

In object-oriented programming you describe a real-world problem in your code using virtual elements (called "objects") which represent real things, sometimes even physical things. For example, you could create two objects which represent cars, and tell one of them to start the engine:

my_car = Car(color='red')
my_other_car = Car(color='blue')
my_car.start()

So now my_car is running and my_other_car is not.

Python is High-Level

Generally speaking, the easier it is for a human to work with a programming language, the harder it is for a computer to run the code. Nowadays, an hour of a programmer's work is usually way more expensive than consuming a few more CPU cycles. That's why we don't do web development in C (although it is perfectly possible to do so!).

Python puts the focus on developer productivity rather than on performance. This does not mean you can't write high-performance code in Python, but - again, generally speaking - Python apps tend to be much slower than Java apps, Go apps or C apps.

However, the upside is that Python is really easy to work with. Its syntax is minimalist (public static void main(String[] args) {System.out.println("Java is minimalist, too!");}), it doesn't rely on arcane characters to do its job, it doesn't use parentheses and brackets when they aren't needed ((((((((hi!)))))))) and you can feel free to forget as many semicolons as you like!

Python is Dynamic

The main thing you need to know for now is that we don't need to tell Python what type of a variable we are creating, it figures it out on its own:

my_string = "Ni!"
my_number = 3

What Python is Not?

Python is not a program. True, most Linux distributions come with an executable called python. This is actually the most common Python interpreter called CPython (because it is a program that is written in C). Python is a language, and CPython is just one Python implementation. There are many others.

And now for something completely different.

2 or 3?

Setting up Your Work Environment

In order to run Python code you will need a Python interpreter. You may already have a Python interpreter installed on your machine. Any Python 3 release would do for this workshop.

Type python on your shell. If you have Python installed, you should see something like the following:

Python 3.6.5 (default, Apr 25 2018, 14:23:58)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Notice the version (Python 3.6.5 in the output above). If you get Python 2.x.x, try running python3. If you still don't get a Python 3 interpreter, follow the instructions below.

Mac Users

TL;DR: brew install python.

For detailed info, follow these great instructions from Kenneth Reitz.

NOTE: If you are running OSX High Sierra and are having trouble getting Python 3 to work for you by default, take a look here.

Windows Users

You can get a Mac here. Or follow the instructions here.

Linux Users

You probably feel you don't need assistance, but in case that's not true - there you go.

Using the Python Interpreter

One of the nicest things about interpreted programming languages is that you can often work with them interactively. Let's try the following:

Open your terminal, type python and hit Enter. Assuming you've set up your work environment, you should get a prompt similar to the following:

Python 3.6.5 (default, Apr 25 2018, 14:23:58)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now type print('hi!') and hit Enter.

>>> print('hi!')
hi!
>>>

What do you know - it worked! You can also calculate stuff:

>>> 3 + 5
8
>>>

You can assign values to variables and then use them:

>>> name = 'Bob'
>>> print("Hello", name)
Hello Bob
>>>

As a matter of fact, you can type any Python expression into this shell and the interpreter will execute it.

Now, while having an interactive shell is great for quick, small-scale ad-hoc tests, it is not very comfortable for writing a lot of code. So, let's write a lot of code! Or, some code, at least.

Indentation!

If you are completely new to Python, this section might cause you to raise an eyebrow or two. We've even heard about some extreme cases in the history in which people who had been exposed to the following information took a vow on the spot to never write a line of Python. Many of them are now Java developers.

So, let's get on with it.

In Python, the following two code snippets are not the same:

for i in range(5):
    print(i)
print('Done!')

for i in range(5):
    print(i)
    print('Done!')

When running the first snippet, the following output is given:

0
1
2
3
4
Done!

When running the second snippet, the output is as follows:

0
Done!
1
Done!
2
Done!
3
Done!
4
Done!

As you might have noticed, the only difference between the two pieces of code is that the 3rd line in the 2nd piece is indented one step to the right. While in most programming languages this makes no difference and the program's behavior would remain identical, that's not the case with Python.

Python cares about indentation.

This means that the way you indent your code directly affects the behavior of your program, not only the visual grouping of the code. In the case of this example, the indentation determines whether the last print statement is a part of the for loop that precedes it.

NOTE: While Python is the only mainstream programming language (at the time of writing) which is indentation-sensitive, it's definitely not the only one overall.

Indentation Sensitivity is Good for You

Some people like Python's approach towards indentation, and some people simply hate it.

Python Indentation Rules

Following are the important indentation rules you should follow when writing Python code:

  • You can use either tabs or spaces, but be consistent.