Python Codes
Python Codes
# Arithmetic operations
addition_result = a + b
subtraction_result = a - b
multiplication_result = a * b
division_result = a / b
modulus_result = a % b
# Display results
print("Addition:", addition_result)
print("Subtraction:", subtraction_result)
print("Multiplication:", multiplication_result)
print("Division:", division_result)
print("Modulus:", modulus_result)
[5:54 PM, 1/7/2024] Ritika Patil: a = int(input("Enter the value of 'a': "))
b = int(input("Enter the value of 'b': "))
a -= b
print("After a -= b, a =", a)
a *= b
print("After a *= b, a =", a)
a /= b
print("After a /= b, a =", a)
a %= b
print("After a %= b, a =", a)
Assignment operators
x = fruits.count("cherry")
print(x)
cars.sort()
print(cars)
[10:44 PM, 1/7/2024] Ritika Patil: ['Audi', 'BMW', 'Ford', 'Volvo']
[10:47 PM, 1/7/2024] Ritika Patil: cars = ['BMW','Ferrari','Volks','MG
Hector','BMW','bmw','Volks']
cars.reverse()
cars
# Comparing lists
print "Comparison of list2 with list1 : ",
print cmp(list2, list1)
# Positive indexing
print("Positive indexing:")
print("First character:", sample_string[0]) # Output: P
print("Character at index 7:", sample_string[7]) # Output: i
print("Last character:", sample_string[len(sample_string) - 1]) # Output: !
# Negative indexing
print("\nNegative indexing:")
print("Last character using negative index:", sample_string[-1]) # Output: !
print("Character at index -6:", sample_string[-6]) # Output: z
print("First character using negative index:", sample_string[-len(sample_string)])
# Output: P
# Bitwise OR
d = a | b # Result in binary: 0011 1101 (61 in decimal)
print("Bitwise OR:", d) # Output: 61
# Bitwise XOR
e = a ^ b # Result in binary: 0011 0001 (49 in decimal)
print("Bitwise XOR:", e) # Output: 49
# Bitwise NOT
f = ~a # Result: -61 (bitwise inversion of 60)
print("Bitwise NOT:", f) # Output: -61
# Left Shift
g = a << 2 # Shift left by 2 bits: 1111 0000 (240 in decimal)
print("Left Shift:", g) # Output: 240
# Right Shift
h = a >> 2 # Shift right by 2 bits: 0000 1111 (15 in decimal)
print("Right Shift:", h) # Output: 15
[7:42 AM, 1/8/2024] Ritika Patil: p = True
q = False
# Logical AND
print("Logical AND:", p and q) # Output: False
# Logical OR
print("Logical OR:", p or q) # Output: True
# Logical NOT
print("Logical NOT:", not p) # Output: False