LEARN TO CODE > PYTHON > OPERATORS

NEXT PAGE >
IF STATEMENTS 
WHAT ARE OPERATORS
Operators are used to do mathematical operations, comparison operations, logic operators and assigning value operations for example if you do: num1 = 10 then you have used the = (equals) operator to assign the value of 10 to the variable num1. There are many different operators as can be seen in the four categories below.
WATCH THIS VIDEO TO GUIDE YOU THROUGH THIS SECTION
SECTION 1  ARITHMETIC OPERATORS
As the name suggests, arithmetic operators are used to perform calculations. Popular arithmetic operators can be seen in the table below.
STECTION 2  COMPARISON OPERATORS
As the name suggests, comparison operators are used compare values. Popular comparison operators can be seen in the table below.
REMEMBER:
 One equals sign, means make it equal to
 Two equal signs mean is it equal to ?
SECTION 3  ASSIGNING OPERATORS
As the name suggests, assigning operators are used assign values to variables. Popular assigning operators can be seen in the table below.
SECTION 4  LOGIC OPERATORS
As the name suggests, logic operators are used like logic gate, they are frequently used in IF statements and can be used as stand alone operators. Popular logic operators can be seen in the table below.
QUESTION
1:Which operator could you use to determine if a given value is odd or even, and how could you use it?
TASK
Create a program to:
1: Ask the user to enter a number
2: Write a line of code to calculate the DIV of the number given by 2
3: Output to the user the answer in a user friendly string concatenated with the numeric answer.
TRY
Have a go at using some of the other operators to see how they work, don't be afraid to experiment.
1:Which operator could you use to determine if a given value is odd or even, and how could you use it?
TASK
Create a program to:
1: Ask the user to enter a number
2: Write a line of code to calculate the DIV of the number given by 2
3: Output to the user the answer in a user friendly string concatenated with the numeric answer.
TRY
Have a go at using some of the other operators to see how they work, don't be afraid to experiment.
Question 1: Arithmetic Operators
What will be the result of the following Python code?
x = 5 y = 2 result = x ** y print(result)
A: 10
B: 25
C: 7
D: 32
EXPLANATION
Correct Answer: D) 32
Explanation: The ** operator in Python is the exponentiation operator. So 5 ** 2 means 5 raised to the power of 2, which equals 25.
Explanation: The ** operator in Python is the exponentiation operator. So 5 ** 2 means 5 raised to the power of 2, which equals 25.
Question 2: Comparison Operators
Which of the following is a valid Python comparison operator?
A: =
B: !=
C: !>
D: <<
EXPLANATION
Correct Answer: B) !=
Explanation: The != operator checks if two values are not equal. The other options are either used incorrectly (= is assignment, not comparison) or are not valid operators in Python.
Explanation: The != operator checks if two values are not equal. The other options are either used incorrectly (= is assignment, not comparison) or are not valid operators in Python.
Question 3: Assignment Operators
What does the following Python code do?
x = 10 x += 5 print(x)
A: Adds 10 and 5, resulting in 15
B: Subtracts 5 from 10, resulting in 5
C: Multiplies 10 by 5, resulting in 50
D: Divides 10 by 5, resulting in 2
EXPLANATION
Correct Answer: A) Adds 10 and 5, resulting in 15
Explanation: The += assignment operator adds the righthand value (5) to the lefthand value (10) and assigns the result back to x. So x becomes 15.
Explanation: The += assignment operator adds the righthand value (5) to the lefthand value (10) and assigns the result back to x. So x becomes 15.
Question 4: Logical Operators
What will be the result of the following code?
a = True b = False result = a and b print(result)
A: True
B: False
C: None
D: Error
EXPLANATION
Correct Answer: B) False
Explanation: The and operator returns True only if both operands are True. Since a is True and b is False, the result is False.
Explanation: The and operator returns True only if both operands are True. Since a is True and b is False, the result is False.
Question 5: Combination of Operators
What will be the result of this expression?
x = 7 y = 3 z = (x > y) or (x == y) print(z)
A: True
B: False
C: 7
D: None
EXPLANATION
Correct Answer: A) True
Explanation: The or operator returns True if at least one of the conditions is True. In this case, x > y (7 > 3) is True, so the whole expression is True, even though x == y is False.
Explanation: The or operator returns True if at least one of the conditions is True. In this case, x > y (7 > 3) is True, so the whole expression is True, even though x == y is False.