






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Python in b com ca department year 2024vishal vm professor it is all about simple coding in python
Typology: Schemes and Mind Maps
1 / 11
This page cannot be seen from the preview
Don't miss anything!
PERI Knowledge Park, Mannivakkam, Chennai- 600 048. PG DEPARTMENT OF B COM CA & CS
The unordered data structure stored as key and value pair, access by key value .It is built-in type denoted by dict.
Availability of items in set can be checked using member operator. The set is not subscript able so the items are not accessible individually using index position.
The set is provided with operations like, intersection, union, difference and symmetric.
The constructor frozenset() is used to create the immutable set. No modification or element adding function can be used on frozen set. All the other operation like set operation and comparison are allowed on frozen set.
The Set is mutable (Changeable after creation) unordered data structure. It doesn’t allow duplicate values. It is denoted by ‘Set’. Items in Set are enclosed by curly braces {}.
Set is mutable (Changeable after creation) used to create the immutable set by default Modification or element adding function can No Modification or element adding function be used can be used on frozen set Items in Set are enclosed by curly braces {} The frozen set element are printed with extra enclose parenthesis () preceded with keyword frozen.
The loop statement is used to work on all the items in set sequentially. One item can be assigned to variable in each iteration of loop.
Sets support various set operations like union, intersection, and difference. Union: set1 = {1, 2, 3} set2 = {3, 4, 5} union_set = set1.union(set2) Intersection: intersection_set = set1.intersection(set2) Difference: difference_set = set1.difference(set2) Iterating Over Sets: You can use a for loop to iterate over the elements in a set. for item in my_set: print(item) Set Comprehensions: Like list comprehensions, Python supports set comprehensions for creating sets in a concise manner. squares = {x * x for x in range(1, 6)}
Del(Dictionary[key]) The method delete the entry match the key given.Giving non existing key will prompt an error
Del(D2[4]) Print(D2) #{3:90,2:89}
A dictionary is a collection that is unordered, changeable, and indexed. In Python dictionaries are written with curly brackets, and they have keys and values. Key-value pairs Unordered We can construct or create a dictionary like: X={1:’A’,2:’B’,3:’c’} X=dict([(‘a’,3) (‘b’,4)]X=dict(‘A’=1,’B’ =2) Example:
dict1 = {"brand":"mrcet","model":"college","year":2004} dict {'brand': 'mrcet', 'model': 'college', 'year': 2004} Operations and methods: Creating a Dictionary: You can create a dictionary using curly braces {} and specify key- value pairs separated by colons. Here's an example: my_dict = { "name": "John", "age": 30, "city": "New York" } Accessing Values: You can access values in a dictionary by referring to their keys. For example: name = my_dict["name"] Adding Key-Value Pairs: To add a new key-value pair to the dictionary, simply assign a value to a new key:
The in-built methods of set are used to create, insert, remove and delete the set and set items. Method Description Example Set() Create empty set S1=set() Set(String) (^) Create set by splitting characters in given string as set item. Eliminates the repeated characters. S1=set(‘samples’) Print(S1) #{‘s’,’a’,’m’,’p’,’l’,’e’} len(set) Return number of items in set. S1={‘m’,’a’,’p’,’e’,’s’,’i’} print (len(set))
Set.add(Item) Adds the given item into the set S1=set() S1.add(2) S1.add(3) Print(S1) #{2,3} Set.copy() Create a copy of set. S1={2,3,5} S2=S1.copy() Print(S2) #{2,3,5} Set.update(items) Updates the existing set. The item can be string, list or tuple. The restrictions on update function :The empty set cannot be updated. The item cannot be numeric literal.
S1.update(‘a’) S2={4,3,2,5} S1. update(S2) Print(“Update:”S1) #Update:{1,2,,3,4,5,’a’} Set.remove(item) Remove the given item from set. It will prompt error when the item not in set.
S1.remove(2) Print(S1) #{3,5} Set.discard(item) (^) Remove the specified item if it exist.It will not prompt error if the item does not exist. S1={1,2,3,4,5,’a’} S1.discard(1) S1.discard(1) Print(“Discard Exist:”,S1 ) #{2,3,4,5,’a’} Set.pop() Removes the first element from the set S1={1,2,3,4,5} S1.pop() Print(“After Pop:”S1) #After Pop: {2,3,4,5} Set.clear() It removes all items from set and make it as empty set.
S1.clear() Print(S1) #set() del(Set) Removes the set given and the memory allocated for that also del(S1)