Learning Python - From Zero To Hero
Learning Python - From Zero To Hero
Forum Donate
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 1/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
The Basics
1. Variables
You can think about variables as words that store a value. Simple as
that.
one = 1
How simple was that? You just assigned the value 1 to the variable
“one.”
two = 2
some_number = 10000
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 2/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
Forum Donate
And you can assign any other value to whatever other variables you
want.Support
As youour
seecharity
in the and
table above,
our theDonate
mission. variable “two” stores the
to freeCodeCamp.org.
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.
# booleans
true_boolean = True
false_boolean = False
# string
my_name = "Leandro Tk"
# float
book_price = 15.80
if True:
print("Hello Python If")
if 2 > 1:
print("2 is greater than 1")
Forum Donate
1 is not greater than 2, so the code inside the “else” statement will be
executed.
if 1 > 2:
print("1 is greater than 2")
elif 2 > 1:
print("1 is not greater than 2")
else:
print("1 is equal to 2")
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.
num = 1
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 4/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
print(num)
num += 1
Forum Donate
loop_condition = True
while loop_condition:
print("Loop Condition keeps: %s" %(loop_condition))
loop_condition = 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).
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:
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 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:
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 6/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
Forum Donate
my_integers = [5, 7, 1, 3, 4]
print(my_integers[0]) # 5
print(my_integers[1]) # 7
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:
relatives_names = [
"Toshiaki",
"Juliana",
"Yuji",
"Bruno",
"Kaio"
]
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 7/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
print(relatives_names[4]) # Kaio
Forum Donate
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).
bookshelf = []
bookshelf.append("The Effective Engineer")
bookshelf.append("The 4 Hour Work Week")
print(bookshelf[0]) # The Effective Engineer
print(bookshelf[1]) # The 4 Hour Work Week
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.
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 8/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
dictionary_example = {
"key1": "value1",
"key2": "value2",
"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:
dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian"
}
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?
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 9/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
Forum Donate
Another cool thing about Dictionary is that we can use anything as
Support
the value. In our
thecharity and ourI mission.
Dictionary created,Donate
I want to
tofreeCodeCamp.org.
add the key “age” and
my real integer age in it:
dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian",
"age": 24
}
Here we have a key (age) value (24) pair using string as the key and
integer as the value.
dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian"
}
dictionary_tk['age'] = 24
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 10/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
Forum Donate
We just needour
Support to charity
assign and
a value to a Dictionary
our mission. Donate to key. Nothing
freeCodeCamp.org.
complicated here, right?
bookshelf = [
"The Effective Engineer",
"The 4-hour Workweek",
"Zero to One",
"Lean Startup",
"Hooked"
]
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 :
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 11/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
Forum , weDonate
This is an example how to use it. For each key in the dictionary
print the key
Support and its and
our charity corresponding value .to freeCodeCamp.org.
our mission. Donate
We did name the two parameters as key and value , but it is not
necessary. We can name them anything. Let’s see it:
dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian",
"age": 24
}
# My name is Leandro
# My nickname is Tk
# My nationality is Brazilian
# My age is 24
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 12/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
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 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.
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 13/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
class Vehicle:
pass
We define classes with a class statement — and that’s it. Easy, isn’t it?
car = Vehicle()
print(car) # <__main__.Vehicle instance at 0x7fb1de6c2638>
class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity,
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 14/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity
Forum Donate
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:
class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity,
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity
def number_of_wheels(self):
return self.number_of_wheels
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 15/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity,
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity
@property
def number_of_wheels(self):
return self.__number_of_wheels
@number_of_wheels.setter
def number_of_wheels(self, number):
self.__number_of_wheels = number
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 16/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
But we can also use methods for other things, like the “make_noise”
method. Let’s see it:
class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity,
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity
def make_noise(self):
print('VRUUUUUUUM')
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 17/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
class Person:
def __init__(self, first_name):
self.first_name = first_name
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 18/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
tk = Person('TK')
Forum Donate
print(tk.first_name) # => TK
Support our charity and our mission. Donate to freeCodeCamp.org.
class Person:
first_name = 'TK'
tk = Person()
print(tk.first_name) # => TK
Keeping the Person class in mind, we want to set another value to its
first_name variable:
tk = Person('TK')
tk.first_name = 'Kaio'
print(tk.first_name) # => Kaio
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 19/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
Forum Donate
There we go. We just set another value ( kaio ) to the first_name
Support
instance our charity
variable and itand our mission.
updated Donate
the value. to freeCodeCamp.org.
Simple as that. Since it’s a
public variable, we can do that.
Here’s an example:
class Person:
def __init__(self, first_name, email):
self.first_name = first_name
self._email = email
Did you see the email variable? This is how we define a non-public
variable :
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 20/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
Forum Donate
class Person:
def __init__(self, first_name, email):
self.first_name = first_name
self._email = email
def email(self):
return self._email
tk = Person('TK', 'tk@mail.com')
print(tk.email()) # => tk@mail.com
# tk._email = 'new_tk@mail.com' -- treat as a non-public part of the clas
print(tk.email()) # => tk@mail.com
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 21/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
tk.update_email('new_tk@mail.com')
print(tk.email()) # => new_tk@mail.com
Forum Donate
Public Method
With public methods , we can also use them out of our class:
class Person:
def __init__(self, first_name, age):
self.first_name = first_name
self._age = age
def show_age(self):
return self._age
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 22/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
tk = Person('TK', 25)
Forum Donate
print(tk.show_age()) # => 25
Support our charity and our mission. Donate to freeCodeCamp.org.
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 ( _ ).
class Person:
def __init__(self, first_name, age):
self.first_name = first_name
self._age = age
def _show_age(self):
return self._age
And now, we’ll try to call this non-public method with our object:
tk = Person('TK', 25)
print(tk._show_age()) # => 25
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 23/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
Forum Donate
class Person:
Support our charity and our mission. Donate to freeCodeCamp.org.
def __init__(self, first_name, age):
self.first_name = first_name
self._age = age
def show_age(self):
return self._get_age()
def _get_age(self):
return self._age
tk = Person('TK', 25)
print(tk.show_age()) # => 25
Encapsulation Summary
With encapsulation we can ensure that the internal representation of
the object is hidden from the outside.
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 24/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
class Car:
def __init__(self, number_of_wheels, seating_capacity, maximum_veloci
self.number_of_wheels = number_of_wheels
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity
class ElectricCar(Car):
def __init__(self, number_of_wheels, seating_capacity, maximum_veloci
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 25/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
Beautiful.
That’s it!
We learned a lot of things about Python basics:
For more stories and posts about my journey learning & mastering
programming, follow my publication The Renaissance Developer.
TK
Read more posts.
If you read this far, thank the author to show them you care.
Say Thanks
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 27/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
Forum Donate
freeCodeCamp is a donor-supported
Support tax-exempt
our charity and 501(c)(3)
our mission. Donatecharity organization (United States
to freeCodeCamp.org.
Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers,
services, and staff.
Trending Guides
Mobile App
Our Charity
About Alumni Network Open Source Shop Support Sponsors Academic Honesty
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 28/29
2/27/24, 6:07 PM Learning Python: From Zero to Hero
https://www.freecodecamp.org/news/learning-python-from-zero-to-hero-120ea540b567/ 29/29