DEV Community

Tim Mainge
Tim Mainge

Posted on

Basic Operators in Python Programming

Display of Python CodesDid you know that Python utilizes arithmetic operators? These are unique symbols that perform operations on values and variables. Understanding how these operators function is vital for aspiring data analysts or scientists. This article provides a comprehensive guide to mastering the essential operators, laying a solid foundation for your coding journey.

Arthimetic Operators

I perceive you have encountered addition, subtraction, multiplication, and division in your basic mathematics class. In Python, they are compatible with variables, integers, and expressions. Other common arithmetic operators include; floor division, modulus, and exponentiation.

# Addition
Print(10 + 25)

# Subtraction
Print(40 - 8)

# Multiplication
Print(7 * 30)

# Division
Print(15 / 4)

# Modulus
Print(19 % 3)

#Floor Division
Print(5 // 2)

# Exponentiation
Print(2 ** 3)

Enter fullscreen mode Exit fullscreen mode

Comparision Operators

Comparison operators compare the values of two operands. The result of comparison operators is a boolean (True or False). Additionally, these operators determine the relationship between values creating conditional statements.

# Equal to
Print(10 == 10)

# Not equal to
Print(5 != 3)

# Greater than
Print(7 > 4)

# Less than
Print(6 < 9)

# Greater than or equal to
Print(12 >= 10)

# Less than or equal to
Print(15 <= 20)

Enter fullscreen mode Exit fullscreen mode

Assignment Operators

Assignment operators assign values to variables and perform operations simultaneously. The most common assignment operator is the "equal to" symbol (=). However, there are other assignment operators used in Python programming.

# Assign
x = 10

# Addition assignment
x += 5

# Subtraction assignment
x -= 3

# Multiplication assignment
x *= 2

# Division assignment
x /= 4

# Modulus assignment
x %= 3

# Exponent assignment 
x **= 5

# Remainder Assignment 
x %= 2

# Floor Division Assignment 
x //= 9

Enter fullscreen mode Exit fullscreen mode

Logical Operators

They evaluate multiple conditions at the same time. There are three logical operators, namely; "AND", "OR" and "NOT". The result of logical operators is always a boolean (True, False)

# "AND" Operator
x = 2
y = 15
Print(x > 1 and y < 18)  # Output: True
Print(x < 0 and y < 20)  # Output: False

# "OR" Operator
x = 4
y = 9
Print(x > 0 or y < 20)   # Output: True
Print(x < 0 or y < 20)   # Output: True
print(x < 0 or y > 20)   # Output: False

# "NOT" Operator
x = True
Print(not x)  # Output: False

y = False
Print(not y)  # Output: True

Enter fullscreen mode Exit fullscreen mode

Bitwise Operators

Have you ever wondered how you can extract specific bits from an integer without affecting others? Bitwise operators are typically utilized to execute bitwise operations on integer-type entities. Rather than regarding the entity as a whole, it is interpreted as a sequence of individual bits. Python has six bitwise operators: &, |, ^, ~, <<, and >>. Except ~, all these operators are binary, meaning they manipulate two operands. Each operand represents a binary digit (bit), 1 or 0.

Conclusion

Python has a wide range of operators, including arithmetic, comparison, logical, and bitwise operators, each serving specific purposes in programming. Understanding these operators and their functionalities is crucial for effective coding and problem-solving. Python's rich set of operators empowers developers to write cleaner, more concise, and more efficient code. As you delve deeper into Python programming, mastering these operators will undoubtedly enhance your coding prowess and enable you to tackle a diverse range of programming tasks with confidence.

Top comments (1)

Collapse
 
heatherfranco3 profile image
Heather

Very handy for a beginner! Thank you!