Exam code:1CP2
What is an operator?
-
An operator is a symbol used to instruct a computer to perform a specific operation on one or more values
-
Examples of common operators include:
-
Arithmetic
-
Relational
-
Logical (AND, OR and NOT)
-
Arithmetic Operators
-
There are a range of arithmetic operators that must be used in programming questions
-
The table below summarises each of the operators
|
Operator |
Python |
|---|---|
|
Addition |
|
|
Subtraction |
|
|
Multiplication |
|
|
Division |
|
|
Modulus (remainder after division) |
|
|
Quotient (whole number division) |
|
|
Exponentiation (to the power of) |
|
-
To demonstrate the use of common arithmetic operators, three sample programs written in Python are given below
-
Comments have been included to help understand how the arithmetic operators are being used
-
Arithmetic operators #1 – a simple program to calculate if a user-entered number is odd or even
-
Arithmetic operators #2 – a simple program to calculate the area of a circle from a user-inputted radius
-
Arithmetic operators #3 – a simple program that generates 5 maths questions based on user inputs and gives a score of how many were correctly answered at the end
-
|
Python code |
|---|
|
|
Relational Operators & Logical Operations
-
There are a range of relational and logical operators that must be used in programming questions
-
The tables below summarise each of the operators
Relational Operators
|
Operator |
Python |
|---|---|
|
Equal to |
|
|
Not equal to |
|
|
Less than |
|
|
Less than or equal to |
|
|
Greater than |
|
|
Greater than or equal to |
|
Logical Operators
-
Logical operators can be categorised into three
-
AND
-
OR
-
NOT
-
-
Logical operators are always used alongside relational operators to form a comparison, some examples of this are:
-
If number > 5 AND number <10:print("Valid number") -
If number <0 OR number >11:print("You are outside the range, try again")
-
Examples
|
Operator |
Python |
|---|---|
|
Addition |
|
|
Multiplication |
|
|
Modulus |
|
|
Quotient |
|
|
Exponentiation |
|
|
Equal to |
|
|
Not equal to |
|
|
Greater than or equal to |
|
|
AND |
|
-
To demonstrate the use of common operators, three sample programs written in Python are given below
-
Comments have been included to help understand how the operators are being used
-
Common operators #1 – a simple program that assigns Boolean values to two variables and outputs basic comparisons
-
Common operators #2 – a simple program to output a grade based on a users score
-
Common operators #3 – a simple program reads a text files and searches for an inputted score
-
|
Python code |
|---|
|
|
Responses