Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Operators in Python

Operators in Python

Operators in Python

In Python, operators are symbols used to perform operations on variables and values. Python supports various types of operators, including arithmetic, comparison, logical, and more. Let's go over them in detail:


1. Arithmetic Operators

These operators are used to perform basic mathematical operations.

OperatorDescriptionExample
+Addition5 + 3 ? 8
-Subtraction5 - 3 ? 2
*Multiplication5 * 3 ? 15
/Division (float result)5 / 2 ? 2.5
//Floor Division5 // 2 ? 2
%Modulo (remainder)5 % 2 ? 1
**Exponentiation5 ** 3 ? 125

Example:

a = 10b = 3print(a + b)  # Output: 13print(a - b)  # Output: 7print(a * b)  # Output: 30print(a / b)  # Output: 3.3333...print(a // b) # Output: 3print(a % b)  # Output: 1print(a ** b) # Output: 1000

2. Comparison Operators

These operators are used to compare values. They return True or False based on the comparison.

OperatorDescriptionExample
==Equal to5 == 3 ? False
!=Not equal to5 != 3 ? True
>Greater than5 > 3 ? True
<Less than5 < 3 ? False
>=Greater than or equal to5 >= 3 ? True
<=Less than or equal to5 <= 3 ? False

Example:

x = 5y = 3print(x == y)  # Output: Falseprint(x != y)  # Output: Trueprint(x > y)   # Output: Trueprint(x < y)   # Output: Falseprint(x >= y)  # Output: Trueprint(x <= y)  # Output: False

3. Logical Operators

Logical operators are used to combine conditional statements.

OperatorDescriptionExample
andReturns True if both conditions are trueTrue and False ? False
orReturns True if one of the conditions is trueTrue or False ? True
notReturns True if the condition is falsenot True ? False

Example:

a = Trueb = Falseprint(a and b)  # Output: Falseprint(a or b)   # Output: Trueprint(not a)    # Output: False

4. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assigns a value to a variablex = 5
+=Adds and assigns to a variablex += 3 ? x = x + 3
-=Subtracts and assignsx -= 2 ? x = x - 2
*=Multiplies and assignsx *= 2 ? x = x * 2
/=Divides and assignsx /= 2 ? x = x / 2
//=Floor division and assignsx //= 2 ? x = x // 2
%=Modulo and assignsx %= 2 ? x = x % 2
**=Exponentiation and assignsx **= 3 ? x = x ** 3

Example:

x = 10x += 5   # x = x + 5 ? 15x -= 3   # x = x - 3 ? 12x *= 2   # x = x * 2 ? 24x /= 4   # x = x / 4 ? 6.0x //= 3  # x = x // 3 ? 2x %= 2   # x = x % 2 ? 0x **= 3  # x = x ** 3 ? 0

5. Membership Operators

These operators are used to test whether a value is found in a sequence (like a list, tuple, or string).

OperatorDescriptionExample
inReturns True if the value is present in the sequence'a' in 'apple' ? True
not inReturns True if the value is not present in the sequence'b' not in 'apple' ? True

Example:

x = [1, 2, 3, 4, 5]print(3 in x)  # Output: Trueprint(6 not in x)  # Output: True

6. Identity Operators

These operators are used to compare the memory location of two objects.

OperatorDescriptionExample
isReturns True if both variables refer to the same objectx is y ? True
is notReturns True if both variables refer to different objectsx is not y ? False

Example:

a = [1, 2, 3]b = [1, 2, 3]c = aprint(a is b)      # Output: False (different objects)print(a is c)      # Output: True (same object)print(a is not b)  # Output: True

7. Bitwise Operators

Bitwise operators work on the binary representations of numbers.

OperatorDescriptionExample
&Bitwise AND5 & 3 ? 1
``Bitwise OR
^Bitwise XOR5 ^ 3 ? 6
~Bitwise NOT~5 ? -6
<<Bitwise left shift5 << 1 ? 10
>>Bitwise right shift5 >> 1 ? 2

Example:

a = 5  # In binary: 101b = 3  # In binary: 011print(a & b)   # Output: 1  (binary: 001)print(a | b)   # Output: 7  (binary: 111)print(a ^ b)   # Output: 6  (binary: 110)print(~a)      # Output: -6 (binary: -110)print(a << 1)  # Output: 10 (binary: 1010)print(a >> 1)  # Output: 2  (binary: 10)

8. Ternary Conditional Operator

The ternary operator allows you to write simple if-else conditions in a single line.

x = 10y = 20# If condition is true, x is assigned; otherwise, y is assignedresult = x if x > y else yprint(result)  # Output: 20

Summary of Python Operators

Operator TypeOperatorsExample
Arithmetic+, -, *, /, //, %, **5 + 3, 5 // 2
Comparison==, !=, >, <, >=, <=5 == 3, 5 > 3
Logicaland, or, notTrue and False
Assignment=, +=, -=, *=, /=, `%=
Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql