top of page

Python Data Types: continued

bytes

It represents  a group of byte numbers just like an array.

Note 1: in bytes data type every values should be in the range of 0 to 256.

bytes
P15.png

Ex: x=[10,20,30]

b=bytes(x)

P16.png

To access value at a particular index:

P17.png

To access all values:

P18.png

Note:2: bytes data types is immutable. Once a object is created we can not perform any change in the content if we are trying to do so with that changes a new object is created.

P20.png

bytearray

'bytes' and 'bytearray' both are same except the difference that 'bytes' data type is immutable while 'bytearray' is mutable.

bytearray
P21.png

list

If we want to represent a group of values as a single entity where insertion order is preserved and duplicates are allowed then we should go for list. By convention, always represent list values in square brackets [].

list
P22.png
P23.png
  • Insertion order is preserved.

  • Hetrogenerous objects are allowed.

  • Duplicates are allowed.

  • 'list' object is growable in nature (based on requirement we can increase or decrease size of list).

​

access list values: we can access list values by using index or slice operator.

Example: -

l=[]

Type(l)------>list

l.append(10)

l.append(20)

l.append(30)

l.append(10)

Print(l)------------>[10,20,30,10]

l.append(‘knowcubs’)

l.append(None)

Print(l)----------> [10,20,30,10,’knowcubs’,None]

L[0]--->10, l[-1]--->none

L[1:5]------->[20,30,40,’knowcubs’]

l.remove(10)----->[20,30,40,’knowcubs’,none]

 

Note:- in python * operator is applicable but one element should be number and another should be letter.

Example: -

s=[10,’knowcubs’]

S1=s*2

Print(s1)----->[10,’knowcubs’, 10,’knowcubs’]

​

tuple

'list' and 'tuple' is almost same, but 'list' is mutable, and 'tuple' is immutable. 'tuple' values are always represented in bracket ().

Example: t= (10,20,30,’ knowcubs’, True)

tuple
P24.png

t [0] =100----->error tuple doesn't support item assignment.

P25.png

range

  • 'range' data type represents a sequence of values.

  • 'range' is only applicable for numbers and in that also not for floating values.

  • 'range' data type is immutable.

 

Form 1: r=range (n); it  represents values from 0 to n-1

​

Form 2: r=range (begin, end); starts from begin and ends at end-1

​

Form 3: r=range (begin, end, steps); starts from begin and ends at end-1, steps defines how many values are skipped after each value. By default step values is always 1.

​

set

  • It is group of individual objects.

  • 'set' data type is represented by {}.

  • 'set' doesn’t allow duplicates and insertion order.

  • Indexing, slicing etc are not applicable in set.

  • 'set' is mutable.

​

How to create empty set:

set s=set ()

 

Example:

s= {10,20,30,10,20,30}

s={10,20,30} ------it removes the duplicates.

s[0] ---->error object does not support indexing

s[1:2]---->set object is not subscriptable

s.add(‘knowscubs’) ----> {a=10,20,30,’knowscubs’}

s.remove(10) ----->{20,30,10,20,30,’knowcubs’}

​

Frozen set

'frozenset' is same as 'set' but in 'frozenset' elements can not be changed after creation. So add and remove is not applicable.

Example:

s={10,20,30,40}

Fs=frozenset(s)

Fs------>frozenset{(10,20,30,40)}

​

dict

  • 'dict' means dictionary which represents a group of key value pairs.

  • Key must be unique, but value may be duplicates.

  • Key and value both can be heterogeneous.

          

How to create empty dictionary

{} ==> empty dictionary

 

Example

d={10:’knowcubs’, 20:’geeks’}

range
set
frozenset
dict
P26.png

add values in dict

d[30]=’tutorialpoint’

P27.png

If we are trying to add an entry with duplicate key, old values will be replaced by new one.

d[30]=’hackerrank’

P28.png

None

None is like null and void in other languages.

Example:

def f1():print(‘Hello don’t be like None’)

none
P29.png

For any Query/Suggestion, do let us know in the comment section.

bottom of page