+ INFORMATION

Share on social networks!

Basic operators in Python

In the previous posts we told you the predefined functions and the Keywords in python, this week we are going to talk about the basic operator types in Python and how to use each of them with some examples.

The operators They are symbols that tell the interpreter to perform a specific operation, such as arithmetic, comparison, logic, etc.

In this post we are going to learn about assignment, arithmetic, relational and logical operators.

We remind you that if you want to learn more about Python, we invite you to stop by our master in Advanced Programming in Python for Big Data, Hacking and Machine Learning in EIP where in just 12 months, you will become a widely qualified expert.

Assignment operators

They are the ones we use to give value to a variable. Normally we usually combine them with other operators to carry out an operation. It is a way of summarizing code.

Operator DescriptionExample
=Assigns the value to the variable>>> x=5; print(«Result «, x)      
Result 5
+=Add the value to the right of the equal to the variable>>> x=5; x+=2; print(«Result «, x)
Result 7
-=Subtract the value to the right of the variable X>>> x=5; x-=2; print(«Result «, x)
Result 3
*=Multiply the value to the right of equal to the variable>>> x=5; x*=2; print(«Result «, x)
Result 10
/=Divide the value to the right of the variable X>>> x=5; x/=2; print(«Result «, x)
Result 2.5
%=Returns the remainder of the division of the value to the right of the equal with the variable>>> x=5; x%=2; print(«Result «, x)
Result 1
//=Returns the quotient of the division of the value to the right of the equal with the variable>>> x=5; x//=2; print(«Result «, x) Results 2
**=Calculates the exponent to the variable X according to the value to the right of the equal>>> x=5; x**=2; print(«Result «, x) 
Result 25

Arithmetic operators

They are the ones we use to make a arithmetic and mathematical operations.

OperatorDescriptionExample
+Add numeric type values>>> 5+2
7
Subtracts numeric type values>>> 5-2
3
Assigns a negative value to a numeric data>>> -5
-5
*Multiplies numeric type values>>> 5*2
10
/Splits numeric type values>>> 5/2
2.5
%Returns the remainder of the division between values of type numeric>>> 5%2
1
//Returns the quotient of the division between values of numeric type>>> 5//2
2
**Calculates the exponent between numerical type values>>> 5**2
25

Relational operators

They are the ones we use to compare two values. Returns a boolean value (true or false) depending on the condition.

OperatorDescriptionExample
==Compare the values to see if they are the same>>> 5==2 False
!=Compare the values to see if they are different>>> 5!=2
true
<Compares that the value on the left is less than the one on the right>>> 5<2 
False
>Compares that the value on the left is greater than the one on the right>>> 5>2 
true
<=Compares that the value on the left is less than or equal to the one on the right>>> 5<=2
False
>=Compares that the value on the left is greater than or equal to the one on the right>>> 5>=2
true

Logical operators

They are the ones we use to work with boolean values. 

OperatorDescriptionExample
andCheck that the left and right value is met>>> True and False
False
orChecks that the left or right value is true>>> True or False
true
notReturns the opposite value of the boolean value>>> not True
False

Surely you knew most of them, in that case, you can always put them into practice to learn more.

Subscribe to our newsletter to stay up to date with all the news

EIP International Business School informs you that the data in this form will be processed by Mainjobs Internacional Educativa y Tecnológica, SA as the person responsible for this website. The purpose of collecting and processing personal data is to respond to the query made as well as to send information about the services of the data controller. Legitimation is the consent of the interested party.
You can exercise your rights of access, rectification, limitation and deletion of data in compliance@grupomainjobs.com as well as the right to file a claim with the supervisory authority. You can consult additional and detailed information on Data Protection in the Privacy Policy that you will find in our Web page
marter-in-python

Leave a comment