Basic Operators

In this class, we will delve into basic Python operators, which are symbols used to perform operations on values ​​and variables.

Types of operators Link to heading

  • Arithmetic operators: They are used to perform mathematical operations such as addition, subtraction, multiplication, division and module.
  • Comparison operators: They are used to compare two values ​​and obtain a boolean value (True or False).
  • Logical operators: They are used to combine boolean values ​​and obtain a boolean value.

Arithmetic operators Link to heading

  • +: Sum
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • //: Integer division
  • %: Module (remainder of division)

Example Link to heading

# Addition
result = 10 + 5

# Subtraction
difference = 10 - 5

# Multiplication
product = 10 * 5

# Division
quotient = 10 / 5

# Integer division
integer_division = 10 // 5

# Module
residue = 10 % 5

print(result, difference, product, quotient, integer_division, residue)

Comparison operators Link to heading

  • ==: Equality
  • !=: Inequality
  • <: Less than
  • <=: Less than or equal to
  • >: Greater than
  • >=: Greater than or equal to

Example Link to heading

# Equality
equality = 10 == 10

# Inequality
inequality = 10 != 10

# Smaller than
less_than = 10 < 15

# Less than or equal to
less_than_or_equal_than = 10 <= 15

# Greater than
greater_than = 10 > 5

# Greater than or equal
greater_than_equal_than = 10 >= 5

print(equality, inequality, less_than, less_or_equal_than, greater_than, greater_than_or_equal_than)

Logical operators Link to heading

  • and: Conjunction (both values ​​must be True)
  • or: Disjunction (at least one value must be True)
  • not: Negation (inverts the boolean value)

Example Link to heading

# Conjunction
conjunction = (10 > 5) and (10 < 15)

# Disjunction
disjunction = (10 == 10) or (10 != 10)

# Denial
negation = not (10 == 10)

print(conjunction, disjunction, negation)

Exercises Link to heading

Write a Python code that calculates the area of ​​a triangle.

<< Lists String Formatting >>