DEV Community

Nkemchor Duru
Nkemchor Duru

Posted on

PYTHON PROGRAMMING-Lesson 2: Python Operators

We will begin this topic by looking into what Python operators mean. Python operators are special symbols or keywords that perform operations on variables and values — like math, comparisons, or combining values.
However, before going indepth we will look briefly into two subtopics
1) Variables(How to declare variables)
2) functions (Type functions and how to find data type
**
HOW TO DECLARE VARIABLES**
✅ Basic Syntax:
variable_name = value
You just choose a name and assign a value using the = sign.
🔹 Examples:

Numbers

age = 25
price = 19.99

Text (string)

name = "Sarah"

Boolean (True or False)

is_active = True

List (collection of items)

fruits = ["apple", "banana", "cherry"]

🧠 Things to remember:

---Variable names must start with a letter or underscore (_)

    ✅ name, _score, age2

    ❌ 2score (starts with a number)

---Python is case-sensitive:name and Name are different variables

---No need to declare a type — Python automatically knows the type based on the value. 
---keywords cannot be used as a variable. There are 36 keywords that cannot be used. 
---variable names can be a combination of alphabet,digit and underscore. 
Enter fullscreen mode Exit fullscreen mode

FUNCTIONS
A function is a block of reusable code that performs a specific task. Instead of writing the same code again and again, you write a function once and use it whenever needed. However, it is important to note that there are built-in functions such as print and type functions
For example:
a=200
b=100
c=300
print(a+b+c)which will give the result 600 as output.
PRINT FUNCTION
print() therefore is a built-in function in Python that displays output on the screen (usually in the terminal or console). It's one of the first functions every Python programmer uses.
✅ Basic usage:

print("Hello, world!")

📤 This prints:

Hello, world!
TYPE FUNCTION
This function tells you the data type of a variable
e.g
a=100
b=10.5
c= "Joshua"
x= True
To find the data type of each variables the following will be inputed
print(type(a)) #Output ---Showing data type is an integer
print(type(b)) #Output --Showing data type is a float
print(type(c)) #Output --- Showing data type is a string
print(type(x)) #Output ---Showing data type is a boolean.

OPERATORS
Now going into operators at the beginning of the tutorial i pointed out that operators are special symbols that perform arithmetic or logical operations. They require operands(Data) to perform their jobs.
e'g 100 + 200
| | |
Operand Operator Operand
We have arithmetic, logical,bitwise, relational and identity operators.

ARITHMETIC OPERATORS
Arithmetic operators are used to perform basic mathematical calculations like addition, subtraction, division, multiplication etc
For Example:
| Operator | Description | Example | Result |
| -------- | --------------------------------- | -------- | ------ |
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 10 - 4 | 6 |
| * | Multiplication | 7 * 2 | 14 |
| / | Division (float) | 9 / 3 | 3.0 |
| // | Floor Division (integer division) | 9 // 4 | 2 |
| % | Modulus (remainder) | 10 % 3 | 1 |
| ** | Exponentiation (power) | 2 ** 3 | 8 |

Example:

a = 10
b = 3

print(a + b) # 13 which is 10+13
print(a - b) # 7 which is 10-3
print(a * b) # 30 which is 10 x3
print(a / b) # 3.3333333333333335 which is 10÷3
print(a // b) # 3 which is the number of times 3 can go in 10 and which is rounded down to a whole number because both numbers are whole numbers.
print(a % b) # 1 which is the remainder when 10 is divided by 3
print(a ** b) # 1000 which is 10 raised to power 3

RELATIONAL OPERATORS
Relational operators are used to compare two values. The result of a comparison is always a Boolean value: True or False.
Note:Relational operators can be used to compare strings.
Relational operators are: <,>,=,==,<=,>=!=
E.g
a=200
b=100
greater_than= a> b
print (greater_than) The result of this will be True (boolean) bedcause 200 is greater than 100.
similarly if it was
a=200
b=100
print(a<b) it will return false because 200 is not less than 100.
also if it were
a=200
b=100
print(a<=b) it will return false because 200 is not less than or equal to 100.

LOGICAL OPERATORS
Logical operators are used to combine multiple conditions and return a Boolean result (True or False).

AND | *OR * |
T and T = True |T or T=True |
T and f = False |T or F=True |
F and T = False |F or T =True |
F and F= False |F or F =False |

*NOT *
The not operator reverses the truth value of a condition.
If the condition is True, not makes it False.
If the condition is False, not makes it True.

ASSIGNMENT OPERATORS
Assignment operators are used to assign values to variables. Besides the basic =, Python offers shorthand operators that combine an operation with assignment.
🔹 Common Assignment Operators:
Operator| Meaning |Example |Explanation
= Simple assignment |x = 5 | Assigns 5 to x
+= Add and assign | x += 3 | Same as x = x + 3
-= Subtract and assign |x -= 2 | Same as x = x - 2
= Multiply and assign |x *= 4 | Same as x = x * 4
/= Divide and assign |x /= 5 | Same as x = x / 5
//= Floor divide and assign| x //=3 | Same as x = x // 3
%= Modulus and assign |x %= 2 | Same as x = x % 2
*
= Exponent and assign |x *= 3 | Same as x = x * 3

Example:

x = 10
x += 5 # x = 10 + 5 = 15
print(x) # Output: 15

x *= 2 # x = 15 * 2 = 30
print(x) # Output: 30

x %= 7 # x = 30 % 7 = 2
print(x) # Output: 2

Which brings this tutorial to an end. Next, we will be looking at** Type casting and input function**

Top comments (0)