Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Comparison operators | If...Else Statement
Python Tutorial for Beginners
course content

Contenido del Curso

Python Tutorial for Beginners

Python Tutorial for Beginners

1. Introduction to Programming
2. Variables in Python
3. Comments
4. If...Else Statement

Comparison operators

Let's talk more about ==. It is one of the comparison operators, in particular the equality operator. You can use it to compare two things to see if they are equal. For example, you can compare the following:

  • a variable with a string,
  • a variable with a number,
  • a variable with a math expression,
  • a variable with a variable.

Let's look at the example of using this operator:

1
if name = "Alex":
copy
1
if number_1 + number_2 == 50:
copy
1
if total_cost == previous_cost + 120:
copy

Another comparsion operator, !=, is the opposite of ==, It means is not equal to.

123
your_ticket_number = 357 if your_ticket_number != 777: print("Better luck next time")
copy

Here are 4 more comparsion operators, usually used to compare numbers.

  • > - is greater than
  • < - is less than
  • >= - is greater than or equal to
  • <= - is less than or equal to

Tarea

Write an if statement that tests whether a first variable is greater than a second variable. If the test passes, make the second variable equal to the first variable. The name of the first variable is number_1, the name of the second variable is number_2. Display the result of the number_2.

Tarea

Write an if statement that tests whether a first variable is greater than a second variable. If the test passes, make the second variable equal to the first variable. The name of the first variable is number_1, the name of the second variable is number_2. Display the result of the number_2.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 4. Capítulo 2
toggle bottom row

Comparison operators

Let's talk more about ==. It is one of the comparison operators, in particular the equality operator. You can use it to compare two things to see if they are equal. For example, you can compare the following:

  • a variable with a string,
  • a variable with a number,
  • a variable with a math expression,
  • a variable with a variable.

Let's look at the example of using this operator:

1
if name = "Alex":
copy
1
if number_1 + number_2 == 50:
copy
1
if total_cost == previous_cost + 120:
copy

Another comparsion operator, !=, is the opposite of ==, It means is not equal to.

123
your_ticket_number = 357 if your_ticket_number != 777: print("Better luck next time")
copy

Here are 4 more comparsion operators, usually used to compare numbers.

  • > - is greater than
  • < - is less than
  • >= - is greater than or equal to
  • <= - is less than or equal to

Tarea

Write an if statement that tests whether a first variable is greater than a second variable. If the test passes, make the second variable equal to the first variable. The name of the first variable is number_1, the name of the second variable is number_2. Display the result of the number_2.

Tarea

Write an if statement that tests whether a first variable is greater than a second variable. If the test passes, make the second variable equal to the first variable. The name of the first variable is number_1, the name of the second variable is number_2. Display the result of the number_2.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 4. Capítulo 2
toggle bottom row

Comparison operators

Let's talk more about ==. It is one of the comparison operators, in particular the equality operator. You can use it to compare two things to see if they are equal. For example, you can compare the following:

  • a variable with a string,
  • a variable with a number,
  • a variable with a math expression,
  • a variable with a variable.

Let's look at the example of using this operator:

1
if name = "Alex":
copy
1
if number_1 + number_2 == 50:
copy
1
if total_cost == previous_cost + 120:
copy

Another comparsion operator, !=, is the opposite of ==, It means is not equal to.

123
your_ticket_number = 357 if your_ticket_number != 777: print("Better luck next time")
copy

Here are 4 more comparsion operators, usually used to compare numbers.

  • > - is greater than
  • < - is less than
  • >= - is greater than or equal to
  • <= - is less than or equal to

Tarea

Write an if statement that tests whether a first variable is greater than a second variable. If the test passes, make the second variable equal to the first variable. The name of the first variable is number_1, the name of the second variable is number_2. Display the result of the number_2.

Tarea

Write an if statement that tests whether a first variable is greater than a second variable. If the test passes, make the second variable equal to the first variable. The name of the first variable is number_1, the name of the second variable is number_2. Display the result of the number_2.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Let's talk more about ==. It is one of the comparison operators, in particular the equality operator. You can use it to compare two things to see if they are equal. For example, you can compare the following:

  • a variable with a string,
  • a variable with a number,
  • a variable with a math expression,
  • a variable with a variable.

Let's look at the example of using this operator:

1
if name = "Alex":
copy
1
if number_1 + number_2 == 50:
copy
1
if total_cost == previous_cost + 120:
copy

Another comparsion operator, !=, is the opposite of ==, It means is not equal to.

123
your_ticket_number = 357 if your_ticket_number != 777: print("Better luck next time")
copy

Here are 4 more comparsion operators, usually used to compare numbers.

  • > - is greater than
  • < - is less than
  • >= - is greater than or equal to
  • <= - is less than or equal to

Tarea

Write an if statement that tests whether a first variable is greater than a second variable. If the test passes, make the second variable equal to the first variable. The name of the first variable is number_1, the name of the second variable is number_2. Display the result of the number_2.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 4. Capítulo 2
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt