{
“cells”: [
{
“cell_type”: “markdown”,
“id”: “c7ded892-dcce-4eb8-bd44-1b06875a4eac”,
“metadata”: {},
“source”: [
“**TUPLES**”
]
},
{
“cell_type”: “markdown”,
“id”: “9244e377-5bb0-4e21-8861-02ddd0673a28”,
“metadata”: {},
“source”: [
“Tuples are immutable. Doesnt matter if you turn a mutable object into tuple. Its still immutable\n”,
“\n”,
“You can create tuples like listing”
]
},
{
“cell_type”: “code”,
“execution_count”: 1,
“id”: “2f4161f4-a2ef-4703-be47-874e433158a4”,
“metadata”: {},
“outputs”: [],
“source”: [
“tup = (5,6,7,(8,9,10),’string’)”
]
},
{
“cell_type”: “markdown”,
“id”: “93874e6f-c93e-4371-bca4-784c8e09c61b”,
“metadata”: {},
“source”: [
“And you can call any item from tuple with a bracket and number”
]
},
{
“cell_type”: “code”,
“execution_count”: 2,
“id”: “95739519-b7cb-4792-9a74-f8dd24fb86cb”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“(8, 9, 10)”
]
},
“execution_count”: 2,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“tup[3]”
]
},
{
“cell_type”: “markdown”,
“id”: “ec8f7321-a33f-448b-aff2-d8d7de528fcb”,
“metadata”: {},
“source”: [
“You can turn anyting into tuple. with a simple tuple function”
]
},
{
“cell_type”: “code”,
“execution_count”: 3,
“id”: “69291527-552a-43ff-bdf3-8da8d5473aaa”,
“metadata”: {},
“outputs”: [],
“source”: [
“tup = tuple(‘string’)”
]
},
{
“cell_type”: “code”,
“execution_count”: 4,
“id”: “93e41ed4-4f3e-4b89-8b8e-ca529135df77”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“(‘s’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’)”
]
},
“execution_count”: 4,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“tup”
]
},
{
“cell_type”: “markdown”,
“id”: “1e869a56-980f-4e1e-bb57-1f5da7e4fde9”,
“metadata”: {},
“source”: [
“**Unpacking Tuples**\n”,
“\n”,
“IF you assign variables as much as len of tuple you can assign each item to a variable.”
]
},
{
“cell_type”: “code”,
“execution_count”: 1,
“id”: “36eff7c8-9910-417a-8979-b12b414b5870”,
“metadata”: {},
“outputs”: [],
“source”: [
“tup = (1,2,3,(4,5))”
]
},
{
“cell_type”: “code”,
“execution_count”: 2,
“id”: “6151477c-dfc2-4e20-bd19-923872a10342”,
“metadata”: {},
“outputs”: [],
“source”: [
“a,b,c,d = tup”
]
},
{
“cell_type”: “code”,
“execution_count”: 3,
“id”: “04823c10-3f92-4e83-858e-d358c97095a7”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“(4, 5)”
]
},
“execution_count”: 3,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“d”
]
},
{
“cell_type”: “markdown”,
“id”: “9dc9f80e-7216-416b-b476-e1a39a661af3”,
“metadata”: {},
“source”: [
“A commoun use of variable unpacking is iterating over sequences of tuples or lists; Note: you can use this method to work on strings as well.”
]
},
{
“cell_type”: “code”,
“execution_count”: 4,
“id”: “91dc0582-1b54-45af-ba77-3de1586a0d5a”,
“metadata”: {},
“outputs”: [],
“source”: [
“seq = [(1,2,3),(4,5,6),(7,8,9)]”
]
},
{
“cell_type”: “code”,
“execution_count”: 6,
“id”: “bd6e5086-239c-4b21-8f77-939498e87896”,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“a=1 b=2 c=3\n”,
“a=4 b=5 c=6\n”,
“a=7 b=8 c=9\n”
]
}
],
“source”: [
“for a,b,c in seq:\n”,
” print(f’a={a} b={b} c={c}’)”
]
},
{
“cell_type”: “markdown”,
“id”: “dd9edd5c-a89c-4613-8663-2e1ec469e762”,
“metadata”: {},
“source”: [
“Also you can pluck a few elements from the beginning of a tuple. with a special syntax; *rest. Rest bit interchangable. you can use *others as well”
]
},
{
“cell_type”: “code”,
“execution_count”: 7,
“id”: “095272cc-a84d-4447-b9c2-ee369a45b92e”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“(1, 2, [3, 4, 5])”
]
},
“execution_count”: 7,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“values = 1,2,3,4,5\n”,
“a,b,*rest = values\n”,
“a,b,rest”
]
},
{
“cell_type”: “markdown”,
“id”: “e1145742-6b2d-480b-bee1-18e5bc87e353”,
“metadata”: {},
“source”: [
“—”
]
},
{
“cell_type”: “markdown”,
“id”: “8b98f1da-763b-4338-a4fd-bc41319ac6b6”,
“metadata”: {},
“source”: [
“**LISTS**”
]
},
{
“cell_type”: “markdown”,
“id”: “07e97fb7-9cd3-4082-ba9e-120929ac5536”,
“metadata”: {},
“source”: [
“In contrast with tuples lists are variable length and their contents can be modified. Lists are mutable. You define them by using [] square brackets.”
]
},
{
“cell_type”: “code”,
“execution_count”: 8,
“id”: “7d2f30af-c909-4c04-9ae4-15039352e9df”,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“1\n”,
“2\n”,
“3\n”
]
}
],
“source”: [
“a_list = [1,2,3]\n”,
“for item in a_list:\n”,
” print(item)”
]
},
{
“cell_type”: “markdown”,
“id”: “3eefe82a-a2cb-42d3-8037-e091b36485a6”,
“metadata”: {},
“source”: [
“The list built-in function is frequently used in data processing as a way to materialize an iterator or generater expression.”
]
},
{
“cell_type”: “code”,
“execution_count”: 10,
“id”: “74b5cdd5-d23f-460f-ac39-b7c5200f86a5”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“range(0, 10)”
]
},
“execution_count”: 10,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“gen = range(10)\n”,
“gen”
]
},
{
“cell_type”: “code”,
“execution_count”: 22,
“id”: “008bf0d5-cb8f-4c85-9cb1-29789fb3c9ac”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]”
]
},
“execution_count”: 22,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“list(gen)”
]
},
{
“cell_type”: “markdown”,
“id”: “aa263334-f7c9-463f-8d80-9a6895dc1d4f”,
“metadata”: {},
“source”: [
“There are functions built in list. Such as: \\\n”,
” .append(add an element at the end of list.), \\\n”,
” .insert(insert an element at a specific location), \\\n”,
” .pop(removes and returns an element at a particular index), \\\n”,
” .remove(takes the value and search it in list and removes first such value) \\”
]
},
{
“cell_type”: “code”,
“execution_count”: 41,
“id”: “4d41663a-3f2f-4e05-b4c2-6f07fb626826”,
“metadata”: {},
“outputs”: [],
“source”: [
“gen_list = list(gen)”
]
},
{
“cell_type”: “code”,
“execution_count”: 42,
“id”: “9d6ccca6-17c3-4c18-97d0-ae27d6ccad97”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]”
]
},
“execution_count”: 42,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“gen_list.append(10)#add 10 to end of the list\n”,
“gen_list”
]
},
{
“cell_type”: “code”,
“execution_count”: 46,
“id”: “128dc5f9-78ec-4b63-aa5b-840e95c79b4c”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[0, 1, 2, 1, 3, 4, 5, 6, 7, 8, 10]”
]
},
“execution_count”: 46,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“gen_list.insert(3,1)#add 1 to 3rd index\n”,
“gen_list”
]
},
{
“cell_type”: “code”,
“execution_count”: 44,
“id”: “9ac2b8aa-277b-46ef-ab22-5b9de2e6b6ee”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]”
]
},
“execution_count”: 44,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“gen_list.pop(3)#removes 4th element\n”,
“gen_list”
]
},
{
“cell_type”: “code”,
“execution_count”: 45,
“id”: “fd368f9f-d7fa-4f95-88ba-7fabfe525a3c”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[0, 1, 2, 3, 4, 5, 6, 7, 8, 10]”
]
},
“execution_count”: 45,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“gen_list.remove(9)#removes the 9\n”,
“gen_list”
]
},
{
“cell_type”: “markdown”,
“id”: “e33d7575-9ca6-4a4e-8ef5-1aaecebef536”,
“metadata”: {},
“source”: [
“You can sort a list in place by calling sort() function.”
]
},
{
“cell_type”: “markdown”,
“id”: “d801949a-8950-4f6e-b941-86cd86ba6d52”,
“metadata”: {},
“source”: [
“**Slicing**”
]
},
{
“cell_type”: “code”,
“execution_count”: 48,
“id”: “2565834a-1afc-4192-a5d2-79036c9aaad3”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[2, 3, 7, 5]”
]
},
“execution_count”: 48,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“seq = [7,2,3,7,5,6,0,1]\n”,
“seq[1:5]#return from starting index 1 till but not including 5”
]
},
{
“cell_type”: “code”,
“execution_count”: 50,
“id”: “a8f4b5b9-52c3-479e-bec8-acbfa1b74271”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[7, 2, 3, 6, 3, 6, 0, 1]”
]
},
“execution_count”: 50,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“seq[3:5] = [6,3]#Change 3rd and 4th item to 6,3\n”,
“seq”
]
},
{
“cell_type”: “code”,
“execution_count”: 52,
“id”: “3a062b8c-077f-49fb-bf72-ff559dac9af2”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[7, 2, 3]”
]
},
“execution_count”: 52,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“seq[:3]#start from beginning to 3rd item”
]
},
{
“cell_type”: “code”,
“execution_count”: 53,
“id”: “b5d24fc5-b783-453b-8416-3fdadbecbd4e”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[6, 3, 6, 0, 1]”
]
},
“execution_count”: 53,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“seq[3:]#start from 3rd item to end”
]
},
{
“cell_type”: “code”,
“execution_count”: 54,
“id”: “87712f89-d19e-4b91-8d04-599e22a69848”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[3, 6, 0, 1]”
]
},
“execution_count”: 54,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“seq[-4:]#start from 4th item from the end and list it till end”
]
},
{
“cell_type”: “markdown”,
“id”: “ceee8bdd-ae63-4998-9a0d-e3ba355fe451”,
“metadata”: {},
“source”: [
“—–”
]
},
{
“cell_type”: “markdown”,
“id”: “3dd824b1-23b6-4ffa-b950-534d2fc50469”,
“metadata”: {},
“source”: [
“**DICTIONARY**”
]
},
{
“cell_type”: “code”,
“execution_count”: 57,
“id”: “0ccc4aaf-5d2b-4bc9-9b68-89614ddf381f”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘an integer’”
]
},
“execution_count”: 57,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“my_dict = {‘a’:’some value’, ‘b’: [1,2,3,4]}\n”,
“my_dict[‘c’] = ‘an integer’ #add new element to dict.\n”,
“my_dict[‘c’]#list the value of key(‘c’)”
]
},
{
“cell_type”: “code”,
“execution_count”: 58,
“id”: “160b939b-4a8b-4ad7-9b91-80ac9fd6edca”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“True”
]
},
“execution_count”: 58,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“‘b’ in my_dict#you can ask if a key exist in my_dict”
]
},
{
“cell_type”: “code”,
“execution_count”: 59,
“id”: “42555cb2-21f7-4560-a5f6-297ff06e0b6f”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘a’: ‘some value’, ‘b’: [1, 2, 3, 4]}”
]
},
“execution_count”: 59,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“my_dict.pop(‘c’)#pops an item\n”,
“my_dict”
]
},
{
“cell_type”: “code”,
“execution_count”: 60,
“id”: “64d7796c-3286-4dba-91a7-8698b28503da”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘a’, ‘b’]”
]
},
“execution_count”: 60,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“list(my_dict.keys())”
]
},
{
“cell_type”: “code”,
“execution_count”: 61,
“id”: “c2ca5827-32fb-4ae2-9348-9fd31c5ab8c1”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘some value’, [1, 2, 3, 4]]”
]
},
“execution_count”: 61,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“list(my_dict.values())”
]
},
{
“cell_type”: “code”,
“execution_count”: 62,
“id”: “8ba350f6-4087-4572-92f7-51a4b86ed7cc”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[(‘a’, ‘some value’), (‘b’, [1, 2, 3, 4])]”
]
},
“execution_count”: 62,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“list(my_dict.items())”
]
},
{
“cell_type”: “code”,
“execution_count”: 63,
“id”: “182dc8d9-cfa3-4173-8aa7-07270d3bcebb”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘a’: ‘some value’, ‘b’: [1, 2, 3, 4], 1: 2, 2: 3, 3: 4}”
]
},
“execution_count”: 63,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“my_dict_2 = {1:2,2:3,3:4}\n”,
“my_dict.update(my_dict_2)#merges two dictionary together\n”,
“my_dict”
]
},
{
“cell_type”: “code”,
“execution_count”: 65,
“id”: “837663bd-9977-4e26-9488-9376c7b6f20f”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4, ‘e’: 5}”
]
},
“execution_count”: 65,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“key_list = [‘a’,’b’,’c’,’d’,’e’]#list of keys\n”,
“value_list = [1,2,3,4,5]#list of values\n”,
“mapping = {}#empty dictionary\n”,
“for key, value in zip(key_list, value_list):\n”,
” mapping[key] = value\n”,
“mapping”
]
},
{
“cell_type”: “code”,
“execution_count”: 71,
“id”: “6b02137d-b71b-4687-8dbd-c1438ad8be91”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [

]
},
“execution_count”: 71,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“tuples = zip(range(5), reversed(range(5)))#a dictionary is a combination of two tuples\n”,
“tuples”
]
},
{
“cell_type”: “code”,
“execution_count”: 72,
“id”: “736b6046-2727-4e1d-8e26-e86d05fc481d”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{0: 4, 1: 3, 2: 2, 3: 1, 4: 0}”
]
},
“execution_count”: 72,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“mappings = dict(tuples)\n”,
“mappings”
]
},
{
“cell_type”: “markdown”,
“id”: “3a95bc77-3ffe-46f9-8a63-de5bd69afb96”,
“metadata”: {},
“source”: [
“**Set Default Dictionary** \\\n”,
“You could imagine categorizing a list of words by their first letter as a dictionary of lists.”
]
},
{
“cell_type”: “code”,
“execution_count”: 75,
“id”: “ba01192f-cef7-4a4c-b3e4-d3c765ca3f20”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘a’: [‘apple’, ‘atom’], ‘b’: [‘bat’, ‘bar’, ‘book’]}”
]
},
“execution_count”: 75,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“words = [\”apple\”, \”bat\”, \”bar\”, \”atom\”, \”book\”]\n”,
“by_letter = {}\n”,
“\n”,
“for word in words:\n”,
” letter = word[0]\n”,
” if letter not in by_letter:\n”,
” by_letter[letter] = [word]\n”,
” else:\n”,
” by_letter[letter].append(word)\n”,
“\n”,
“by_letter”
]
},
{
“cell_type”: “markdown”,
“id”: “af46a32e-4905-49d4-9235-c0ad74e0fca9”,
“metadata”: {},
“source”: [
“The setdefault dictionary method can be used to simplify this workflow. The preceding for loop can be rewritten as:”
]
},
{
“cell_type”: “code”,
“execution_count”: 78,
“id”: “4336935b-9f14-414f-87a1-b015689bad57”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{‘a’: [‘apple’, ‘atom’], ‘b’: [‘bat’, ‘bar’, ‘book’]}”
]
},
“execution_count”: 78,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“by_letter = {} #lets empty dict.\n”,
“\n”,
“for word in words:\n”,
” letter = word[0]\n”,
” by_letter.setdefault(letter,[]).append(word)#its basically setting if key exist enters if else pass and append word to the list.\n”,
” #this way every key will be added only once.\n”,
“by_letter”
]
},
{
“cell_type”: “markdown”,
“id”: “71c71501-55ba-4c58-bf4b-e620d37fc4e4”,
“metadata”: {},
“source”: [
“The built-in collections module has a useful class, defaultdict, which makes this even easier. To create one, you pass a type or function for generating the default vallue for each slot in the dictionary.”
]
},
{
“cell_type”: “code”,
“execution_count”: 80,
“id”: “f08d9817-3167-49d1-bfab-b10ce9887986”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“defaultdict(list, {‘a’: [‘apple’, ‘atom’], ‘b’: [‘bat’, ‘bar’, ‘book’]})”
]
},
“execution_count”: 80,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“from collections import defaultdict\n”,
“by_letter = defaultdict(list)\n”,
“\n”,
“for word in words:\n”,
” by_letter[word[0]].append(word)\n”,
“\n”,
“by_letter”
]
},
{
“cell_type”: “markdown”,
“id”: “5fff63c6-dac8-4e26-b54f-86666ec256a9”,
“metadata”: {},
“source”: [
“—\n”,
“***SET*** \\\n”,
“A set is an unordered collection of unique elements.”
]
},
{
“cell_type”: “code”,
“execution_count”: 81,
“id”: “a9b0cc8f-1628-4882-9e94-ae6430adfd83”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{1, 2, 3}”
]
},
“execution_count”: 81,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“set([2,2,2,1,3,3])”
]
},
{
“cell_type”: “code”,
“execution_count”: 82,
“id”: “902c705a-3893-4541-a9dc-bc3b3519fc18”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{1, 2, 3}”
]
},
“execution_count”: 82,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“{2,2,2,1,3,3}”
]
},
{
“cell_type”: “code”,
“execution_count”: 83,
“id”: “a7dddb1c-1a9e-41bc-a096-ab12c6726346”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{1, 2, 3, 4, 5, 6, 7, 8}”
]
},
“execution_count”: 83,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“a = {1,2,3,4,5}\n”,
“b = {3,4,5,6,7,8}\n”,
“a.union(b)”
]
},
{
“cell_type”: “code”,
“execution_count”: 84,
“id”: “416096d3-c220-4bd0-958b-4366e44e812f”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{1, 2, 3, 4, 5, 6, 7, 8}”
]
},
“execution_count”: 84,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“a|b”
]
},
{
“cell_type”: “code”,
“execution_count”: 85,
“id”: “3ccd0422-18c4-4c3e-aeb5-945bbf1a32f8”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“{3, 4, 5}”
]
},
“execution_count”: 85,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“a.intersection(b)\n”,
“a&b”
]
},
{
“cell_type”: “code”,
“execution_count”: 91,
“id”: “52b95435-6ab7-4aff-8645-81aeccaccd81”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“False”
]
},
“execution_count”: 91,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“a.clear()#Reset set to an empty state discarding all of its elements\n”,
“a.add(‘x’)#Add element x to set a\n”,
“\n”,
“a = {1,2,3,4,5,’x’}\n”,
“a.remove(‘x’)#Remove element x from set a\n”,
“a.pop()#remove an arbitrary element from seet a raising keyerror if the set is empty\n”,
“\n”,
“a.union(b)#Combines set a and b with unique elements\n”,
“a.update(b)#Set the contents of a to be the union of elements in a and b\n”,
“\n”,
“a.intersection(b)#All of the elements that is both in a and in b\n”,
“a.intersection_update(b)#Set the contents of a to be the intersection of the elements in a and b\n”,
“\n”,
“a.difference(b)#The elements in a that are not in b\n”,
“a.difference_update(b)#set a to the elements in a that are not in b\n”,
“a.symmetric_difference(b)#all of the elements in either a or b but not both\n”,
“a.symmetric_difference_update(b)#set a to contain the elements in either a or b but not both\n”,
“\n”,
“a.issubset(b)#True if the elements of a are all contained in b\n”,
“a.issuperset(b)#true if the elements of b are all contained in a\n”,
“a.isdisjoint(b)#true if a and b have no elements in common.”
]
},
{
“cell_type”: “markdown”,
“id”: “c0c24a1b-3e82-49d4-9de0-5e7fac16674a”,
“metadata”: {},
“source”: [
“**enumerate**”
]
},
{
“cell_type”: “code”,
“execution_count”: 95,
“id”: “7fbfab31-3ef3-4e00-b739-8c70143fa581”,
“metadata”: {},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“apple\n”,
“0\n”,
“banana\n”,
“1\n”,
“orange\n”,
“2\n”,
“pear\n”,
“3\n”
]
}
],
“source”: [
“words = [‘apple’, ‘banana’, ‘orange’, ‘pear’]\n”,
“\n”,
“for i, word in enumerate(words):\n”,
” print(word)\n”,
” print(i)”
]
},
{
“cell_type”: “markdown”,
“id”: “7687a1a7-ffa0-49f8-b6b0-3d7f8df23261”,
“metadata”: {},
“source”: [
“—\n”,
“**List, Set and Dictionary Comprehensions** \n”,
“\n”,
” List Comprehensions:”
]
},
{
“cell_type”: “code”,
“execution_count”: 97,
“id”: “42eb8dd4-cdf7-43ac-83ab-3dc14b2fab45”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘bat’, ‘car’, ‘dove’, ‘python’]”
]
},
“execution_count”: 97,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“strings = [‘a’, ‘as’, ‘bat’, ‘car’, ‘dove’, ‘python’]\n”,
“result = []\n”,
“for string in strings:\n”,
” if len(string)>2:\n”,
” result.append(string)\n”,
“result\n”,
“#This is the long way we can instead do it like the below”
]
},
{
“cell_type”: “code”,
“execution_count”: 98,
“id”: “bca5c432-d838-4dfe-a32e-b825b0bb02e5”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“[‘bat’, ‘car’, ‘dove’, ‘python’]”
]
},
“execution_count”: 98,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“result = [string for string in strings if len(string)>2]\n”,
“result”
]
},
{
“cell_type”: “markdown”,
“id”: “7012dea1-569c-4d69-a3d9-3644e979b99e”,
“metadata”: {},
“source”: [
“Dict and set comprehensions are similar: \\\n”,
“dict_comp = {key-expr: value-expr for value in collection if condition} \\\n”,
“set_comp = {expr for value in collection if condition}”
]
},
{
“cell_type”: “markdown”,
“id”: “43b9db22-5b55-4d84-913f-199cbbd8e1a3”,
“metadata”: {},
“source”: [
“—\n”,
“**FUNCTIONS** \\\n”,
“\\\n”,
“def short_function(x):\n”,
“>return x*2 ”
]
},
{
“cell_type”: “code”,
“execution_count”: 1,
“id”: “d2876f56-d154-4353-9ab7-1dccfd785f94”,
“metadata”: {},
“outputs”: [],
“source”: [
“equiv_anon = lambda x: x*2”
]
},
{
“cell_type”: “code”,
“execution_count”: 2,
“id”: “326f37c2-fc4e-4bc9-a8e1-b6bf38fb299c”,
“metadata”: {},
“outputs”: [
{
“data”: {
“text/plain”: [
“10”
]
},
“execution_count”: 2,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“equiv_anon(5)”
]
},
{
“cell_type”: “code”,
“execution_count”: null,
“id”: “fcf270d9-9982-48a6-8466-346887a35fde”,
“metadata”: {},
“outputs”: [],
“source”: []
}
],
“metadata”: {
“kernelspec”: {
“display_name”: “Python 3 (ipykernel)”,
“language”: “python”,
“name”: “python3”
},
“language_info”: {
“codemirror_mode”: {
“name”: “ipython”,
“version”: 3
},
“file_extension”: “.py”,
“mimetype”: “text/x-python”,
“name”: “python”,
“nbconvert_exporter”: “python”,
“pygments_lexer”: “ipython3”,
“version”: “3.10.13”
}
},
“nbformat”: 4,
“nbformat_minor”: 5
}


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *