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

Python_Basic_Functions_Summary

The document outlines basic functions for Python data structures: lists, tuples, sets, and dictionaries. It provides examples of common operations such as adding, removing, and counting elements in these structures. Each section details specific methods available for manipulation and retrieval of data within these types.

Uploaded by

Shruti Shevade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python_Basic_Functions_Summary

The document outlines basic functions for Python data structures: lists, tuples, sets, and dictionaries. It provides examples of common operations such as adding, removing, and counting elements in these structures. Each section details specific methods available for manipulation and retrieval of data within these types.

Uploaded by

Shruti Shevade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Basic Functions in Python (List, Tuple, Set, Dictionary)

1. List Functions:
- append(): Adds an item at the end.
my_list = [1, 2]; my_list.append(3) # [1, 2, 3]
- insert(): Inserts item at given index.
my_list = [1, 3]; my_list.insert(1, 2) # [1, 2, 3]
- remove(): Removes first matching item.
my_list = [1, 2, 3]; my_list.remove(2) # [1, 3]
- pop(): Removes and returns last item.
my_list = [1, 2, 3]; my_list.pop() # 3
- sort(): Sorts list in ascending order.
my_list = [3, 1, 2]; my_list.sort() # [1, 2, 3]
- reverse(): Reverses the list.
my_list = [1, 2, 3]; my_list.reverse() # [3, 2, 1]
- clear(): Removes all elements.
my_list = [1, 2]; my_list.clear() # []
- count(): Counts item occurrences.
my_list = [1, 2, 2]; my_list.count(2) # 2

2. Tuple Functions:
- count(): Counts occurrences of value.
my_tuple = (1, 2, 2); my_tuple.count(2) # 2
- index(): Returns index of value.
my_tuple = (1, 2, 3); my_tuple.index(2) # 1

3. Set Functions:
- add(): Adds an element.
my_set = {1, 2}; my_set.add(3) # {1, 2, 3}
- remove(): Removes item or error.
my_set = {1, 2}; my_set.remove(2) # {1}
- discard(): Removes if exists, no error.
my_set = {1, 2}; my_set.discard(3) # {1, 2}
- clear(): Empties the set.
my_set = {1}; my_set.clear() # set()
- union(): Combines sets.
{1, 2}.union({2, 3}) # {1, 2, 3}
- intersection(): Common elements.
{1, 2}.intersection({2, 3}) # {2}
- difference(): Items in A not B.
{1, 2}.difference({2, 3}) # {1}

4. Dictionary Functions:
- get(): Returns value or None.
{"a": 1}.get("a") # 1
- keys(): Returns all keys.
list({"a": 1}.keys()) # ['a']
- values(): Returns all values.
list({"a": 1}.values()) # [1]
- items(): Returns key-value pairs.
list({"a": 1}.items()) # [('a', 1)]
- update(): Adds/updates key-value.
d = {"a": 1}; d.update({"b": 2}) # {'a': 1, 'b': 2}
- pop(): Removes key, returns value.
d = {"a": 1, "b": 2}; d.pop("b") # 2

You might also like