Learning Python: From Zero to Hero
Tags: Python, Programming, Coding, Web Development, Software Development

This post was originally published at TK's Blog.

First of all, what is Python? According to its creator, Guido van Rossum, Python is a:

“high-level programming language, and its core design philosophy is all about code readability and a syntax which allows programmers to express concepts in a few lines of code.”

For me, the first reason to learn Python was that it is, in fact, a beautiful programming language. It was really natural to code in it and express my thoughts.

Another reason was that we can use coding in Python in multiple ways: data science, web development, and machine learning all shine here. Quora, Pinterest and Spotify all use Python for their backend web development. So let’s learn a bit about it.

The Basics

1. Variables

You can think about variables as words that store a value. Simple as that.

In Python, it is really easy to define a variable and set a value to it. Imagine you want to store number 1 in a variable called “one.” Let’s do it:

How simple was that? You just assigned the value 1 to the variable “one.”

And you can assign any other value to whatever other variables you want. As you see in the table above, the variable “two” stores the integer 2, and “some_number” stores 10,000.

Besides integers, we can also use booleans (True / False), strings, float, and so many other data types.

2. Control Flow: conditional statements

“If” uses an expression to evaluate whether a statement is True or False. If it is True, it executes what is inside the “if” statement. For example:

2 is greater than 1, so the “print” code is executed.

The “else” statement will be executed if the “if” expression is false.

1 is not greater than 2, so the code inside the “else” statement will be executed.

You can also use an “elif” statement:

3. Looping / Iterator

In Python, we can iterate in different forms. I’ll talk about two: while and for.

While Looping: while the statement is True, the code inside the block will be executed. So, this code will print the number from 1 to 10.

The while loop needs a “loop condition.” If it stays True, it continues iterating. In this example, when num is 11 the loop condition equals False .

Another basic bit of code to better understand it:

The loop condition is True so it keeps iterating — until we set it to False .

For Looping: you apply the variable “num” to the block, and the “for” statement will iterate it for you. This code will print the same as while code: from 1 to 10.

See? It is so simple. The range starts with 1 and goes until the 11 th element ( 10 is the 10 th element).

List: Collection | Array | Data Structure

Imagine you want to store the integer 1 in a variable. But maybe now you want to store 2. And 3, 4, 5 …

Do I have another way to store all the integers that I want, but not in millions of variables? You guessed it — there is indeed another way to store them.

List is a collection that can be used to store a list of values (like these integers that you want). So let’s use it:

It is really simple. We created an array and stored it on my_integer.

But maybe you are asking: “How can I get a value from this array?”

Great question. List has a concept called index. The first element gets the index 0 (zero). The second gets 1, and so on. You get the idea.

To make it clearer, we can represent the array and each element with its index. I can draw it:

Using the Python syntax, it’s also simple to understand:

Imagine that you don’t want to store integers. You just want to store strings, like a list of your relatives’ names. Mine would look something like this:

It works the same way as integers. Nice.

We just learned how Lists indices work. But I still need to show you how we can add an element to the List data structure (an item to a list).

The most common method to add a new value to a List is append . Let’s see how it works:

append is super simple. You just need to apply the element (eg. “The Effective Engineer”) as the append parameter.

Well, enough about Lists . Let’s talk about another data structure.

Dictionary: Key-Value Data Structure

Now we know that Lists are indexed with integer numbers. But what if we don’t want to use integer numbers as indices? Some data structures that we can use are numeric, string, or other types of indices.

Let’s learn about the Dictionary data structure. Dictionary is a collection of key-value pairs. Here’s what it looks like:

The key is the index pointing to the value. How do we access the Dictionary value? You guessed it — using the key. Let’s try it:

I created a Dictionary about me. My name, nickname, and nationality. Those attributes are the Dictionary keys.

As we learned how to access the List using index, we also use indices (keys in the Dictionary context) to access the value stored in the Dictionary .

In the example, I printed a phrase about me using all the values stored in the Dictionary . Pretty simple, right?

Another cool thing about Dictionary is that we can use anything as the value. In the Dictionary I created, I want to add the key “age” and my real integer age in it:

Here we have a key (age) value (24) pair using string as the key and integer as the value.

As we did with Lists , let’s learn how to add elements to a Dictionary . The key pointing to a value is a big part of what Dictionary is. This is also true when we are talking about adding elements to it:

We just need to assign a value to a Dictionary key. Nothing complicated here, right?

Iteration: Looping Through Data Structures

As we learned in the Python Basics, the List iteration is very simple. We Python developers commonly use For looping. Let’s do it:

So for each book in the bookshelf, we (can do everything with it) print it. Pretty simple and intuitive. That’s Python.

For a hash data structure, we can also use the for loop, but we apply the key :

This is an example how to use it. For each key in the dictionary , we print the key and its corresponding value .

Another way to do it is to use the iteritems method.

We did name the two parameters as key and value , but it is not necessary. We can name them anything. Let’s see it:

We can see we used attribute as a parameter for the Dictionary key , and it works properly. Great!

Classes & Objects

A little bit of theory:

Objects are a representation of real world objects like cars, dogs, or bikes. The objects share two main characteristics: data and behavior.

Cars have data, like number of wheels, number of doors, and seating capacity They also exhibit behavior: they can accelerate, stop, show how much fuel is left, and so many other things.

We identify data as attributes and behavior as methods in object-oriented programming. Again:

Data → Attributes and Behavior → Methods

And a Class is the blueprint from which individual objects are created. In the real world, we often find many objects with the same type. Like cars. All the same make and model (and all have an engine, wheels, doors, and so on). Each car was built from the same set of blueprints and has the same components.

Python Object-Oriented Programming mode: ON

Python, as an Object-Oriented programming language, has these concepts: class and object.

A class is a blueprint, a model for its objects.

So again, a class it is just a model, or a way to define attributes and behavior (as we talked about in the theory section). As an example, a vehicle class has its own attributes that define what objects are vehicles. The number of wheels, type of tank, seating capacity, and maximum velocity are all attributes of a vehicle.

With this in mind, let’s look at Python syntax for classes:

We define classes with a class statement — and that’s it. Easy, isn’t it?

Objects are instances of a class. We create an instance by naming the class.

Here car is an object (or instance) of the class Vehicle .

Remember that our vehicle class has four attributes: number of wheels, type of tank, seating capacity, and maximum velocity. We set all these attributes when creating a vehicle object. So here, we define our class to receive data when it initiates it:

We use the init method. We call it a constructor method. So when we create the vehicle object, we can define these attributes. Imagine that we love the Tesla Model S, and we want to create this kind of object. It has four wheels, runs on electric energy, has space for five seats, and the maximum velocity is 250km/hour (155 mph). Let’s create this object:

Four wheels + electric “tank type” + five seats + 250km/hour maximum speed.

All attributes are set. But how can we access these attributes’ values? We send a message to the object asking about them. We call it a method. It’s the object’s behavior. Let’s implement it:

This is an implementation of two methods: number_of_wheels and set_number_of_wheels. We call it getter & setter . Because the first gets the attribute value, and the second sets a new value for the attribute.

In Python, we can do that using @property ( decorators ) to define getters and setters . Let’s see it with code:

And we can use these methods as attributes:

This is slightly different than defining methods. The methods work as attributes. For example, when we set the new number of wheels, we don’t apply two as a parameter, but set the value 2 to number_of_wheels . This is one way to write pythonic getter and setter code.

But we can also use methods for other things, like the “make_noise” method. Let’s see it:

When we call this method, it just returns a string “VRRRRUUUUM.”