COMPUTER SCIENCE CAFÉ
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
LEARN TO CODE    >     PYTHON     >     OPERATORS
NEXT PAGE >
IF STATEMENTS
Picture
  • LEARN
  • QUICK REFERENCE
  • CODING CHALLENGES
  • QUESTIONS
<
>
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.
Picture
STECTION 2 | COMPARISON OPERATORS
As the name suggests, comparison operators are used compare values. Popular comparison operators can be seen in the table below.
Picture
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.
Picture
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.
Picture
Picture
​ARITHMETIC OPERATORS
Picture
COMPARISON OPERATORS
Picture
ASSIGNING OPERATORS
Picture
LOGIC OPERATORS
Picture
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.
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.
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.
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 right-hand value (5) to the left-hand 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.
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.
Picture
NEXT PAGE | OPERATORS
1
2
3
4
5
6
7
8
9
10
LEVEL 1: GETTING STARTED | Downloading Python and creating Hello World
LEVEL 2: VARIABLES AND DATA TYPES | Storing data in variables
LEVEL 3: OPERATORS | Performing calculations and comparisons
LEVEL 4: SELECTION | Using IF statements
LEVEL 5: ITERATION | Using FOR Loops and WHILE Loops
LEVEL 6: ARRAYS | Getting started with Lists
LEVEL 7: FUNCTIONS AND PROCEDURES | Breaking your code into logical sections
LEVEL 8: FORMATTING DATA | Further data manipulation
LEVEL 9: ERROR CHECKING | Using Try and Except to catch user errors
LEVEL 10: OBJECT ORIENTED PROGRAMMING | Getting to grips with OOP

 PYTHON QUICK REFERENCE
Picture
SUGGESTIONS
We would love to hear from you
SUBSCRIBE 
To enjoy more benefits
We hope you find this site useful. If you notice any errors or would like to contribute material then please contact us.