Courses from $13.99 USD plus 20% extra with the code BLACK | We bring you closer to your goals

BLACK

Kickstart your career! Fill your cart with courses starting at $13.99 USD . Limited time offer!

Introduction to Python Language

Introducción al lenguaje Python

Jerotshi Cáceres |

Python is a programming language used to develop applications of any kind. It is an interpretive language, which means that it does not need to be transformed to process applications written in Python. Python is executed directly by the computer using a program called an interpreter, so it does not need to be converted to machine language.


However, even though Python language is interpretive, it does not mean that an interpreter is needed to execute a script written in Python language without compiling it.


Main features of the Python language:


This high-level Python programming language, which is also readable in its code, has the following main characteristics:


It is Multiparadigm, which means it supports imperative programming and goal-oriented programming.


✔ It is Multiplatform: A Python language interpreter can be found for the following operating systems: Linux, Windows and Mac OS. It is possible to reuse the same code on each of them.


✔ Dynamically typed: This means that the type of the variables is resolved at runtime.


✔ Strongly typed: This means that a variable cannot be used in an environment outside of its type.


✔ Interpreted: The source code is not compiled into machine language.

What version of the Python language do I have installed?


If you already have Python installed , open a terminal and add the command python3. This command will launch the appropriate Python interpreter.


You will see something similar to this image:

language-python-version

As shown in the image, the first line will show the version of the Python interpreter you have installed on your computer. In our case, we have version 3.7.4.


In it, we can write expressions and instructions that it will understand and execute.


Examples of programs using Python language


Typically, programs developed using the Python language are written in files with the .py extension. What you need to do is pass it to the Python interpreter so that it can interpret and execute it.


Example: Create a file called “suma.py” that contains the following:

sum = 2 + 3

print(sum)


Next step, open a terminal, position yourself in the directory where you created the suma.py file and run the python3 suma.py code .


In the terminal you will see the number 5 as a result of running the previous program. This happens because the Python interpreter has read and executed the code you have written in the file suma.py . This is the most common way to create and run programs under the Python language .

20% EXTRA DISCOUNT


Get started today and get fully certified in Python with our course


  • 100% online at your own pace
  • Practical exercises
  • Lifetime Access
  • Certified endorsed


Apply the coupon [DARE] and get an extra 20% discount for only 100 students. Click the button and join!

Operators, expressions and statements in Python language


Operator, expression and statement are the most basic forms that make up the structure of any program. That is why we want you to learn perfectly the difference between these structures:


Operator


An operator can be made up of a character or a set of characters that is executed on one, two or more variables to perform an operation that leads to a specific result.


Some examples of common operators are arithmetic operators like + (addition), - (subtraction) or * (product). However, there are other operators in the Python language .


Expression


An expression can be defined as a set of code that returns a value and is made up of a composition of operands and operators. Below is an example. It should be noted that each line is a different expression:


5 + 2 # Sum of 5 and 2

a < 10 # Compares whether the value of variable a is less than 10

b is None # Compares if the identity of variable b is None

3 * (200 - c) # Subtracts the value of c from 200 and multiplies it by 3.


Judgment


A statement in the Python language is an instruction that establishes an action. A statement could be made up of one or more expressions, of course this is not always the case.


In short, sentences are the rules that make up the program and define its behavior.


Some examples of statements are the assignment = or , the if, if, else, for or while statements, etc.


Statements of more than one line: Generally, statements are one line only, example; a = 2 + 3 # Assigns to the variable <a> the result of 2 + 3


However, very long statements can span more than one line. Here we recommend a maximum length of 72 characters per line.

If we want to divide a statement into several lines, we must use the character ( \ ).


Example:

a = 2 + 3 + 5 + \

7 + 9 + 4 + \

6


In the Python language , in addition to the separation that is done with the \ character, the line continuation is understood as long as the expression is within the characters {}, [] and ().


For example, we can start a list like this:

a = [1, 2, 7,

3, 8, 4,

9]


If you don't know what a list is or you don't understand the previous example, no problem, the importance here is that you understand that the above is a multi-line sentence since it is made up of the characters [].

FREE DOWNLOAD

Developed with the intention of making you aware of the importance of this programming language, its main elements, its development environment, its libraries and object-oriented programming.

Code blocks in the Python language


It is important that you are clear that a block of code in Python language is a group of statements that are related and well delimited.


Unlike programming languages ​​such as JAVA or C, in which the characters {} are used to establish a block of code, in the Python language, indentation is used.


This consists of moving a block of text to the right, including spaces or tabs at the beginning of the line, leaving a margin on the left.


A block in the Python language starts with a new indentation and ends with the first line with the smallest indentation. The Python style guide recommends using spaces instead of tabs to perform indentation.


Example:


def sum_numbers (numbers):

def sum_numbers(numbers): # Block 1

sum = 0 # Block 2

for n in numbers: # Block 2

sum += n # Block 3

print(sum) # Block 3

return sum # Block 2


From this example, the important thing to understand is that on line 1 the sum_numbers function is established. The body of this function is established by the set of statements that correspond to block 2 and 3. At the same time, the for statement defines the actions to be performed in the group of statements that correspond to block 3.


Comments in Python Language


Python allows you to write comments in your code. These comments are used to explain why you are programming something in a certain way or simply to add instructions or hints. These comments in the Python language are useful when you return to a program or application after a while.


The interpreter ignores these comments. They make sense only to programmers who understand the Python language.


To add a comment to the code you just have to start a line with the # character:


# This line is a comment

a = 5

# Result of multiplying a by 2

print(a * 2)


This is not all about the Python language, it is just a brief introduction. We still have much more to say. But if you want to get started today on this wonderful and intuitive programming language, don't miss our Python online course.

Courses that may interest you