Python Crash Course for Beginners | Operators in Python
Python is a popular programming language that allows developers to perform a wide range of operations, from basic arithmetic to complex logical operations. Python operators are used to perform different types of operations, such as arithmetic operations, comparison operations, and logical operations. In this article, we’ll explore the different types of operators in Python and provide coding examples with explanations.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations, such as addition, subtraction, multiplication, and division. Here are some of the most commonly used arithmetic operators in Python:
Addition (+): The addition operator is used to add two or more values together. For example, the following code adds two numbers together:
x = 10
y = 5
z = x + y
print(z) # Output: 15
Subtraction (-): The subtraction operator is used to subtract one value from another. For example, the following code subtracts one number from another:
x = 10
y = 5
z = x - y
print(z) # Output: 5
Multiplication (*): The multiplication operator is used to multiply two or more values together. For example, the following code multiplies two numbers together:
x = 10
y = 5
z = x * y
print(z) # Output: 50
Division (/): The division operator is used to divide one value by another. For example, the following code divides one number by another:
x = 10
y = 5
z = x / y
print(z) # Output: 2.0
Modulo (%): The modulo operator is used to find the remainder when one value is divided by another. For example, the following code finds the remainder when one number is divided by another:
x = 10
y = 3
z = x % y
print(z) # Output: 1
Comparison Operators
Comparison operators are used to compare two values and return a Boolean value (True or False) depending on whether the comparison is true or false. Here are some of the most commonly used comparison operators in Python:
Equal to (==): The equal to operator is used to compare two values to see if they are equal. For example, the following code checks if two numbers are equal:
x = 10
y = 5
print(x == y) # Output: False
Not equal to (!=): The not equal to operator is used to compare two values to see if they are not equal. For example, the following code checks if two numbers are not equal:
x = 10
y = 5
print(x != y) # Output: True
Greater than (>): The greater than operator is used to check if one value is greater than another. For example, the following code checks if one number is greater than another:
x = 10
y = 5
print(x > y) # Output: True
Less than (<): The less than operator is used to check if one value is less than another. For example, the following code checks if one number is less than another:
x = 10
y = 5
print(x < y) # Output: False
Greater than or equal to (>=): The greater than or equal to operator is used to check if one value is greater than or equal to another. For example, the following code checks if one number is greater than or equal to another:
x = 10
y = 5
print(x >= y) # Output: True
Less than or equal to (<=): The less than or equal to operator is used to check if one value is less than or equal to another. For example, the following code checks if one number is less than or equal to another:
x = 10
y = 5
print(x <= y) # Output: False
Logical Operators
Logical operators are used to perform logical operations on Boolean values (True or False). Here are some of the most commonly used logical operators in Python:
And (and): The and operator returns True if both of the operands are True. For example, the following code checks if two conditions are True:
x = 10
y = 5
print(x > y and x < 20) # Output: True
Or (or): The or operator returns True if either of the operands is True. For example, the following code checks if at least one of two conditions is True:
x = 10
y = 5
print(x > y or x == 10) # Output: True
Not (not): The not operator returns the opposite of the operand. For example, the following code checks if a condition is not True:
x = 10
y = 5
print(not(x > y)) # Output: False
Assignment Operators
Assignment operators are used to assign values to variables. Here are some of the most commonly used assignment operators in Python:
Equals (=): The equals operator is used to assign a value to a variable. For example, the following code assigns a value to a variable:
x = 10
Plus equals (+=): The plus equals operator is used to add a value to a variable and then assign the result to the variable. For example, the following code adds a value to a variable:
x = 10
x += 5
print(x) # Output: 15
Minus equals (-=): The minus equals operator is used to subtract a value from a variable and then assign the result to the variable. For example, the following code subtracts a value from a variable:
x = 10
x -= 5
print(x) # Output: 5
Times equals (*=): The times equals operator is used to multiply a variable by a value and then assign the result to the variable. For example, the following code multiplies a variable by a value:
x = 10
x *= 5
print(x) # Output: 50
Divide equals (/=): The divide equals operator is used to divide a variable by a value and then assign the result to the variable. For example, the following code divides a variable by a value:
x = 10
x /= 5
print(x) # Output: 2.0
In summary, python operators are essential for performing different types of operations, from basic arithmetic to complex logical operations. Understanding how to use Python operators can help you write more efficient and effective code. By mastering Python operators, you can become a more effective Python programmer and take on more challenging programming tasks.
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.