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

2 Data Types Operators

Uploaded by

Aman Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

2 Data Types Operators

Uploaded by

Aman Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Introduction to Python

Noble Xavier
Difference Between List and Tuple in Python
Difference Between List and Tuple in Python

Operations

Although there are many operations similar to both lists and tuples, lists have additional
functionalities that are not available with tuples. These are insert and pop operations, and
sorting and removing elements in the list.

Size
In Python, tuples are allocated large blocks of memory with lower overhead, since they are
immutable; whereas for lists, small memory blocks are allocated. Between the two, tuples
have smaller memory. This helps in making tuples faster than lists when there are a large
number of elements.
Difference Between List and Tuple in Python
Length
Lengths differ in the two data structures. Tuples have a fixed length, while lists have
variable lengths. Thus, the size of created lists can be changed, but that is not the case for
tuples.

Debugging
When it comes to debugging, in lists vs tuples, tuples are easier to debug for large projects
due to its immutability. So, if there is a smaller project or a lesser amount of data, it is
better to use lists. This is because lists can be changed, while tuples cannot, making tuples
easier to track.
bytes, bytearray, memoryview
It can store Values from 0 to 255

bytes and bytearray are used for manipulating binary data. The memoryview uses the
buffer protocol to access the memory of other binary objects without needing to make a
copy.

Bytes objects are immutable sequences of single bytes. We should use them only when
working with ASCII compatible data.

The syntax for bytes literals is same as string literals, except that a 'b' prefix is added.

bytearray objects are always created by calling the constructor bytearray(). These are
mutable objects.
bytes, bytearray, memoryview
Bytes
• It can store Values from 0 to 255

example -1

b1= bytes ([10,20,30,255])


print (b1) # Print Bytes
for i in b1:
print (i)

example -2- Error – since value more than 255

b1= bytes ([10,20,30,300])


print (b1) # Print Bytes
bytes, bytearray, memoryview
Bytes

• It is immutable - Item assignment not possible

example -1 – This is not possible since immutable

b1[1] =25
bytes, bytearray, memoryview
Bytearray

• It is mutable - Item assignment possible

example -1

b1= bytearray([10,20,30,255])
print (b1)
b1[1]=100 # update first element with 100
for i in b1:
print (i)
Arithmetic Operator
Assignment Operator
Comparison True / False
Logical Operator
Bitwise -Operator
Identity Operator
x = ["apple", "banana"] print(x is z)
y = ["apple", "banana"] print(x is y)
z=x print(x == y)
a = 50 a = 500
b = 50 b = 500
c= a c= a
print (id(a)) print (id(a))
print (id(b)) print (id(b))
print (id(c)) print (id(c))
print (a is b) print (a is b)
print (c is a) print (c is a)
print (c is b) print (c is b)
a = 256 when their values are outside the range
b = 256 of common integers (ranging from -5 to 256),
print (a is b) they’re stored at separate memory addresses.
print ( id(a))
print ( id(b))
a = 257
b = 257
print (a is b)
print ( id(a))
print ( id(b))
Append Copy- Different
Location
a = [1, 2, 3]
b=a a = [1, 2, 3]
print (a) b = a.copy()
print (id(a)) print (a)
print (b) print (id(a))
print (id(b)) print (b)
print () print (id(b))
print (a is b) print ()
print ('*****Append****') print (a is b)
print () print ('*****Append****')
a.append(4) print ()
print (a) a.append(4)
print (id(a)) print (a)
print (b) print (id(a))
print (id(b)) print (b)
print (a is b) print (id(b))
print (a is b)
Member ship
 A = [10,15,20]
 print (15 in A)

A = [10,15,20]
print (15 in A)

You might also like