Python Data Types (List, Tuple, Set and Dictionary) and Methods
1. List
List is collection of same or different data that are enclosed by [], separated by comma(,). List is Data Structure in Python.
Creating a Python List
For creating a list you need bunch of values that should be enclosed in Square bracket [] and separated by comma
alpha = [‘a’,’b’,’c’,’d’,’e’]
num = [1,2,3,4,5]
mixed = [1 ,’naren’,3.3]
list_in=[0,[1.1,1.2],2,[3.3,3.4]]
Accessing List
Accessing a list mean how you gonna to use or access a set of value that may be all list or individual element in list.
print(alpha)
print(num)
print(mixed)
print(list_in)
print(type(alpha))#accessing particular element
print(alpha[2])
print(alpha[:])
print(alpha[1:4])
print(alpha[:3])
print(alpha[2:])
List Methods
Adding in List
You can add an element on list using append, extend and (‘+’)methods
alpha+=[‘f’,’g’]
print(alpha)num+=[6,7]
print(num)alpha.append(‘h’)
num.append(8)alpha.extend([‘i’])
num.extend([9])print(alpha)
print(num)
Altering in List
Altering a particular element or set of element in List.
list_in[0]=’a’
print(list_in)list_in[4:] = [0.415,3.1415926]
print(list_in)list_in[3:4] = [[3.5,3.6],2.7182818]
print(list_in)
# Or you could completely re-alter all values
Counting values in List
This method help how much element present on particular List.
print(“How much a is present “,alpha.count(‘a’))
print(“How much z is present “,alpha.count(‘z’))
Copying in List
You can copy all element of list to another variable name.
letters = alpha.copy()
print(“Copying Values”,letters)
Indexing in List
To find on which place that element is presented in List.
print(“In which element a is present “,alpha.index(‘a’))
print(“In which element d is present “,alpha.index(‘d’))
Inserting in List
To insert or change the element on list and you need to mention on which element to be placed.
alpha.insert(9,’j’)
print(“Newly inserted value is “,alpha)
Length of List
To find the length of List.
print(“Length of alpha is “,len(alpha))
Remove elements in List
You can remove the element on list using remove and pop method.
Remove an element by directly mentioning value Pop will remove last element
alpha.remove(‘j’)
print(“J is removed “,alpha)#Last element remove
alpha.pop()
print(“Last element removed “,alpha)
Reverse elements in List
To reverse all element in List.
alpha.reverse()
print(“Reversed elements are “,alpha)
alpha.reverse()
Clear elements in List
To clear all element in List.
print(list_in)
list_in.clear()
print(list_in)
Sort in List
Sort all element in List.
unsorted = [“m”,”n”,”z”,”y”,”q”,”a”]
print(“Before sorting “,unsorted)
unsorted.sort()
print(“After sorting “,unsorted)
Concatenation two List
Adding of two Lists.
result=alpha+num
print("The concatenation of two list is ",result)
Looping in List
Looping all elements in List.
for value in alpha:
print(value)
2. Tuple
Tuples are collection are same or same or different data that are enclosed in parenthesis ( ) separated by comma , Tuple is Data structure in Python
Creating Tuple
For creating a list you need set of values that should be enclosed in parenthesis () and separated by comma ,
alpha = (“a”,”b”,”c”,”d”)
num = (1,2,3,4,5)
#List in tuples
lint = [1,(2,3),4]
Accessing Tuples
Accessing a tuple mean how you gonna to use or access a set of value that may be all tuple elements or individual element on tuple but when you access particular element the result will be in List.
print(alpha)
print(num)
print(lint)
print(type(alpha))print(alpha[2:5])
print(alpha[3:])
print(alpha[:4])
print(alpha[:])
print(alpha[:-2])
Tuple Methods
Adding elements in Tuple
You can add tuple element using ‘+’ operator but basically tuple is unmutable which means once you set a value you cant alter back
alpha += (“e”,”f”)
print(alpha)
Counting in Tuple
This method help how much element present on particular Tuple.
print(alpha.count(‘a’))
print(alpha.count(‘z’))
print(num.count(5))
Copying in Tuple
This is not core method you can use variable to access that value.
#This is not core python tuple method
b = alpha
print(b)
Indexing in Tuple
To find on which place that element is presented in Tuple.
print(alpha.index(“c”))
Length in Tuple
To find the length of Tuple.
print(len(alpha))
Min and Max
To find minimum and maximum value on Tuple.
print("Maximum value in tuple is ",max(num))
print("Minimum value in tuple is ",min(num))
Sum in Tuple
To find sum value on Tuple.
print(sum(num))
Sort in Tuple
To sort all element in tuple you need to use another method to sort the tuple element.
abc = (“m”,”p”,”n”,”h”,”k”,”s”,”y”)
print(sorted(abc))
Looping in Tuple
Looping all elements in Tuple.
for value in alpha:
print(value)
3. Sets
Set is collection of same different data that are enclosed in braces {} separated in comma , Set is Data structure in Python.
Creating Set
For creating a set you need bunch of values that should be enclosed in Braces {} and separated by comma ,
alpha = {‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’}
number={1,2,3,4,5}
mixed = {5.4,”hello”,(3,2,1)}
Accessing a Set
Accessing a set mean how you gonna to use or access all element in set.
print(alpha)
print(number)
print(mixed)
print(type(alpha))
Set Methods
Add in Set
To add an element in set.
alpha.add(‘i’)
print(alpha)
Sorted in Set
To sort the set element because all element are unsorted and the result will be in tuple.
print(sorted(alpha))
Remove in Set
To remove an element in set you can use remove method or discard method.
alpha.remove(‘i’)
print(sorted(alpha))alpha.discard(‘h’)
print(sorted(alpha))
Update in Set
To update an element in set.
alpha.update([‘g’,’h’,’i’])
print(sorted(alpha))#no Duplication
min and max
To find minimum and maximum value in set.
print(min(alpha))
print(max(alpha))print(min(number))
print(max(number))
Pop in Set
To remove an last element in set but it may remove any element because it set are unsorted.
mixed.pop()
print(mixed)
Clear in Set
To clear all element in set.
print(“Before Clear “,mixed)
mixed.clear()
print(“After Clear “,mixed)
Looping in Set
To loop all element in set.
for value in alpha:
print(value)
Creating some variable for math methods
set1={1,2,3,4,5}
set2={4,5,6,7,8}
set3={1,2,3}
set4={10,20,30}alpha1 = {‘a’,’b’,’c’,’d’,’e’}
alpha2 = {‘d’,’e’,’f’,’g’,’h’}
alpha3 = {‘a’,’b’,’c’}
alpha4 = {‘x’,’y’,’z’}
Union Set
To print all element on union elements.
result = set1.union(set2)
print(“Union Result is “,result)result1 = alpha1 | alpha2
print(“Union Result is “,sorted(result1))
Intersection Set
To print an element that present on both intersection elements.
result = set1.intersection(set2)
print(“Intersection Result is “,result)result1 = alpha1 & alpha2
print(“Intersection Result is “,sorted(result1))
Difference Set
To show what difference on comparing to another set elements.
result = set1.difference(set2)
print(“Differences Result is “,result)result1 = alpha1 — alpha2
print(“Differences Result is “,sorted(result1))result2 = set2.difference(set1)
print(“Another Differences side Result “,result2)result3 = alpha2 — alpha1
print(“Another Differences side Result “,sorted(result3))
Symmetric Difference Set
It will print unmatched element on both set.
result = set1.symmetric_difference(set2)
print(“First Sym-Differences Result “,result)result1 = alpha1 ^ alpha2
print(“Second Sym-Differences Result “,sorted(result1))
Subset
To find father element on set means all value of set 3 will be in some elements set 1. (Result on boolean)
result = set3.issubset(set1)
print(“Set 3 is child setl is parent “,result)result1 = set2.issubset(set1)
print(“Set 2 is child setl is parent “,result1)result2 = alpha3 <= alpha1
print(“alpha 3 is child alphal is parent “,result2)
Superset
To find children method.(Result on boolean)
result = set1.issuperset(set3)
print(“setl is parent Set 3 is child “,result)result1 = set2.issuperset(set1)
print(“Set 2 is child setl is parent “,result1)result2 = alpha1 >= alpha3
print(“alphal is parent alpha 3 is child “,result2)
Disjoint
To find that no difference element on both set.(Result on boolean)
result = set3.isdisjoint(set1)
print(“The is no matching values”,result)result1 = set4.isdisjoint(set1)
print(“The is no matching values”,result1)result2 = alpha3.isdisjoint(alpha1)
print(“The is no matching values”,result2)result3 = alpha4.isdisjoint(alpha1)
print(“The is no matching values”,result3)
4. Dictionary
Dictionary are enclosed in braces {} not as single element, present as key value pair {“key”:”value”} dictionary is data structure in python
Creating dictionary
For creating a dictionary you need bunch of values that should be enclosed in Braces {} and separated by comma , and it was represented by Key value pair {key:value}
num_alpha={1:’A’,2:’B’,3:’C’,4:’D’,5:’E’}
Accessing the dictionary
Accessing a dictionary mean how you gonna to use or access a set of key value pair that may be all element or individual element in dictionary.
print(num_alpha)
print(num_alpha[2])
print(num_alpha.keys())
print(num_alpha.values())
print(num_alpha.items())
print(type(num_alpha))
Dictionary Methods
Adding in dictionary
To add an element in dictionary you need to insert key and value to be placed on the particular key element.
num_alpha[6]=’G’
print(num_alpha)num_alpha[6]=’F’
print(num_alpha)
Copying in dictionary
To copy an element in dictionary.
dupli=num_alpha.copy()
print(“dupli result is”,dupli)
Removing in dictionary
To remove an element in dictionary directly del method, pop, popitem and clear methods
del dupli[6]
print(“dupli result is”,dupli)dupli.pop(4)
print(“dupli result is”,dupli)dupli.popitem()
print(“dupli result is”,dupli)dupli.clear()
print(“dupli result is”,dupli)
Update in dictionary
To update an element in dictionary you need insert both key and new value.
print(num_alpha)
num_alpha.update({6:”Z”})
print(num_alpha)
Length of dictionary
To find the length in dictionary.
print(“The length of dicionary is “,len(num_alpha))
Get dictionary
To get a value you ‘get’ method.
one=num_alpha.get(1)
print(one)
Set default in Dictionary
One thing while using set default you can insert new value on ‘setdefault’ method but if the key element already have a value the value cant be changed.
res1=num_alpha.setdefault(3)
print(res1)res2=num_alpha.setdefault(5,’y’)
print(res2)res3=num_alpha.setdefault(7,’Y’)
print(res3)print(num_alpha)
From key to iter value
You can use ‘fromkey’ method to combine both element to represent in dictionary element.
num={5,4,3,2,1}
val1=’Value’num1=dict.fromkeys(num,val1)
print(num1)
Looping in dict
To loop an particular element or all dictionary elements.
for key,value in num_alpha.items():
print(key,’ has value of ‘,value)for val in num_alpha.keys():
print(val)for a in num_alpha.values():
print(a)
Follow for more topics :-)
Follow us on
Instagram:- https://www.instagram.com/Coder_033/
Github:- https://github.com/Coder033
Skillshare:- https://www.skillshare.com/profile/Coder033/26410885 Youtube:-https://www.youtube.com/channel/UCjTUcwOms1NJDo2UiIvGfyg