Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


LoginGet Started
← Back to Lessons
  • Conditions

  • Python

Edit on Github

Introduction to Python

Why Python?
Python Syntax

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, understanding how to use variables, functions, logical operations and conditionals. This lesson should be the first 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.

Why Python?

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 Development, Web Development, Networks, Automation, etc.

Here are some of the reasons Python has come to this point:

SimplicityPerformance
Python was meant to be simple and easy. Here is the Python manifest:
https://en.wikipedia.org/wiki/Zen_of_Python

Note: No more semicolons or curly brackets anymore, 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 ones, like Node.js can beat it.

Python's scalability has been proven over and over with applications like Google Search Engine, YouTube, Google Apps, etc.
CommunityTools
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: MatLab (for data processing), Pandas (big data), Web.py (web server), 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).

Python Syntax

Printing a value

An essential thing you can do in any programming language is print some messages on the terminal. In Python you can use the print statement:

Loading...

Declaring a variable

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's area, you have to do:

Loading...

Your code will calculate the area no matter the value of width:

Loading...

Variables make your code reusable and dynamic.

Printing a variable

You can also print from a stored variable value, or even concatenate it to another string using the plus + operator:

Loading...

Data-types

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 own types of data. In Python, these are the types (there are more types that we will review later on):

Group typeTypeDescription
Text TypestrWords, paragraphs, etc.
Numeric Typesint, float, complexMath operations, etc.
Sequence Typeslist, tuple, rangeMultiple values at once, one after the other
Mapping TypedictionaryMultiple values without sequence
Boolean TypeboolOnly True or False

Casting (parsing) Data-Types

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.

Why?

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:

Loading...

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.

Lists and Tuples

All the major programming languages have ways of storing lists of values together; these are called lists or arrays.

A Python list is an ordered succession of values, for example:

Loading...

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:

Loading...

Important: Lists start at position 0.

Lists are broadly used in every programming language, especially Python, because Machine Learning uses enormous lists of information.

Note: we will discuss lists in more detail soon.

Python Algorithms

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.

Loading...

The first line is name = "Taylor" and it's executed first; the last line with the print statement already knows the value of name because it was completed first (earlier line).

Conditionals

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:

Loading...

Note: The 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.

Logical Operations and Expressions

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:

OperationSyntaxExamples
Equal to==Is 5 == 5? True!
Is 5 == 4? False
Is 5 == '5'? False
Not Equal to!=Is 5 != 5? False!
Is 5 != '5'? True
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 7 >= 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.

OperationSyntaxisExamples
ANDandWith AND, all operations 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!
ORorWith OR, if at least one of the operations is True, the final result will be True.
Is ('Oscar' != 'Maria' or 2 != 2)? True!
Is (5 == '5' and 'Ramon' != 'Pedro') or (2 == 2)? True!
NOTnotNOT will be the exact opposite of the result of the logical operator:
Is not (5 > 5)? True!
Is not True? False!

Control the Flow of Your Code

Okay, now is when everything starts getting fun! To control your application's 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.

Conditionals: If...else

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.

Loading...

Loops

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:

Loading...

You can also loop lists of values:

Loading...

Ready to start learning?

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!