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

2 Data Types

The document provides an introduction to Python, focusing on data types, particularly the differences between lists and tuples. It highlights that lists are mutable and have more functionalities, while tuples are immutable and more memory efficient. Additionally, it covers bytes and bytearray for binary data manipulation, explaining their mutability and usage in Python.

Uploaded by

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

2 Data Types

The document provides an introduction to Python, focusing on data types, particularly the differences between lists and tuples. It highlights that lists are mutable and have more functionalities, while tuples are immutable and more memory efficient. Additionally, it covers bytes and bytearray for binary data manipulation, explaining their mutability and usage in Python.

Uploaded by

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

Introduction to Python

Noble Xavier
Variable Name- Rules
DATA TYPES
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.
Range Examples
Range from 0 to 5 result as list

print (list(range (5)))

Range 10 to 20 with increment 2 result as a list

print (list(range (10,20,2)))

Range 10 to 20 with increment 2 result as a tuple

print (tuple (range (10,20,2)))


bytes, byte array, memory view
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, byte array, memory view
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, byte array, memory view
Bytes

• It is immutable - Item assignment not possible

example -1 – This is not possible since immutable

b1[1] =25
bytes, byte array, memory view
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)
OPERATORS
Arithmetic Operator
Assignment Operator
Comparison True / False

You might also like