Lädt...

🔧 Python Operators: Arithmetic and Logical Operators Explained


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

An operator is something that performs an operation on values. Just like in mathematics, where we use + for addition, Python provides various types of operators, including:

  1. Arithmetic Operators
  2. Relational Operators
  3. Conditional Operators
  4. Ternary Operators
  5. Logical Operators

In this first part, we will focus on Arithmetic and Logical operators.

Arithmetic Operators in Python

Arithmetic operators are used to perform basic mathematical operations. Here’s a list of them:

+

Adds two numbers (e.g., 5 + 3 = 8)

Subtraction

-

Subtracts the right operand from the left operand (e.g., 10 - 4 = 6)

Multiplication

*

Multiplies two numbers (e.g., 7 * 2 = 14)

Division

/

Divides the left operand by the right operand (e.g., 10 / 2 = 5.0)

Floor Division

//

Returns the quotient, discarding the decimal part (e.g., 12 // 5 = 2)

Modulus

%

Returns the remainder (e.g., 12 % 5 = 2)

Exponentiation

**

Raises one number to the power of another (e.g., 2 ** 3 = 8)

Understanding Floor Division and Modulus

Floor Division (//): This operator returns the quotient but removes the decimal part. Example:

print(12 // 5)  # Output: 2

Here, 12 / 5 would normally give 2.4, but // only takes 2.

Modulus (%): This operator returns the remainder after division. Example:

print(12 % 5)  # Output: 2

Since 12 divided by 5 gives 2 remainder 2, % returns 2.

Exponentiation ()**: This operator raises a number to a power. Example:

print(2 ** 2)  # Output: 4
print(3 ** 2)  # Output: 9

Logical Operators in Python

Logical operators include and, or, and not. They operate on conditions, not numbers. For example, if we want to check whether 4 > 3 and 4 > 1 simultaneously, we use logical operators. These conditions evaluate to True or False.

AND (and): Returns True only if both conditions are True. If any condition is False, the result will be False.

OR (or): Returns True if at least one condition is True. The result is False only when both conditions are False.

NOT (not): This operator negates the result. If a condition is True, not makes it False, and vice versa.

In computer language, we often denote True with 1 and False with 0.

*AND behaves like multiplication:
*

1 and 1 → 1

1 and 0 → 0

0 and 1 → 0

0 and 0 → 0

*OR is like choosing the larger value:
*

1 or 1 → 1

1 or 0 → 1

0 or 1 → 1

0 or 0 → 0

*NOT reverses the condition:
*

not True → False

not False → True

Example Usage:

x = 4
y = 3

# AND operator
print(x > 3 and x > 1)  # Output: True

# OR operator
print(x > 10 or x > 3)  # Output: True

# NOT operator
print(not (x > 3))  # Output: False

This concludes our first part on Arithmetic and Logical Operators. In the next part, we will explore Relational, Conditional, and Ternary Operators in Python. Stay tuned!

Happy coding! 🚀

...

🔧 Python Operators: Arithmetic and Logical Operators Explained


📈 68.95 Punkte
🔧 Programmierung

🔧 Operators (Logical Operators)


📈 35.32 Punkte
🔧 Programmierung

🔧 33.Operators (Logical Operators)


📈 35.32 Punkte
🔧 Programmierung

🔧 33.Operators (Logical Operators)


📈 35.32 Punkte
🔧 Programmierung

🔧 Arithmetic Operators in Python


📈 33.56 Punkte
🔧 Programmierung

🔧 Part II: Foundations of Logical Thinking in Programming, Logical Connectors


📈 32.41 Punkte
🔧 Programmierung

🔧 PYTHON LOGICAL OPERATORS


📈 30.64 Punkte
🔧 Programmierung

🔧 Python Logical OR Operator Usage Explained


📈 29.57 Punkte
🔧 Programmierung

🔧 Tsonnet #3 - Supporting arithmetic operators


📈 28.68 Punkte
🔧 Programmierung

🔧 Tsonnet #3 - Supporting arithmetic operators


📈 28.68 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Operators: From Arithmetic to Ternary


📈 28.68 Punkte
🔧 Programmierung

🔧 Day 5: C++ language | Arithmetic Operators


📈 28.68 Punkte
🔧 Programmierung

🔧 Introduction to Arithmetic Operators in GBase 8a MPP Cluster


📈 28.68 Punkte
🔧 Programmierung

🔧 Day 1: Arithmetic Operators Solution


📈 28.68 Punkte
🔧 Programmierung

🔧 Arithmetic Operators in JavaScript


📈 28.68 Punkte
🔧 Programmierung

🔧 Understanding JavaScript's ?? (Nullish Coalescing) and || (Logical OR) Operators 🤔💡


📈 26.9 Punkte
🔧 Programmierung

🔧 Mastering Logical Operators in JavaScript: `||`, `&&`, and `!`


📈 26.9 Punkte
🔧 Programmierung

🔧 Short-Circuiting and Logical Operators in JavaScript: &&, ||, ??


📈 26.9 Punkte
🔧 Programmierung

🔧 Logical operators - mantiq operatorlari


📈 25.76 Punkte
🔧 Programmierung

🐧 Boolean Logical Operators in C#


📈 25.76 Punkte
🐧 Linux Tipps

🔧 Day 8 : C++Language| Logical Operators


📈 25.76 Punkte
🔧 Programmierung

🔧 Js in bits - 11.5(Logical Operators)


📈 25.76 Punkte
🔧 Programmierung

🔧 Js in bits - 11.3(Logical Operators)


📈 25.76 Punkte
🔧 Programmierung

🔧 Js in bits - 11.4(Logical Operators)


📈 25.76 Punkte
🔧 Programmierung

🔧 Js in bits - 11.2.1(Logical Operators)


📈 25.76 Punkte
🔧 Programmierung

🔧 Js in bits - 11.2(Logical Operators)


📈 25.76 Punkte
🔧 Programmierung

🔧 Js in bits - 11.1(Logical Operators)


📈 25.76 Punkte
🔧 Programmierung

🔧 Js in bits - 11.1(Logical Operators)


📈 25.76 Punkte
🔧 Programmierung

🔧 Python : Basics Concepts in Arithmetic, Data Types, and Conditional Logic


📈 25.14 Punkte
🔧 Programmierung

matomo