Python Operators Examples | Live Code Programming

Python Operators Examples

Operators are symbols that perform operations on variables and values. Below are examples of common Python operators with outputs.

Operators वे चिन्ह होते हैं जो variables और values पर operations करते हैं। नीचे सामान्य Python operators के उदाहरण और आउटपुट दिए गए हैं।

Example 1: Arithmetic Operators
a = 10
b = 3
print('Addition:', a + b)
print('Subtraction:', a - b)
print('Multiplication:', a * b)
print('Division:', a / b)
print('Floor Division:', a // b)
print('Modulus:', a % b)
print('Exponent:', a ** b)

Output:

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Floor Division: 3
Modulus: 1
Exponent: 1000

Performs basic arithmetic operations.

मूल arithmetic operations करता है।

Example 2: Comparison Operators
x = 5
y = 10
print('x == y:', x == y)
print('x != y:', x != y)
print('x > y:', x > y)
print('x < y:', x < y)
print('x >= 5:', x >= 5)
print('y <= 10:', y <= 10)

Output:

x == y: False
x != y: True
x > y: False
x < y: True
x >= 5: True
y <= 10: True

Checks relationship between values.

मूल्यों के बीच संबंध जांचता है।

Example 3: Logical Operators
p = True
q = False
print('p and q:', p and q)
print('p or q:', p or q)
print('not p:', not p)

Output:

p and q: False
p or q: True
not p: False

Performs logical AND, OR, NOT operations.

Logical AND, OR, NOT ऑपरेशन्स करता है।

Example 4: Assignment Operators
a = 5
a += 3
print('a after += 3:', a)
a *= 2
print('a after *= 2:', a)

Output:

a after += 3: 8
a after *= 2: 16

Shorthand for updating variable values.

variable के मान को जल्दी update करने के लिए।

Example 5: Identity Operators
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print('x is y:', x is y)
print('x is z:', x is z)

Output:

x is y: True
x is z: False

Checks if two variables point to the same object.

जांचता है कि दो variables एक ही object को point करते हैं।

Example 6: Membership Operators
lst = [1, 2, 3, 4]
print('2 in list:', 2 in lst)
print('5 not in list:', 5 not in lst)

Output:

2 in list: True
5 not in list: True

Checks if a value is present in a sequence.

जांचता है कि कोई मान sequence में है या नहीं।

Example 7: Bitwise Operators
a = 6  # 110 in binary
b = 3  # 011 in binary
print('a & b:', a & b)
print('a | b:', a | b)
print('a ^ b:', a ^ b)
print('~a:', ~a)
print('a << 1:', a << 1)
print('a >> 1:', a >> 1)

Output:

a & b: 2
a | b: 7
a ^ b: 5
~a: -7
a << 1: 12
a >> 1: 3

Performs bitwise AND, OR, XOR, NOT, shift left/right.

bitwise AND, OR, XOR, NOT, shift operations करता है।

Example 8: Ternary Operator (Conditional Expression)
age = 18
status = 'Adult' if age >= 18 else 'Minor'
print(status)

Output:

Adult

Simplified if-else expression in one line.

एक लाइन में सरल if-else expression।

Example 9: Floor Division Operator
print('7 // 3 =', 7 // 3)
print('-7 // 3 =', -7 // 3)

Output:

7 // 3 = 2
-7 // 3 = -3

Divides and rounds down to nearest integer.

भाग देकर सबसे नज़दीकी नीचे के integer तक round करता है।

Example 10: Exponentiation Operator
print('2 ** 3 =', 2 ** 3)
print('9 ** 0.5 =', 9 ** 0.5)

Output:

2 ** 3 = 8
9 ** 0.5 = 3.0

Calculates power and roots using exponent.

घातांक के द्वारा power और roots निकालता है।