You are on page 1of 8

The Tuple Data Type

❖ The tuple data type is almost identical to the list data type

❖ tuples are typed with parentheses and tuples are immutable

❖ Tuples cannot be modified, appended, or removed.

❖ Don’t intend for that sequence of values to change, contents don’t change

❖ Code using tuples slightly faster than code using lists.

Strings and Tuples are IMMUTABLE Objects, can’t Modify

Lists and Dictionaries are MUTABLE objects, can insert, delete (can modify
structure)

The Dictionary Data Type in Python


❖ Like a list, a dictionary is a collection of many values.

❖ Unlike indexes for lists, indexes for dictionaries can use many different data
types, not just integers.

❖ Indexes for dictionaries are called keys, and a key with its associated value
is called a key-value pair

❖ a dictionary is typed with braces, {}

❖ Dictionaries can still use integer values as keys, just like lists use integers for
indexes, but they do not have to start at 0 and can be any number.

Repititions
The keys(), values(), and items() Methods
❖ Three dictionary methods that will return list-like values of the dictionary’s
keys, values, or both keys and values: keys(), values(), and items()

❖ The values returned by these methods are not true lists

❖ They cannot be modified and do not have an append() method.

❖ can also use the multiple assignment trick in a for loop to assign the key
and value to separate variables

Type Conversion on Collection Data Types

>>> ls=[12,34,56]

>>> tuple(ls)

(12, 34, 56)

>>> t=(12, 34, 56)

>>> list(t)

[12, 34, 56]

>>> d={23:4,56:7}

>>> tuple(d)

(56, 23)

>>> list(d)

[56, 23]

#sample program to print repetitions of each letter in a


word
Method 1: Using for loops
n=input('enter the string ')
k=''
for j in n:
count=0
for k in n:
if(k==j):
count=count+1
print(j,'letter is repeated for',count,'times')
Method 2: Using Dictionary
#sample program to print repetitions of each letter in a
n=input('enter the string ')
dic={}
for ch in n:
dic.setdefault(ch,0)
dic[ch]=dic[ch]+1
print(dic)
SET DATA TYPE:

s={12,'iglobal',56.7,'123'}

>>> type(s)

<class 'set'>

>>> s[0]

Traceback (most recent call last):

File "<pyshell#20>", line 1, in <module>

s[0]

TypeError: 'set' object does not support indexing

>>> for i in s:

print(i)

56.7

iglobal

123

12

>>> dir(set)

['__and__', '__class__', '__contains__', '__delattr__', '__dir__',


'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__',
'__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__',
'__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__',
'__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__',
'__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__',
'__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference',
'difference_update', 'discard', 'intersection', 'intersection_update',
'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove',
'symmetric_difference', 'symmetric_difference_update', 'union',
'update']

>>> s={12,56.7,8,12,56.7}

>>> s

{56.7, 8, 12}

>>> s={1,2,3,4}

>>> s1={3,4,5,6}

>>> s.intersection(s1)

{3, 4}

>>> s={1,2,3,4,5,6}

>>> s1={3,4,5}

>>> s1.subset(s)

Traceback (most recent call last):

File "<pyshell#32>", line 1, in <module>

s1.subset(s)

AttributeError: 'set' object has no attribute 'subset'

>>> s1.issubset(s)
True

>>> s.issuperset(s1)

True

>>> s.remove(6)

>>> s

{1, 2, 3, 4, 5}

>>> s={'java','c++','python'}

>>> s1={'are programming languages'}

>>> s.union(s1)

{'java', 'c++', 'are programming languages', 'python'}

>>> ls=[1,2,3,2,5,1]

>>> set(ls)

{1, 2, 3, 5}

>>> list(set(ls))

[1, 2, 3, 5]

You might also like