From 7e97dcf70c433e969b2b8fa9337855ab5875afaa Mon Sep 17 00:00:00 2001 From: Sakshi Arora <59931342+iam-sakshi@users.noreply.github.com> Date: Mon, 29 Nov 2021 00:32:03 +0530 Subject: [PATCH 1/3] Create List.md --- Python/List_Methods/List.md | 225 ++++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 Python/List_Methods/List.md diff --git a/Python/List_Methods/List.md b/Python/List_Methods/List.md new file mode 100644 index 0000000000..61ad3385c4 --- /dev/null +++ b/Python/List_Methods/List.md @@ -0,0 +1,225 @@ +# List in Python + +It is kind of Dynamic Array , that means +- It is mutable collection of data +- it is resizeable. +- it can contain elements of different types. +- an ordered group of sequence enclosed inside square bracket and seprated by comma (,) + +## Declare Empty list +``` python + list1 = [] +``` +``` python + list2 = list ( ) +``` +In 1st list1(variable name) is initialised to empty list using square brackets. +In 2nd , list2(variable name) is initialised to empty list using list function. + +## Initialising a list + +### Initialising a list with similar Data + +``` python +l1 = ["a","b","c"] +``` +### Initialising a list with mixed Data +``` python +l2 = [1,2.2,"abc",4] +``` +## Finding Length of the List + +``` python +l1 = ["a","b",22,9.0] +print ( len ( l1 ) ) + +``` +## Concatenation of two list + +``` python +l1 = [1,2.2,"abc",4] +l2 = ["a","b",22,9.0] +l = l1 + l2 +print (l) + +# Output : [1,2.2,"abc",4,"a","b",22,9.0] + +``` +## Membership Operator in List + +- in : returns true, if the value present in list. +- not in : returns true , if the value is not present in the list. + +``` python +l1 = [1,2.2,"abc",4] +if 4 in l1 : + print ("Found 4 ") +else: + print ("not found") + +``` +## Accessing values in List + +- Through Index or Indices +- Through Slicing + +### 1. Through Index or Indices + +``` python +l1 = [1,2.2,"abc",4] +print (l1[1]) +print(l1[-1]) + +# output: +# 2.2 +# 4 + +``` +- The indexing start from 0 in forward direction and in reverse direction indexing start from -1. + +### 2. Through Slicing + +Syntax : list_name[starting index : ending index +1 : step] + +by default the step value is 1 + +l1[3:6] + +starting index is 3 + +ending index + 1 is 6 + +so it will result in a list from 3rd index to 5th index. + +``` python +l1 = [1,4,3,8,6,5,2] +print (l1[3:6]) +print (l1[2:7:2]) + +# output: +# [8, 6, 5] +# [3, 6, 2] + +``` + +## Count the occurence of a value in the list + +``` python +l1 = [1,4,3,2,8,2,6,5,2] +print ( l1.count(2) ) + +# Output : 3 +``` + +## Sort the list values + +``` python + +l1 = [1,4,3,2,8,2,6,5,2] +l1.sort() +print (l1) + +# Output : [1, 2, 2, 2, 3, 4, 5, 6, 8] +``` + +## Sort in descending order + +```python +l1 = [1,4,3,2,8,2,6,5,2] +l1.sort(reverse=True) +print (l1) + +# Output : [8, 6, 5, 4, 3, 2, 2, 2, 1] + +``` +## Insert element at particular location + +Syntax : List_name(index , value) + +index where the value has to be inserted. + +```python +l1 = [1,4,3,2,8,2,6,5,2] +l1.insert(5,10) +print (l1) + +# Output : [1, 4, 3, 2, 8, 10, 2, 6, 5, 2] + +``` + +## Append element in the list + +Syntax : list_name.append(value) + +This will add the value at the end of the list + +``` python +l1 = [1,4,3,2,8,2,6,5,2] +l1.append(9) +print (l1) + +# Output : [1, 4, 3, 2, 8, 2, 6, 5, 2, 9] + +``` +Incase of two list , it will append the list as a list at the end of other. + +``` python +l1 = [1,2.2,"abc",4] +l2 = ["a","b",22,9.0] +l1.append(l2) +print (l1) + +# Output : [1, 2.2, 'abc', 4, ['a', 'b', 22, 9.0]] + +``` +## Extend element in the list + +Syntax : list_name.extend(value) + +This will add the value at the end of the list + +``` python +l1 = [1,4,3,2,8,2,6,5,2] +l1.append(9) +print (l1) + +# Output : [1, 4, 3, 2, 8, 2, 6, 5, 2, 9] + +``` +Incase of two list , it will add the list items from one list to other (not the whole list +) +``` python +l1 = [1,2.2,"abc",4] +l2 = ["a","b",22,9.0] +l1.extend(l2) +print (l1) + +# Output : [1, 2.2, 'abc', 4, 'a', 'b', 22, 9.0] + +``` +## pop in list + +``` python +l1 = [1,4,3,2,8,2,6,5,2] +l=l1.pop() +print ("element popped: ",l) +print("resultant list: ",l1) + +# Output : +# element popped: 2 +# resultant list: [1, 4, 3, 2, 8, 2, 6, 5] + +``` + +## Deleting Whole List + +``` python +l1 = [1,4,3,2,8,2,6] +print(" list Before: ",l1) +del (l1[:]) +print("resultant list: ",l1) + +# Output : +# list Before: [1, 4, 3, 2, 8, 2, 6] +# resultant list: [] +``` From abcf27c8af96beff99f7dcf9a43e0d3dd17960a4 Mon Sep 17 00:00:00 2001 From: Sakshi Arora <59931342+iam-sakshi@users.noreply.github.com> Date: Mon, 29 Nov 2021 01:46:46 +0530 Subject: [PATCH 2/3] Create set.md --- Python/Set_Methods/set.md | 195 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 Python/Set_Methods/set.md diff --git a/Python/Set_Methods/set.md b/Python/Set_Methods/set.md new file mode 100644 index 0000000000..c577508f91 --- /dev/null +++ b/Python/Set_Methods/set.md @@ -0,0 +1,195 @@ +# Sets in Python + +- Unordered collection of distinct elements +- Can contain elements of different types. +- Sets are not hashable +- Can only contain hashable items. +- Enclosed inside curly braces and separated by symbol comma(,) + +## Declare Empty set + +``` python + set1 = set ( ) +``` +set1(variable name) is initialised to empty set using set function. + +## Initialising a set + +### Initialising a set with similar Data + +``` python +s1 = {"a","b","c","a"} +print (s1) + +# Output : {"a","b","c"} it can be in any order , but only unique value will be stored +``` +### Initialising a set with mixed Data +``` python +s2 = {1,2.2,"abc",4} +``` +## Finding Length of the set + +``` python +s1 = ["a","b",22,9.0] +print ( len ( s1 ) ) + +# Output : 4 +``` +## Union of two sets + +It will return the union of two sets as a new set + +``` python +s1={1,4,5,6,2,3,9} +s2={8,7,4,10,9,12} +s = s1.union(s2) +print (s) + +# Output : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12} + +``` +or + +``` python +s1={1,4,5,6,2,3,9} +s2={8,7,4,10,9,12} +s = s1 | s2 +print (s) + +# Output : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12} + +``` +## Intersection of two sets + +It will return the intersection of two sets as a new set + +``` python +s1={1,4,5,6,2,3,9} +s2={8,7,4,10,9,12} +s = s1.intersection(s2) +print (s) + +# Output : {9,4} + +``` +or + +``` python +s1={1,4,5,6,2,3,9} +s2={8,7,4,10,9,12} +s = s1 & s2 +print (s) + +# Output : {9,4} + +``` +## Difference of two sets + +It will return the difference of two sets as a new set + +``` python +s1={1,4,5,6,2,3,9} +s2={8,7,4,10,9,12} +s = s1.difference(s2) +print (s) + +# Output : {1, 2, 3, 5, 6} + +``` +or + +``` python +s1={1,4,5,6,2,3,9} +s2={8,7,4,10,9,12} +s = s1 - s2 +print (s) + +# Output : {1, 2, 3, 5, 6} + +``` +## Symmetric Difference of two sets + +It will return the symmetric difference of two sets as a new set + +``` python +s1={1,4,5,6,2,3,9} +s2={8,7,4,10,9,12} +s = s1.symmetric_difference(s2) +print (s) + +# Output : {1, 2, 3, 5, 6, 7, 8, 10, 12} + +``` +or + +``` python +s1={1,4,5,6,2,3,9} +s2={8,7,4,10,9,12} +s = s1 ^ s2 +print (s) + +# Output : {1, 2, 3, 5, 6, 7, 8, 10, 12} + +``` + +## Membership Operator in List + +- in : returns true, if the value present in set. +- not in : returns true , if the value is not present in the set. + +``` python +s1={1,4,5,6,2,3,9} +s2={8,7,4,10,9,12} +if 5 in s1: + print ("found") +else: + print("not found") + +# Output : found + +``` + +## Add element in a set + +we cannot access using index as set is unordered. + +``` python +s1={1,4,5,6,2,3,9} +s1.add(10) +print(s1) +# Output : {1,4,5,6,2,10,3,9} +``` +the element will be added but since the set are unordered so we cannot predict its position. + +## Remove element from set +to remove particular value from the set . +``` python +s1={1,4,5,6,2,3,9} +s1.remove(6) +print(s1) + +# Output : {1,4,5,2,9,3} +``` +If the element is not present it will show an error. + +## Discard element from set +To discard particular value from the set. +``` python +s1={1,4,5,6,2,3,9} +s1.remove(9) +print(s1) + +# Output : {1,4,5,2,6,3} +``` +if the element is not present it will not show any error. + +## Clear the Set +To remove all the elements from the set. + +``` python +s1={1,4,5,6,2,3,9} +s1.clear() +print(s1) + +# Output : set() +``` From 552eaa20c49e3a9541c316d7af8dcbb7aa9bf820 Mon Sep 17 00:00:00 2001 From: Sakshi Arora <59931342+iam-sakshi@users.noreply.github.com> Date: Mon, 29 Nov 2021 01:49:28 +0530 Subject: [PATCH 3/3] Delete List.md --- Python/List_Methods/List.md | 225 ------------------------------------ 1 file changed, 225 deletions(-) delete mode 100644 Python/List_Methods/List.md diff --git a/Python/List_Methods/List.md b/Python/List_Methods/List.md deleted file mode 100644 index 61ad3385c4..0000000000 --- a/Python/List_Methods/List.md +++ /dev/null @@ -1,225 +0,0 @@ -# List in Python - -It is kind of Dynamic Array , that means -- It is mutable collection of data -- it is resizeable. -- it can contain elements of different types. -- an ordered group of sequence enclosed inside square bracket and seprated by comma (,) - -## Declare Empty list -``` python - list1 = [] -``` -``` python - list2 = list ( ) -``` -In 1st list1(variable name) is initialised to empty list using square brackets. -In 2nd , list2(variable name) is initialised to empty list using list function. - -## Initialising a list - -### Initialising a list with similar Data - -``` python -l1 = ["a","b","c"] -``` -### Initialising a list with mixed Data -``` python -l2 = [1,2.2,"abc",4] -``` -## Finding Length of the List - -``` python -l1 = ["a","b",22,9.0] -print ( len ( l1 ) ) - -``` -## Concatenation of two list - -``` python -l1 = [1,2.2,"abc",4] -l2 = ["a","b",22,9.0] -l = l1 + l2 -print (l) - -# Output : [1,2.2,"abc",4,"a","b",22,9.0] - -``` -## Membership Operator in List - -- in : returns true, if the value present in list. -- not in : returns true , if the value is not present in the list. - -``` python -l1 = [1,2.2,"abc",4] -if 4 in l1 : - print ("Found 4 ") -else: - print ("not found") - -``` -## Accessing values in List - -- Through Index or Indices -- Through Slicing - -### 1. Through Index or Indices - -``` python -l1 = [1,2.2,"abc",4] -print (l1[1]) -print(l1[-1]) - -# output: -# 2.2 -# 4 - -``` -- The indexing start from 0 in forward direction and in reverse direction indexing start from -1. - -### 2. Through Slicing - -Syntax : list_name[starting index : ending index +1 : step] - -by default the step value is 1 - -l1[3:6] - -starting index is 3 - -ending index + 1 is 6 - -so it will result in a list from 3rd index to 5th index. - -``` python -l1 = [1,4,3,8,6,5,2] -print (l1[3:6]) -print (l1[2:7:2]) - -# output: -# [8, 6, 5] -# [3, 6, 2] - -``` - -## Count the occurence of a value in the list - -``` python -l1 = [1,4,3,2,8,2,6,5,2] -print ( l1.count(2) ) - -# Output : 3 -``` - -## Sort the list values - -``` python - -l1 = [1,4,3,2,8,2,6,5,2] -l1.sort() -print (l1) - -# Output : [1, 2, 2, 2, 3, 4, 5, 6, 8] -``` - -## Sort in descending order - -```python -l1 = [1,4,3,2,8,2,6,5,2] -l1.sort(reverse=True) -print (l1) - -# Output : [8, 6, 5, 4, 3, 2, 2, 2, 1] - -``` -## Insert element at particular location - -Syntax : List_name(index , value) - -index where the value has to be inserted. - -```python -l1 = [1,4,3,2,8,2,6,5,2] -l1.insert(5,10) -print (l1) - -# Output : [1, 4, 3, 2, 8, 10, 2, 6, 5, 2] - -``` - -## Append element in the list - -Syntax : list_name.append(value) - -This will add the value at the end of the list - -``` python -l1 = [1,4,3,2,8,2,6,5,2] -l1.append(9) -print (l1) - -# Output : [1, 4, 3, 2, 8, 2, 6, 5, 2, 9] - -``` -Incase of two list , it will append the list as a list at the end of other. - -``` python -l1 = [1,2.2,"abc",4] -l2 = ["a","b",22,9.0] -l1.append(l2) -print (l1) - -# Output : [1, 2.2, 'abc', 4, ['a', 'b', 22, 9.0]] - -``` -## Extend element in the list - -Syntax : list_name.extend(value) - -This will add the value at the end of the list - -``` python -l1 = [1,4,3,2,8,2,6,5,2] -l1.append(9) -print (l1) - -# Output : [1, 4, 3, 2, 8, 2, 6, 5, 2, 9] - -``` -Incase of two list , it will add the list items from one list to other (not the whole list -) -``` python -l1 = [1,2.2,"abc",4] -l2 = ["a","b",22,9.0] -l1.extend(l2) -print (l1) - -# Output : [1, 2.2, 'abc', 4, 'a', 'b', 22, 9.0] - -``` -## pop in list - -``` python -l1 = [1,4,3,2,8,2,6,5,2] -l=l1.pop() -print ("element popped: ",l) -print("resultant list: ",l1) - -# Output : -# element popped: 2 -# resultant list: [1, 4, 3, 2, 8, 2, 6, 5] - -``` - -## Deleting Whole List - -``` python -l1 = [1,4,3,2,8,2,6] -print(" list Before: ",l1) -del (l1[:]) -print("resultant list: ",l1) - -# Output : -# list Before: [1, 4, 3, 2, 8, 2, 6] -# resultant list: [] -```