Symbols for Performing Operations, Operators
In programming, an Operator refers to a symbol or keyword used to perform a specific operation.
Key Python Operators
Operators are symbols that perform mathematical or logical operations. In Python, the following operators are mainly used:
Arithmetic Operators
Operators like +(addition), -(subtraction), *(multiplication), /(division), **(exponentiation), %(modulus), //(floor division) are used to perform arithmetic operations between numbers.
multiply = 10 * 5 # 50 division = 10 / 2 # 5.0, Division in Python returns a float integer_division = 10 // 3 # 3, Returns the integer part of the division result remainder = 10 % 3 # 1 power = 2 ** 3 # 8
Assignment Operators
Used to store values in variables, either directly or as the result of an operation.
-
=: Assign the value on the right to the variable on the left (e.g.,x = 1assigns 1 to the variable x) -
+=: Add the value on the right to the variable on the left, then assign the result to the left variable (e.g.,x += 1is equivalent tox = x + 1) -
-=: Subtract the value on the right from the variable on the left, then assign the result to the left variable (e.g.,x -= 1is equivalent tox = x - 1)
In Python, = is used for assignment, meaning it stores the value on the right into the variable on the left.
To check if two values are equal, use the comparison operator ==.
x = 10 y = 20 x += 10 # Equivalent to x = x + 10 print(x) # 20
Comparison Operators
Compare values to see if they are equal, greater, or less.
-
==(equal to),!=(not equal) -
>(left is greater),<(right is greater) -
>=(left is greater or equal),<=(right is greater or equal)
x = 10 y = 20 # == : equal to print(x == y) # False # != : not equal to print(x != y) # True # > : left is greater print(x > y) # False # <= : right is greater or equal print(x <= y) # True
Logical Operators
Perform logical operations within a program.
-
and: The result is true only if both the left and right conditions are true. -
or: The result is true if at least one of the left or right conditions is true. -
not: Reverses the result of the condition.
x = 10 y = 20 # and: Result is true only if both conditions are true print(x > 5 and y > 15) # Both conditions are true, so the result is True # or: Result is true if at least one condition is true print(x < 5 or y > 15) # The condition y > 15 is true, so the result is True # x > 5 is true, but the not operator reverses the result to False print(not x > 5)
Coding Practice
In programs, the multiplication symbol is represented by an asterisk (*).
Store the result of multiplying 10 and 5 in the multiply variable of the blank space and print the result.
Arithmetic Operator Problem
Store the result of multiplying 10 and 2 in a variable and print it. The output should be 20.
a = 10b = 2result = a bprint(result)Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result