Learning Python - From Zero To Hero
Learning Python - From Zero To Hero
org
Images haven’t loaded yet. Please exit printing, wait for images to load, and try to
Learning Python: From Zero to Hero
print again.
TK Follow
Sep 30, 2017 · 11 min read
First of all, what is Python? According to its creator, Guido van Rossum,
Python is a:
For me, the rst 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.
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 1/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
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.
1 one = 1
How simple was that? You just assigned the value 1 to the variable
“one.”
1 two = 2
2 some_number = 10000
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, oat,
and so many other data types.
1 # booleans
2 true_boolean = True
3 false_boolean = False
4
5 # string
6 my_name = "Leandro Tk"
7
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 2/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
1 if True:
2 print("Hello Python If")
3
4 if 2 > 1:
1 if 1 > 2:
2 print("1 is greater than 2")
3 else:
4 print("1 is not greater than 2")
1 is not greater than 2, so the code inside the “else” statement will be
executed.
1 if 1 > 2:
2 print("1 is greater than 2")
3 elif 2 > 1:
4 print("1 is not greater than 2")
5 else:
3. Looping / Iterator
In Python, we can iterate in di erent 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.
1 num = 1
2
3 while num <= 10:
4 print(num)
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 3/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
1 loop_condition = True
2
3 while loop_condition:
4 print("Loop Condition keeps: %s" %(loop_condition))
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).
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:
1 my_integers = [1, 2, 3, 4, 5]
But maybe you are asking: “How can I get a value from this array?”
Great question. List has a concept called index. The rst element
gets the index 0 (zero). The second gets 1, and so on. You get the idea.
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 4/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
To make it clearer, we can represent the array and each element with its
index. I can draw it:
1 my_integers = [5, 7, 1, 3, 4]
2 print(my_integers[0]) # 5
3 print(my_integers[1]) # 7
4 print(my integers[4]) # 4
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:
1 relatives_names = [
2 "Toshiaki",
3 "Juliana",
4 "Yuji",
5 "Bruno",
6 "Kaio"
7 ]
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).
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 5/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
1 bookshelf = []
2 bookshelf.append("The Effective Engineer")
3 bookshelf.append("The 4 Hour Work Week")
4 print(bookshelf[0]) # The Effective Engineer
append is super simple. You just need to apply the element (eg. “The
E ective Engineer”) as the append parameter.
Well, enough about Lists . Let’s talk about another data structure.
1 dictionary_example = {
2 "key1": "value1",
3 "key2": "value2",
4 "key3": "value3"
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:
1 dictionary_tk = {
2 "name": "Leandro",
3 "nickname": "Tk",
4 "nationality": "Brazilian"
5 }
6
7 print("My name is %s" %(dictionary tk["name"])) # My name is
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 .
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 6/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
In the example, I printed a phrase about me using all the values stored
in the Dictionary . Pretty simple, right?
1 dictionary_tk = {
2 "name": "Leandro",
3 "nickname": "Tk",
4 "nationality": "Brazilian",
5 "age": 24
6 }
7
8 print("My name is %s" %(dictionary tk["name"])) # My name i
Here we have a key (age) value (24) pair using string as the key and
integer as the value.
1 dictionary_tk = {
2 "name": "Leandro",
3 "nickname": "Tk",
4 "nationality": "Brazilian"
5 }
6
7 dictionary_tk['age'] = 24
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 7/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
1 bookshelf = [
2 "The Effective Engineer",
3 "The 4 hours work week",
4 "Zero to One",
5 "Lean Startup",
6 "Hooked"
7 ]
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 .
We did name the two parameters as key and value , but it is not
necessary. We can name them anything. Let’s see it:
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 8/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
1 dictionary_tk = {
2 "name": "Leandro",
3 "nickname": "Tk",
4 "nationality": "Brazilian",
5 "age": 24
6 }
7
8 for attribute, value in dictionary_tk.items():
9 print("My %s is %s" %(attribute, value))
10
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.
And a Class is the blueprint from which individual objects are created.
In the real world, we often nd 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.
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 9/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
vehicle class has its own attributes that de ne what objects are
vehicles. The number of wheels, type of tank, seating capacity, and
maximum velocity are all attributes of a vehicle.
1 class Vehicle:
2 pass
We de ne classes with a class statement — and that’s it. Easy, isn’t it?
1 car = Vehicle()
2 print(car) # <__main__.Vehicle instance at 0x7fb1de6c2638>
1 class Vehicle:
2 def __init__(self, number_of_wheels, type_of_tank, seati
3 self.number_of_wheels = number_of_wheels
4 self.type_of_tank = type_of_tank
5 self.seating capacity = seating capacity
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 10/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
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:
1 class Vehicle:
2 def __init__(self, number_of_wheels, type_of_tank, seat
3 self.number_of_wheels = number_of_wheels
4 self.type_of_tank = type_of_tank
5 self.seating_capacity = seating_capacity
6 self.maximum_velocity = maximum_velocity
7
8 def number_of_wheels(self):
1 class Vehicle:
2 def __init__(self, number_of_wheels, type_of_tank, seat
3 self.number_of_wheels = number_of_wheels
4 self.type_of_tank = type_of_tank
5 self.seating_capacity = seating_capacity
6 self.maximum_velocity = maximum_velocity
7
8 @property
9 def number_of_wheels(self):
10 return self number of wheels
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 11/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
But we can also use methods for other things, like the “make_noise”
method. Let’s see it:
1 class Vehicle:
2 def __init__(self, number_of_wheels, type_of_tank, seati
3 self.number_of_wheels = number_of_wheels
4 self.type_of_tank = type_of_tank
5 self.seating_capacity = seating_capacity
6 self.maximum_velocity = maximum_velocity
7
. . .
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 12/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
1 class Person:
2 def __init__(self, first_name):
3 self.first_name = first_name
instance variable .
1 tk = Person('TK')
2 print(tk.first_name) # => TK
1 class Person:
2 first_name = 'TK'
1 tk = Person()
2 print(tk.first_name) # => TK
variable values.
Keeping the Person class in mind, we want to set another value to its
first_name variable:
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 13/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
1 tk = Person('TK')
2 tk.first_name = 'Kaio'
3 print(tk.first_name) # => Kaio
instance variable and it updated the value. Simple as that. Since it’s a
public variable, we can do that.
Here’s an example:
1 class Person:
2 def __init__(self, first_name, email):
3 self.first_name = first_name
4 self email email
variable :
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 14/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
1 tk = Person('TK', '[email protected]')
2 print(tk._email) # [email protected]
We can access and update it. Non-public variables are just a convention
and should be treated as a non-public part of the API.
1 class Person:
2 def __init__(self, first_name, email):
3 self.first_name = first_name
4 self._email = email
5
6 def update_email(self, new_email):
7 self._email = new_email
1 tk = Person('TK', '[email protected]')
2 print(tk.email()) # => [email protected]
3 tk._email = '[email protected]'
4 print(tk.email()) # => [email protected]
5 tk.update email('new [email protected]')
6. Success! We can update it inside our class with the helper method
Public Method
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 15/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
With public methods , we can also use them out of our class:
1 class Person:
2 def __init__(self, first_name, age):
3 self.first_name = first_name
4 self._age = age
5
1 tk = Person('TK', 25)
2 print(tk.show_age()) # => 25
Non-public Method
But with non-public methods we aren’t able to do it. Let’s implement
the same Person class, but now with a show_age non-public method
using an underscore ( _ ).
1 class Person:
2 def __init__(self, first_name, age):
3 self.first_name = first_name
4 self._age = age
5
And now, we’ll try to call this non-public method with our object:
1 tk = Person('TK', 25)
2 print(tk._show_age()) # => 25
We can access and update it. Non-public methods are just a convention
and should be treated as a non-public part of the API.
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 16/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
1 class Person:
2 def __init__(self, first_name, age):
3 self.first_name = first_name
4 self._age = age
5
6 def show_age(self):
7 return self._get_age()
8
9 def _get_age(self):
public method . The show_age can be used by our object (out of our
class) and the _get_age only used inside our class de nition (inside
show_age method). But again: as a matter of convention.
Encapsulation Summary
With encapsulation we can ensure that the internal representation of
the object is hidden from the outside.
1 class Car:
2 def __init__(self, number_of_wheels, seating_capacity, m
3 self.number_of_wheels = number_of_wheels
4 self.seating_capacity = seating_capacity
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 17/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
1 class ElectricCar(Car):
2 def __init__(self, number_of_wheels, seating_capacity, m
3 Car.__init__(self, number_of_wheels, seating_capacit
Beautiful.
That’s it!
We learned a lot of things about Python basics:
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 18/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
. . .
For more stories and posts about my journey learning & mastering
programming, follow my publication The Renaissance Developer.
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 19/20
12/23/2018 Learning Python: From Zero to Hero – freeCodeCamp.org
https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567 20/20