Python Crash Course for Beginners | Operators in Python

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.

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!