0% found this document useful (0 votes)
10 views

Python Codes

The document contains code snippets demonstrating various Python operators including arithmetic, relational, assignment, logical, bitwise, and slice operators. It takes user input, performs operations on variables, and prints output to demonstrate the use of each operator type. List methods like append(), insert(), remove(), sort() are also shown.

Uploaded by

ritikavizpatil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python Codes

The document contains code snippets demonstrating various Python operators including arithmetic, relational, assignment, logical, bitwise, and slice operators. It takes user input, performs operations on variables, and prints output to demonstrate the use of each operator type. List methods like append(), insert(), remove(), sort() are also shown.

Uploaded by

ritikavizpatil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

[5:42 PM, 1/7/2024] Ritika Patil: a = int(input("Enter the value of 'a': "))

b = int(input("Enter the value of 'b': "))

# 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': "))

# Use relation operators


print("a == b:", a == b)
print("a != b:", a != b)
print("a < b:", a < b)
print("a > b:", a > b)
print("a <= b:", a <= b)
print("a >= b:", a >= b)
[5:54 PM, 1/7/2024] Ritika Patil: relational operators
[5:54 PM, 1/7/2024] Ritika Patil: # Get user input
a = int(input("Enter the initial value of 'a': "))
b = int(input("Enter the value to be assigned to 'b': "))

# Use assignment operators


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)

a %= b
print("After a %= b, a =", a)
Assignment operators

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]


tinylist = [123, 'john']
print (list)

[9:47 PM, 1/7/2024] Ritika Patil: #append() and len()


lang = ['C', 'C++', 'JAVA']
print("Number of items in the list:",len(lang))
lang.append("PYTHON")
print(lang)
[9:58 PM, 1/7/2024] Ritika Patil: #extend()
sportscar = ['ferrari']
cars = ['Ford', 'BMW', 'Volvo']
sportscar.extend(cars)
print(sportscar)

[10:20 PM, 1/7/2024] Ritika Patil: #insert()


cars = ['BMW','Ferrari','Volks','MG Hector']
cars.insert(1,"Tata Harrior")
print(cars)

[10:26 PM, 1/7/2024] Ritika Patil: #remove()


cars = ['BMW','Ferrari','Volks','Volks','MG Hector']
cars.remove("Volks")
print(cars)
cars = ['BMW','Ferrari','Volks','MG Hector']
cars.pop() #without index removes last item
print(cars)
cars.pop(1) #pass index/position
print(cars)

[10:31 PM, 1/7/2024] Ritika Patil: #index()


cars = ['BMW','Ferrari','Volks','MG Hector']
print("Index of specified car:",cars.index("BMW"))
print(cars)
for i in range(len(cars)):
print(i,cars[i])

[10:38 PM, 1/7/2024] Ritika Patil: fruits = ["apple", "banana", "cherry","cherry"]

x = fruits.count("cherry")

print(x)

[10:44 PM, 1/7/2024] Ritika Patil: cars = ['Ford', 'BMW', 'Volvo','Audi']

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

[10:50 PM, 1/7/2024] Ritika Patil: #copy()


lang = ['C', 'C++', 'JAVA']
print(lang)
x = lang.copy()
print(x)
x=lang
print(x)

[10:50 PM, 1/7/2024] Ritika Patil: #clear()


lang = ['C', 'C++', 'JAVA']
print(lang)
lang.clear()
print(lang)
[11:05 PM, 1/7/2024] Ritika Patil: list1 = [1, 2, 4, 3]
list2 = [1, 2, 5, 8]
list3 = [1, 2, 5, 8, 10]
list4 = [1, 2, 4, 3]
list5 = [7, 2, 5, 8]

# Comparing lists
print "Comparison of list2 with list1 : ",
print cmp(list2, list1)

# prints -1, because list3 has larger size than list2


print "Comparison of list2 with list3(larger size) : ",
print cmp(list2, list3)

# prints 0 as list1 and list4 are equal


print "Comparison of list4 with list1(equal) : ",
print cmp(list4, list1)
[11:16 PM, 1/7/2024] Ritika Patil: print("Minimum of 4,12,43.3,19 and 100 is :
",end="")
print (min( 4,12,43.3,19,100 ) )
[11:42 PM, 1/7/2024] Ritika Patil: sample_string = "Hello, World!" #Slice operator

# Accessing a single character at a specific index


char_at_index = sample_string[7]
print("Character at index 7:", char_at_index) # Output: W

# Slicing from a specific index to the end of the string


slice_to_end = sample_string[7:]
print("Slice from index 7 to end:", slice_to_end) # Output: World!

# Slicing from the beginning to a specific index


slice_from_start = sample_string[:5]
print("Slice from start to index 5:", slice_from_start) # Output: Hello

# Slicing within a specific range of indices


slice_within_range = sample_string[2:9]
print("Slice from index 2 to 8:", slice_within_range) # Output: llo, Wo

# Using negative indices for slicing from the end


slice_from_end = sample_string[-6:]
print("Slic…
[11:44 PM, 1/7/2024] Ritika Patil: sample_string = "Python is amazing!"

# 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

[6:37 AM, 1/8/2024] Ritika Patil: list1 = [111, 22]


list2 = [99, 88]
print(sorted(list1))
print(sorted(list2))
[7:42 AM, 1/8/2024] Ritika Patil: # Bitwise AND
a = 60 # 60 in binary: 0011 1100
b = 13 # 13 in binary: 0000 1101
c = a & b # Result in binary: 0000 1100 (12 in decimal)
print("Bitwise AND:", c) # Output: 12

# 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

You might also like