Python, as a versatile and beginner-friendly programming language, offers a wide range of basic operations that are essential for performing various computational tasks. These operations include arithmetic, comparison, assignment, and logical operations. In this tutorial, we will delve into the fundamentals of these operations in Python.
Arithmetic Operations
Arithmetic operations are the most basic form of operations in Python. They include addition (+), subtraction (-), multiplication (*), division (/), modulus (%), exponentiation (**), and floor division (//).
pythonCopy Code# Example of arithmetic operations
x = 10
y = 3
print(x + y) # Addition
print(x - y) # Subtraction
print(x * y) # Multiplication
print(x / y) # Division
print(x % y) # Modulus
print(x ** y) # Exponentiation
print(x // y) # Floor division
Comparison Operations
Comparison operations are used to compare two values and return a Boolean value (True or False). They include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).
pythonCopy Code# Example of comparison operations
x = 5
y = 3
print(x == y) # Equal to
print(x != y) # Not equal to
print(x > y) # Greater than
print(x < y) # Less than
print(x >= y) # Greater than or equal to
print(x <= y) # Less than or equal to
Assignment Operations
Assignment operations are used to assign values to variables. The basic assignment operator is ‘=’. Python also supports augmented assignment operators for arithmetic operations.
pythonCopy Code# Example of assignment operations
x = 5
x += 2 # x = x + 2
x -= 2 # x = x - 2
x *= 2 # x = x * 2
x /= 2 # x = x / 2
x %= 2 # x = x % 2
x==‌**= 2 # x = x **‌== 2
x //= 2 # x = x // 2
Logical Operations
Logical operations are used to combine two or more conditions. They include AND (and), OR (or), and NOT (not).
pythonCopy Code# Example of logical operations
a = True
b = False
print(a and b) # AND operation
print(a or b) # OR operation
print(not a) # NOT operation
Understanding these basic operations is crucial for building a strong foundation in Python programming. With these operations, you can perform various computations, make decisions, and manipulate data effectively.
[tags]
Python, Basic Operations, Arithmetic, Comparison, Assignment, Logical, Tutorial