This lesson aims to familiarize you with basic Python syntax and how to do the essential things like printing something on the terminal, manipulating strings, and understanding how to use variables, functions, logical operations, and conditionals. This lesson should be the first to read in your python journey.
Please know that there are other lessons explaining each concept in detail; this is just a basic overall to help you get the big picture.
Python is the fastest-growing backend language in the world. It's the most versatile and easy-to-code language with one of the most robust communities.
When you compare it to other backend languages, Python is leading in almost every functionality it offers: Data Science, AI, API developments, Web Developments, Networks, Automation, etc.
Here are some of the reasons Python has come to this point:
|Python was meant to be simple and easy. Here is the Python manifest:|
Note: No more semicolons or curly brackets anymore, or declaring variables, or the confusing "this" functionality.
|Python is faster than Java, PHP, Ruby and 90% of the other backend languages. Only low-level languages like C++ (hard to use) or very specialized like Node.js can beat it.|
Python scalability has been proven over and over with applications like Google Search Engine, Youtube, Google Apps, etc.
|Python is Google's official language. It's also one of the oldest languages with huge communities around each of its libraries\tools. MIT uses it to teach code. NASA to build rockets. Quora, Facebook, Yahoo, Amazon, etc. Almost every big company in the world has to use it!||Most of the Python libraries are the best at what they do: MathLab (for data processing), Pandas (big data), Web.py (webserver), Django (web framework), PyBrain (AI), TensorFlow (Machine Learning), etc. We could be here all day! The most amazing thing is that these libraries are only one "pip install" away (just like when using NPM with JS).|
An essential thing you can do in any programming language is print some messages on the terminal. In Python you can use the
Like most programming languages (or math), Python lets you store data in variables for later use; variables are a powerful tool because they allow your code to be reusable for any variable value in the future, for example: To calculate a square area you have to do:
Your code will calculate the
area no matter the value of
Variables make your code reusable and dynamic.
You can also print from a stored variable value, or even concatenate it to another string using the plus
If you don't remember or don't know, data types are the types of information you can store in a variable: Numbers, text, lists, etc. Every programming language has its types of data. In Python, these are the types (there are more types that we will review later on):
|Text Type||str||words, paragraphs, etc.|
|Numeric Types||int, float, complex||math operations, etc.|
|Sequence Types||list, tuple, range||multiple values at once one after the other|
|Mapping Type||dict||multiple values without sequence|
|Boolean Type||bool||only |
It's important to highlight that the string
"1" is not the same as the number
1, so if you want to sum
"1" + 1, the result will not be 2. Instead, the computer will raise an error.
Because types matter, you have to be consistent with the types, if you want to sum the string "5" with the string "2" you have to convert them to integers first like this:
On the other hand, if you sum the string
"5" with the string
"2", the result will be
"52" because that is the default operation for summing strings: Concatenation.
We will talk more about concatenation in another lesson
All the major programming languages have ways of storing lists of values together; these are called
A Python list is an ordered succession of values, for example:
It's important to remember that the order matters. Each of the values in a list has a position that will always be the same. You can retrieve any value inside using the position they take in the list, for example:
Important: Lists start at position 0
Lists are broadly used in every programming language, especially in Python, because Machine Learning uses enormous lists of information.
Note: we will discuss lists in more detail soon.
After knowing the basics of Python you can start building algorithms: An Algorithm is a succession of lines of code with a common purpose. The lines of code go from top to bottom (from line 1 to line N). For example, the following is a very simple algorithm that prints the full name of the given first and last name.
The first line is
name = "Taylor" and its executed first; the last line with the
name because it was completed first (earlier line)
Conditions are fantastic because they let you skip or conditionally execute parts of your algorithms, making them very flexible and intelligent. For example, we could conditionally print "Miss" or "Mrs" depending on the relationship status:
if...else... statement reads like English. Also, to make comparisons, you have to use two equals
== instead of one, and that is called a Logical Expression
Computers think of everything in black or white. Everything is either True or False. All the decisions in a computer are reduced into a simple Boolean. You can prepare a computer to solve particular problems, if you write code that asks the proper questions required to solve that problem.
For example, if I want a computer to give candy only to kids older than 13 years of age, I can instruct the computer to ask:
Is this kid's age greater than 13? Yes or no?
In Python, you can instruct the computer to do the following logical operations:
|Equal to||==||Is 5 == 5? True!|
Is 5 == 4? False!
Is 5 == '5'? True!
|Not Equal to||!=||Is 5 != 5? False!|
Is 5 != '5'? False!
Is 1 != 'Hello' True!
|Greater than||>||Is 5 > 5? False!|
Is 6 > 3? True!
|Less than||<||Is 6 < 12? True|
|Greater or equal||>=||Is 6 <= 6? True|
Is 3 <= 6? True
|Less or equal||<=||You get the idea 🙂|
To create really useful operations, you can combine several operations in the same question using AND, OR and NOT.
You can group the logical operations in parentheses, and also use nested parentheses for several operations at the same time.
|AND||and||With AND, both sides HAVE TO BE TRUE in order for everything to become true.|
Is (5 == 5 and 3 > 1) ? True!
Is ('Ramon' == 'Pedro' and 2 == 2) ? False!
|OR||||||Is ('Oscar' or 'Maria' or 2 != 2)? True!|
Is (5 == '5' and 'Ramon' != 'Pedro') or (2 == 2)? True!
|NOT||!||NOT will be the exact opposite of the result of the logical operator:|
Is !(5 > 5)? True!
Is !(True)? False!
Okay, now is when everything starts getting fun! To control your applications flow, you'll have several options, and you are going to use each of them every single day. So, you must become comfortable using them.
The first tool you have is the if…else conditional. You can tell the computer to skip any part of your code depending on the current value of your variables. The if statement allows you to execute a piece of code if certain conditions are met (or are true). The "else" statement will execute an alternate piece of code in case the condition is false.
The last trick we will see today is loops. Loops are amazing because they let you repeat the execution of one or more lines of code without having to write the same line several times:
You can also loop lists of values:
Note: the above code will print each name on the console.
Please don't get overwhelmed because we will be going over each of these concepts in the following lessons. You will get more opportunities to practice, learn and unleash your brain´s potential!