├── README.md
└── Cyber Security Bootcamp
├── weather.py
├── Day_1_Data_types_operators_CySec.ipynb
├── Day_3_Loops_if&else_Functions.ipynb
└── Day _2_Boolean_list_tuple_sets_dict_cysec.ipynb
/README.md:
--------------------------------------------------------------------------------
1 | # cybersecurity-bootcamp Notebooks
2 | By Harsh Akshit
3 |
--------------------------------------------------------------------------------
/Cyber Security Bootcamp/weather.py:
--------------------------------------------------------------------------------
1 | import requests
2 | #import os
3 | from datetime import datetime
4 |
5 | api_key = '87d845b0b6cf29baa1a73cc34b067a95'
6 | location = input("Enter the city name: ")
7 |
8 | complete_api_link = "https://api.openweathermap.org/data/2.5/weather?q="+location+"&appid="+api_key
9 | api_link = requests.get(complete_api_link)
10 | api_data = api_link.json()
11 |
12 | #create variables to store and display data
13 | temp_city = ((api_data['main']['temp']) - 273.15)
14 | weather_desc = api_data['weather'][0]['description']
15 | hmdt = api_data['main']['humidity']
16 | wind_spd = api_data['wind']['speed']
17 | date_time = datetime.now().strftime("%d %b %Y | %I:%M:%S %p")
18 |
19 | print ("-------------------------------------------------------------")
20 | print ("Weather Stats for - {} || {}".format(location.upper(), date_time))
21 | print ("-------------------------------------------------------------")
22 |
23 | print ("Current temperature is: {:.2f} deg C".format(temp_city))
24 | print ("Current weather desc :",weather_desc)
25 | print ("Current Humidity :",hmdt, '%')
26 | print ("Current wind speed :",wind_spd ,'kmph')
27 |
28 | print("====================================================")
29 |
30 |
31 | # making a list so that i can print the info to a txt
32 | txtlist = [temp_city,weather_desc,hmdt,wind_spd,date_time]
33 | #using open() buit-in function to write to a text file
34 | with open("textfile.txt" , mode= 'w' ,encoding= 'utf-8') as f :
35 | #encoding = utf-8 for linux and cp1252 for win
36 | f.write("------------------------------------------------------------- \n ")
37 | f.write("Weather Stats for - {} || {}".format(location.upper(), date_time))
38 | f.write("\n ------------------------------------------------------------- \n")
39 | f.write("Current temperature is: {:.2f} deg C\n".format(txtlist[0]))
40 |
41 | f.write("{},{} \n".format("Current weather desc :" ,txtlist[1]))
42 | f.write("{},{},{} \n".format("Current Humidity :",txtlist[2],"%"))
43 | f.write("{},{},{} \n".format("Current wind speed :",txtlist[3],"kmph"))
44 | f.write("====================================================")
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Cyber Security Bootcamp/Day_1_Data_types_operators_CySec.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "
Data Types and Operators
\n",
8 | "Welcome to this lesson on Data Types and Operators! You'll learn about:
\n",
9 | "\n",
10 | "- Data Types:
Strings, Integers, Floats, Booleans, Lists, Tuples, Sets, Dictionaries\n",
11 | "\n",
12 | "- Operators:
Arithmetic, Assignment, Comparison, Logical, Membership, Identity\n",
13 | "\n",
14 | "\n",
15 | "- Built-In Functions, Compound Data Structures, Type Conversion\n"
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {},
21 | "source": [
22 | "Strings:"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": 8,
28 | "metadata": {},
29 | "outputs": [],
30 | "source": [
31 | "#String: Enclosed textual data within quotes\n",
32 | "var_1 = 'Hello World!'\n",
33 | "var_2 = \"Youtube is Google's video streaming platform\""
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": 9,
39 | "metadata": {},
40 | "outputs": [
41 | {
42 | "name": "stdout",
43 | "output_type": "stream",
44 | "text": [
45 | "Hello World!\n"
46 | ]
47 | }
48 | ],
49 | "source": [
50 | "print(var_1)\n",
51 | "#Prints the value of variable"
52 | ]
53 | },
54 | {
55 | "cell_type": "code",
56 | "execution_count": 10,
57 | "metadata": {},
58 | "outputs": [
59 | {
60 | "name": "stdout",
61 | "output_type": "stream",
62 | "text": [
63 | "12\n"
64 | ]
65 | }
66 | ],
67 | "source": [
68 | "print(len(var_1))\n",
69 | "#Prints the length of variable"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": 11,
75 | "metadata": {},
76 | "outputs": [
77 | {
78 | "name": "stdout",
79 | "output_type": "stream",
80 | "text": [
81 | "d\n"
82 | ]
83 | }
84 | ],
85 | "source": [
86 | "#In python, Indexing starts from 0,1,2,3\n",
87 | "print(var_1[10])\n",
88 | "#prints character at index 10\n"
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": 12,
94 | "metadata": {},
95 | "outputs": [
96 | {
97 | "name": "stdout",
98 | "output_type": "stream",
99 | "text": [
100 | "Hello\n"
101 | ]
102 | }
103 | ],
104 | "source": [
105 | "print(var_1[0:5])\n",
106 | "#prints character index 0-4 but 5 isn't included"
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": 13,
112 | "metadata": {},
113 | "outputs": [
114 | {
115 | "name": "stdout",
116 | "output_type": "stream",
117 | "text": [
118 | "World!\n"
119 | ]
120 | }
121 | ],
122 | "source": [
123 | "print(var_1[6:])\n",
124 | "#prints character index 6-last index "
125 | ]
126 | },
127 | {
128 | "cell_type": "markdown",
129 | "metadata": {},
130 | "source": [
131 | "###### The above operations using square [] brackets is called slicing"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": 14,
137 | "metadata": {},
138 | "outputs": [
139 | {
140 | "name": "stdout",
141 | "output_type": "stream",
142 | "text": [
143 | "hello world!\n"
144 | ]
145 | }
146 | ],
147 | "source": [
148 | "print(var_1.lower()) #Prints strings in lower case"
149 | ]
150 | },
151 | {
152 | "cell_type": "code",
153 | "execution_count": 15,
154 | "metadata": {},
155 | "outputs": [
156 | {
157 | "name": "stdout",
158 | "output_type": "stream",
159 | "text": [
160 | "HELLO WORLD!\n"
161 | ]
162 | }
163 | ],
164 | "source": [
165 | "print(var_1.upper()) #Prints strings in upper case"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": 21,
171 | "metadata": {},
172 | "outputs": [
173 | {
174 | "name": "stdout",
175 | "output_type": "stream",
176 | "text": [
177 | "hello, harsh akshit. Welcome!\n",
178 | "hello, harsh akshit. Welcome!\n"
179 | ]
180 | }
181 | ],
182 | "source": [
183 | "greeting = 'hello'\n",
184 | "name = 'harsh akshit'\n",
185 | "\n",
186 | "message_1 = greeting+', '+name+'. Welcome!' #String concatenation\n",
187 | "message_2 = '{}, {}. Welcome!'.format(greeting,name)\n",
188 | "\n",
189 | "print(message_1)\n",
190 | "print(message_2)"
191 | ]
192 | },
193 | {
194 | "cell_type": "markdown",
195 | "metadata": {},
196 | "source": [
197 | "
\n",
198 | "\n",
199 | "## Variable Naming Conventions:**\n",
200 | "\n",
201 | "There are some rules we need to follow while giving a name for a Python variable.\n",
202 | "\n",
203 | "- **Rule-1**: You should start variable name with an alphabet or **underscore(_)** character.\n",
204 | "- **Rule-2:** A variable name can only contain **A-Z,a-z,0-9** and **underscore(_)**.\n",
205 | "- **Rule-3:** You cannot start the variable name with a **number**.\n",
206 | "- **Rule-4:** You cannot use special characters with the variable name such as such as **$,%,#,&,@.-,^** etc.\n",
207 | "- **Rule-5**: Variable names are **case sensitive**. For example str and Str are two different variables.\n",
208 | "- **Rule-6:** Do not use reserve keyword as a variable name for example keywords like **class, for, def, del, is, else,** **try, from,** etc. more examples are given below and as we go through the course we will come across many more. Creating names that are descriptive of the values often will help you avoid using any of these words."
209 | ]
210 | },
211 | {
212 | "cell_type": "code",
213 | "execution_count": 22,
214 | "metadata": {},
215 | "outputs": [],
216 | "source": [
217 | "#Allowed variable names\n",
218 | "\n",
219 | "x=2\n",
220 | "y=\"Hello\"\n",
221 | "mypython=\"PythonGuides\"\n",
222 | "my_python=\"PythonGuides\"\n",
223 | "_my_python=\"PythonGuides\"\n",
224 | "_mypython=\"PythonGuides\"\n",
225 | "MYPYTHON=\"PythonGuides\"\n",
226 | "myPython=\"PythonGuides\"\n",
227 | "myPython7=\"PythonGuides\"\n"
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "execution_count": 23,
233 | "metadata": {},
234 | "outputs": [
235 | {
236 | "ename": "SyntaxError",
237 | "evalue": "invalid syntax (, line 3)",
238 | "output_type": "error",
239 | "traceback": [
240 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m 7mypython=\"PythonGuides\"\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
241 | ]
242 | }
243 | ],
244 | "source": [
245 | "#Variable name not Allowed\n",
246 | "\n",
247 | "7mypython=\"PythonGuides\"\n",
248 | "-mypython=\"PythonGuides\"\n",
249 | "myPy@thon=\"PythonGuides\"\n",
250 | "my Python=\"PythonGuides\"\n",
251 | "for=\"PythonGuides\"\n",
252 | "\n",
253 | "#It shows invalid syntax. \n",
254 | "#It will execute one by one and will show the error."
255 | ]
256 | },
257 | {
258 | "cell_type": "markdown",
259 | "metadata": {},
260 | "source": [
261 | "# Integers and Floats:\n",
262 | "##### Working with numeric data"
263 | ]
264 | },
265 | {
266 | "cell_type": "markdown",
267 | "metadata": {},
268 | "source": [
269 | "### Arithmetic Operators:\n",
270 | "#### Addition: 7+5\n",
271 | "#### Subtraction: 7-5\n",
272 | "#### Multiplication: 7*5\n",
273 | "#### Division: 7/5 \n",
274 | "#### Floor Division: 7//5 -> Drops decimal value\n",
275 | "#### Exponent: 7**5\n",
276 | "#### Modulus: 7%5 "
277 | ]
278 | },
279 | {
280 | "cell_type": "code",
281 | "execution_count": 25,
282 | "metadata": {},
283 | "outputs": [
284 | {
285 | "data": {
286 | "text/plain": [
287 | "11"
288 | ]
289 | },
290 | "execution_count": 25,
291 | "metadata": {},
292 | "output_type": "execute_result"
293 | }
294 | ],
295 | "source": [
296 | "abs(-11) #Returns absolute value of integer data"
297 | ]
298 | },
299 | {
300 | "cell_type": "code",
301 | "execution_count": 27,
302 | "metadata": {},
303 | "outputs": [
304 | {
305 | "name": "stdout",
306 | "output_type": "stream",
307 | "text": [
308 | "3\n"
309 | ]
310 | }
311 | ],
312 | "source": [
313 | "num = 1\n",
314 | "num = num+1\n",
315 | "num += 1\n",
316 | "print(num)"
317 | ]
318 | },
319 | {
320 | "cell_type": "code",
321 | "execution_count": 28,
322 | "metadata": {},
323 | "outputs": [
324 | {
325 | "data": {
326 | "text/plain": [
327 | "9"
328 | ]
329 | },
330 | "execution_count": 28,
331 | "metadata": {},
332 | "output_type": "execute_result"
333 | }
334 | ],
335 | "source": [
336 | "round(8.63) # round to nearest integer"
337 | ]
338 | },
339 | {
340 | "cell_type": "code",
341 | "execution_count": 29,
342 | "metadata": {},
343 | "outputs": [
344 | {
345 | "data": {
346 | "text/plain": [
347 | "8.6"
348 | ]
349 | },
350 | "execution_count": 29,
351 | "metadata": {},
352 | "output_type": "execute_result"
353 | }
354 | ],
355 | "source": [
356 | "round(8.63,1) #rounds till first decimal point"
357 | ]
358 | },
359 | {
360 | "cell_type": "markdown",
361 | "metadata": {},
362 | "source": [
363 | "### Comparison Operators:\n",
364 | "#### Equal: 7==5\n",
365 | "#### Not Equal: 7!=5\n",
366 | "#### Greater than: 7>5\n",
367 | "#### Less than: 7<5 \n",
368 | "#### Greater or Equal: 7>=5\n",
369 | "#### Less or Equal: 7<=5"
370 | ]
371 | },
372 | {
373 | "cell_type": "code",
374 | "execution_count": 30,
375 | "metadata": {},
376 | "outputs": [
377 | {
378 | "name": "stdout",
379 | "output_type": "stream",
380 | "text": [
381 | "False\n"
382 | ]
383 | }
384 | ],
385 | "source": [
386 | "num_1 = 7\n",
387 | "num_2 = 5\n",
388 | "print(num_1 == num_2)"
389 | ]
390 | },
391 | {
392 | "cell_type": "code",
393 | "execution_count": null,
394 | "metadata": {},
395 | "outputs": [],
396 | "source": []
397 | }
398 | ],
399 | "metadata": {
400 | "kernelspec": {
401 | "display_name": "Python 3",
402 | "language": "python",
403 | "name": "python3"
404 | },
405 | "language_info": {
406 | "codemirror_mode": {
407 | "name": "ipython",
408 | "version": 3
409 | },
410 | "file_extension": ".py",
411 | "mimetype": "text/x-python",
412 | "name": "python",
413 | "nbconvert_exporter": "python",
414 | "pygments_lexer": "ipython3",
415 | "version": "3.7.3"
416 | }
417 | },
418 | "nbformat": 4,
419 | "nbformat_minor": 2
420 | }
421 |
--------------------------------------------------------------------------------
/Cyber Security Bootcamp/Day_3_Loops_if&else_Functions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Control Flow:\n",
8 | "#### Welcome to this lesson on Control Flow! Control flow is the sequence in which your code is run helping you in decision making.
Here, we'll learn about several tools in Python we can use to affect our code's control flow:\n",
9 | "\n",
10 | "- Conditional Statements\n",
11 | "- Boolean Expressions\n",
12 | "- For and While Loops\n",
13 | "- Break and Continue\n",
14 | "- Zip and Enumerate\n",
15 | "- List Comprehensions"
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {},
21 | "source": [
22 | "### Comparisons operators:\n",
23 | "- Equal: ==\n",
24 | "- Not Equal: !=\n",
25 | "- Greater than: >\n",
26 | "- Less than: <\n",
27 | "- Greater or equal: >=\n",
28 | "- Less or equal: <=\n",
29 | "- Object Identity: is - Checks weather two elements are same object and memory\n"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": 1,
35 | "metadata": {},
36 | "outputs": [
37 | {
38 | "name": "stdout",
39 | "output_type": "stream",
40 | "text": [
41 | "Language is Java\n"
42 | ]
43 | }
44 | ],
45 | "source": [
46 | "lang = 'Java'\n",
47 | "\n",
48 | "if lang == 'Python':\n",
49 | " print('Language is Python')\n",
50 | "elif lang == 'Java':\n",
51 | " print('Language is Java')\n",
52 | "else:\n",
53 | " print('No match')"
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": 2,
59 | "metadata": {},
60 | "outputs": [
61 | {
62 | "name": "stdout",
63 | "output_type": "stream",
64 | "text": [
65 | "Bad creds\n"
66 | ]
67 | }
68 | ],
69 | "source": [
70 | "#and,or & not:\n",
71 | "\n",
72 | "user = 'Admin'\n",
73 | "logged_in = False\n",
74 | "\n",
75 | "if user == 'Admin' and logged_in:\n",
76 | " print('Admin page')\n",
77 | "else:\n",
78 | " print('Bad creds')"
79 | ]
80 | },
81 | {
82 | "cell_type": "code",
83 | "execution_count": 3,
84 | "metadata": {},
85 | "outputs": [
86 | {
87 | "name": "stdout",
88 | "output_type": "stream",
89 | "text": [
90 | "Admin page\n"
91 | ]
92 | }
93 | ],
94 | "source": [
95 | "user = 'Admin'\n",
96 | "logged_in = True\n",
97 | "\n",
98 | "if user == 'Admin' or logged_in:\n",
99 | " print('Admin page')\n",
100 | "else:\n",
101 | " print('Bad creds')"
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": 5,
107 | "metadata": {},
108 | "outputs": [
109 | {
110 | "name": "stdout",
111 | "output_type": "stream",
112 | "text": [
113 | "True\n"
114 | ]
115 | }
116 | ],
117 | "source": [
118 | "a = [1,2,3]\n",
119 | "b = [1,2,3]\n",
120 | "\n",
121 | "print(a == b)"
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": 6,
127 | "metadata": {},
128 | "outputs": [
129 | {
130 | "name": "stdout",
131 | "output_type": "stream",
132 | "text": [
133 | "2466803111560\n",
134 | "2466803109960\n",
135 | "False\n",
136 | "False\n"
137 | ]
138 | }
139 | ],
140 | "source": [
141 | "a = [1,2,3]\n",
142 | "b = [1,2,3]\n",
143 | "\n",
144 | "print(id(a))\n",
145 | "print(id(b))\n",
146 | "print(id(a)==id(b))\n",
147 | "print(a is b)"
148 | ]
149 | },
150 | {
151 | "cell_type": "markdown",
152 | "metadata": {},
153 | "source": [
154 | "#### False values:
\n",
155 | "False
\n",
156 | "None
\n",
157 | "Numerical Zero
\n",
158 | "Any empty sequence. Eg: '', (), []
\n",
159 | "Any empty mapping. Eg: {}
\n"
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": 8,
165 | "metadata": {},
166 | "outputs": [
167 | {
168 | "name": "stdout",
169 | "output_type": "stream",
170 | "text": [
171 | "Evaluated to True\n"
172 | ]
173 | }
174 | ],
175 | "source": [
176 | "condition = 2\n",
177 | "\n",
178 | "if condition:\n",
179 | " print('Evaluated to True')\n",
180 | "else:\n",
181 | " print('Evaluated to False')"
182 | ]
183 | },
184 | {
185 | "cell_type": "markdown",
186 | "metadata": {},
187 | "source": [
188 | "### Loops & Iterations - for/while loops:"
189 | ]
190 | },
191 | {
192 | "cell_type": "code",
193 | "execution_count": 9,
194 | "metadata": {},
195 | "outputs": [],
196 | "source": [
197 | "nums = [1,2,3,4,5]"
198 | ]
199 | },
200 | {
201 | "cell_type": "code",
202 | "execution_count": 10,
203 | "metadata": {},
204 | "outputs": [
205 | {
206 | "name": "stdout",
207 | "output_type": "stream",
208 | "text": [
209 | "1\n",
210 | "2\n",
211 | "3\n",
212 | "4\n",
213 | "5\n"
214 | ]
215 | }
216 | ],
217 | "source": [
218 | "for num in nums:\n",
219 | " print(num)\n",
220 | "\n",
221 | "#Iterates and returns each item using loop"
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "execution_count": 11,
227 | "metadata": {},
228 | "outputs": [
229 | {
230 | "name": "stdout",
231 | "output_type": "stream",
232 | "text": [
233 | "1\n",
234 | "2\n",
235 | "Found!\n"
236 | ]
237 | }
238 | ],
239 | "source": [
240 | "for num in nums:\n",
241 | " if num == 3:\n",
242 | " print('Found!')\n",
243 | " break\n",
244 | " print(num)\n",
245 | " \n",
246 | "#Breaks out of loop if the item is found"
247 | ]
248 | },
249 | {
250 | "cell_type": "code",
251 | "execution_count": null,
252 | "metadata": {},
253 | "outputs": [],
254 | "source": []
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": 12,
259 | "metadata": {},
260 | "outputs": [
261 | {
262 | "name": "stdout",
263 | "output_type": "stream",
264 | "text": [
265 | "1\n",
266 | "2\n",
267 | "Found!\n",
268 | "4\n",
269 | "5\n"
270 | ]
271 | }
272 | ],
273 | "source": [
274 | "for num in nums:\n",
275 | " if num == 3:\n",
276 | " print('Found!')\n",
277 | " continue\n",
278 | " print(num)\n",
279 | " \n",
280 | "#Breaks out of loop if the item is found"
281 | ]
282 | },
283 | {
284 | "cell_type": "code",
285 | "execution_count": 13,
286 | "metadata": {},
287 | "outputs": [
288 | {
289 | "name": "stdout",
290 | "output_type": "stream",
291 | "text": [
292 | "1 a\n",
293 | "1 b\n",
294 | "1 c\n",
295 | "2 a\n",
296 | "2 b\n",
297 | "2 c\n",
298 | "3 a\n",
299 | "3 b\n",
300 | "3 c\n",
301 | "4 a\n",
302 | "4 b\n",
303 | "4 c\n",
304 | "5 a\n",
305 | "5 b\n",
306 | "5 c\n"
307 | ]
308 | }
309 | ],
310 | "source": [
311 | "for num in nums:\n",
312 | " for letter in 'abc':\n",
313 | " print(num,letter)\n",
314 | "\n",
315 | "#Nested loop - Loop within a loop"
316 | ]
317 | },
318 | {
319 | "cell_type": "code",
320 | "execution_count": 14,
321 | "metadata": {},
322 | "outputs": [
323 | {
324 | "name": "stdout",
325 | "output_type": "stream",
326 | "text": [
327 | "0\n",
328 | "1\n",
329 | "2\n",
330 | "3\n",
331 | "4\n",
332 | "5\n",
333 | "6\n",
334 | "7\n",
335 | "8\n",
336 | "9\n"
337 | ]
338 | }
339 | ],
340 | "source": [
341 | "for i in range(10):\n",
342 | " print(i)\n",
343 | "\n",
344 | "#prints 0-9"
345 | ]
346 | },
347 | {
348 | "cell_type": "code",
349 | "execution_count": 16,
350 | "metadata": {},
351 | "outputs": [
352 | {
353 | "name": "stdout",
354 | "output_type": "stream",
355 | "text": [
356 | "1\n",
357 | "2\n",
358 | "3\n",
359 | "4\n",
360 | "5\n",
361 | "6\n",
362 | "7\n",
363 | "8\n",
364 | "9\n",
365 | "10\n"
366 | ]
367 | }
368 | ],
369 | "source": [
370 | "for i in range(1,11):\n",
371 | " print(i)\n",
372 | "#prints 1-10, taking one step at a moment"
373 | ]
374 | },
375 | {
376 | "cell_type": "code",
377 | "execution_count": 17,
378 | "metadata": {},
379 | "outputs": [
380 | {
381 | "name": "stdout",
382 | "output_type": "stream",
383 | "text": [
384 | "1\n",
385 | "3\n",
386 | "5\n",
387 | "7\n",
388 | "9\n",
389 | "11\n",
390 | "13\n",
391 | "15\n",
392 | "17\n",
393 | "19\n"
394 | ]
395 | }
396 | ],
397 | "source": [
398 | "for i in range(1,21,2):\n",
399 | " print(i)\n",
400 | "#prints 1-19, skipping a value after each iteration"
401 | ]
402 | },
403 | {
404 | "cell_type": "code",
405 | "execution_count": 19,
406 | "metadata": {},
407 | "outputs": [
408 | {
409 | "name": "stdout",
410 | "output_type": "stream",
411 | "text": [
412 | "0\n",
413 | "1\n",
414 | "2\n",
415 | "3\n",
416 | "4\n",
417 | "5\n",
418 | "6\n",
419 | "7\n",
420 | "8\n",
421 | "9\n"
422 | ]
423 | }
424 | ],
425 | "source": [
426 | "### While loop:\n",
427 | "x = 0\n",
428 | "\n",
429 | "while x < 10:\n",
430 | " print(x)\n",
431 | " x += 1\n",
432 | "\n",
433 | "''' \n",
434 | "while \n",
435 | " \n"
436 | ]
437 | },
438 | {
439 | "cell_type": "markdown",
440 | "metadata": {},
441 | "source": [
442 | "# Functions:\n"
443 | ]
444 | },
445 | {
446 | "cell_type": "code",
447 | "execution_count": 37,
448 | "metadata": {},
449 | "outputs": [],
450 | "source": [
451 | "def hello_func():\n",
452 | " pass\n",
453 | "\n",
454 | "#pass keywords avoid errors if we want to leave that function for a while \n",
455 | "#Paranthesis () is where we pass the parameters"
456 | ]
457 | },
458 | {
459 | "cell_type": "code",
460 | "execution_count": 38,
461 | "metadata": {},
462 | "outputs": [],
463 | "source": [
464 | "hello_func() # to run the function"
465 | ]
466 | },
467 | {
468 | "cell_type": "code",
469 | "execution_count": 23,
470 | "metadata": {},
471 | "outputs": [
472 | {
473 | "name": "stdout",
474 | "output_type": "stream",
475 | "text": [
476 | "Hello Function!\n"
477 | ]
478 | }
479 | ],
480 | "source": [
481 | "def hello_func():\n",
482 | " print('Hello Function!')\n",
483 | "hello_func()"
484 | ]
485 | },
486 | {
487 | "cell_type": "markdown",
488 | "metadata": {},
489 | "source": [
490 | "#### Functions allows to reuse the code just like variable. \n",
491 | "#### Works as a machine, we give input to get the desirable output"
492 | ]
493 | },
494 | {
495 | "cell_type": "code",
496 | "execution_count": 27,
497 | "metadata": {},
498 | "outputs": [
499 | {
500 | "name": "stdout",
501 | "output_type": "stream",
502 | "text": [
503 | "HELLO FUNCTION!\n"
504 | ]
505 | }
506 | ],
507 | "source": [
508 | "def hello_func():\n",
509 | " return 'Hello Function!'\n",
510 | "hello_func()\n",
511 | "\n",
512 | "#When we executes the function, then it's equivalent to the return value\n",
513 | "#As the return value is a string, then we can use string methods with the function\n",
514 | "\n",
515 | "print(hello_func().upper())"
516 | ]
517 | },
518 | {
519 | "cell_type": "code",
520 | "execution_count": 31,
521 | "metadata": {},
522 | "outputs": [
523 | {
524 | "name": "stdout",
525 | "output_type": "stream",
526 | "text": [
527 | "Hi Function!\n"
528 | ]
529 | }
530 | ],
531 | "source": [
532 | "# Passing an argument/parameter to the function.\n",
533 | "\n",
534 | "def hello_func(greeting):\n",
535 | " return '{} Function!'.format(greeting)\n",
536 | "print(hello_func('Hi'))\n",
537 | "\n",
538 | "# greeting variable doesn't affect any variable outside the function. It's scope is local to this function\n",
539 | "# You can define a variable named greeting, outside the function."
540 | ]
541 | },
542 | {
543 | "cell_type": "code",
544 | "execution_count": 41,
545 | "metadata": {},
546 | "outputs": [
547 | {
548 | "name": "stdout",
549 | "output_type": "stream",
550 | "text": [
551 | "Hi, harsh!\n"
552 | ]
553 | }
554 | ],
555 | "source": [
556 | "#Passing two parameters and default values\n",
557 | "\n",
558 | "def hello_func(greeting,name='You'):\n",
559 | " return '{}, {}!'.format(greeting, name)\n",
560 | "\n",
561 | "print(hello_func('Hi','harsh'))\n",
562 | "\n",
563 | "#You is default value for name parameter"
564 | ]
565 | },
566 | {
567 | "cell_type": "code",
568 | "execution_count": 42,
569 | "metadata": {},
570 | "outputs": [
571 | {
572 | "name": "stdout",
573 | "output_type": "stream",
574 | "text": [
575 | "('Math', 'Art')\n",
576 | "{'name': 'harsh', 'age': '21'}\n",
577 | "None\n"
578 | ]
579 | }
580 | ],
581 | "source": [
582 | "# Use it when you aren't sure about the numbers of arguments(args) and keyword arguments (kwargs)\n",
583 | "# args - arguments\n",
584 | "# kwargs - Keyword arguments\n",
585 | "\n",
586 | "def student_info(*args, **kwargs):\n",
587 | " print(args)\n",
588 | " print(kwargs)\n",
589 | "\n",
590 | "print(student_info('Math', 'Art', name = 'harsh', age = '21'))"
591 | ]
592 | },
593 | {
594 | "cell_type": "code",
595 | "execution_count": 46,
596 | "metadata": {},
597 | "outputs": [
598 | {
599 | "name": "stdout",
600 | "output_type": "stream",
601 | "text": [
602 | "(['Math', 'Art'], {'name': 'harsh', 'age': '21'})\n",
603 | "{}\n",
604 | "None\n"
605 | ]
606 | }
607 | ],
608 | "source": [
609 | "# Use it when you aren't sure about the numbers of arguments(args) and keyword arguments (kwargs)\n",
610 | "# args - arguments\n",
611 | "# kwargs - Keyword arguments\n",
612 | "\n",
613 | "def student_info(*args, **kwargs):\n",
614 | " print(args)\n",
615 | " print(kwargs)\n",
616 | "\n",
617 | "courses = ['Math','Art']\n",
618 | "info = {'name':'harsh', 'age':'21'}\n",
619 | "\n",
620 | "print(student_info(courses,info))"
621 | ]
622 | },
623 | {
624 | "cell_type": "markdown",
625 | "metadata": {},
626 | "source": [
627 | "#### * & ** is added to differentiate b/w arguments and keyword arguments"
628 | ]
629 | },
630 | {
631 | "cell_type": "markdown",
632 | "metadata": {},
633 | "source": [
634 | "### Let's put everything together and create a program to check if a year is leap or not "
635 | ]
636 | },
637 | {
638 | "cell_type": "code",
639 | "execution_count": 1,
640 | "metadata": {},
641 | "outputs": [
642 | {
643 | "name": "stdout",
644 | "output_type": "stream",
645 | "text": [
646 | "29\n"
647 | ]
648 | }
649 | ],
650 | "source": [
651 | "def month_ui(year,month):\n",
652 | "\n",
653 | " leap=0\n",
654 | " if year % 400 == 0:\n",
655 | " leap=1\n",
656 | " elif year % 100 == 0:\n",
657 | " leap = 0\n",
658 | " elif year % 4 == 0:\n",
659 | " leap = 1\n",
660 | " if month == 2:\n",
661 | " return 28 + leap\n",
662 | " month_list = [1,3,5,7,8,10,12]\n",
663 | " if month in month_list:\n",
664 | " return 31\n",
665 | " else:\n",
666 | " return 30\n",
667 | "\n",
668 | "\n",
669 | "\n",
670 | "year = 2020\n",
671 | "month = 2\n",
672 | "print(month_ui(year,month))"
673 | ]
674 | },
675 | {
676 | "cell_type": "code",
677 | "execution_count": 48,
678 | "metadata": {},
679 | "outputs": [],
680 | "source": []
681 | },
682 | {
683 | "cell_type": "code",
684 | "execution_count": null,
685 | "metadata": {},
686 | "outputs": [],
687 | "source": []
688 | },
689 | {
690 | "cell_type": "code",
691 | "execution_count": null,
692 | "metadata": {},
693 | "outputs": [],
694 | "source": []
695 | }
696 | ],
697 | "metadata": {
698 | "kernelspec": {
699 | "display_name": "Python 3",
700 | "language": "python",
701 | "name": "python3"
702 | },
703 | "language_info": {
704 | "codemirror_mode": {
705 | "name": "ipython",
706 | "version": 3
707 | },
708 | "file_extension": ".py",
709 | "mimetype": "text/x-python",
710 | "name": "python",
711 | "nbconvert_exporter": "python",
712 | "pygments_lexer": "ipython3",
713 | "version": "3.7.3"
714 | }
715 | },
716 | "nbformat": 4,
717 | "nbformat_minor": 2
718 | }
719 |
--------------------------------------------------------------------------------
/Cyber Security Bootcamp/Day _2_Boolean_list_tuple_sets_dict_cysec.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "LIST:
\n"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "subjects = ['History', 'Math', 'Physics', 'CS']"
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": 2,
22 | "metadata": {},
23 | "outputs": [
24 | {
25 | "name": "stdout",
26 | "output_type": "stream",
27 | "text": [
28 | "['History', 'Math', 'Physics', 'CS']\n"
29 | ]
30 | }
31 | ],
32 | "source": [
33 | "print(subjects) \n",
34 | "#Prints all the elements of the list"
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": 3,
40 | "metadata": {},
41 | "outputs": [
42 | {
43 | "name": "stdout",
44 | "output_type": "stream",
45 | "text": [
46 | "4\n"
47 | ]
48 | }
49 | ],
50 | "source": [
51 | "print(len(subjects))\n",
52 | "#Prints number of elements in list"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 4,
58 | "metadata": {},
59 | "outputs": [
60 | {
61 | "name": "stdout",
62 | "output_type": "stream",
63 | "text": [
64 | "History\n"
65 | ]
66 | }
67 | ],
68 | "source": [
69 | "print(subjects[0])\n",
70 | "#Prints the 1st element (History)"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": 5,
76 | "metadata": {},
77 | "outputs": [
78 | {
79 | "name": "stdout",
80 | "output_type": "stream",
81 | "text": [
82 | "CS\n"
83 | ]
84 | }
85 | ],
86 | "source": [
87 | "print(subjects[-1])\n",
88 | "#Prints the last element (History)"
89 | ]
90 | },
91 | {
92 | "cell_type": "code",
93 | "execution_count": 6,
94 | "metadata": {},
95 | "outputs": [
96 | {
97 | "name": "stdout",
98 | "output_type": "stream",
99 | "text": [
100 | "['History', 'Math']\n",
101 | "['History', 'Math']\n"
102 | ]
103 | }
104 | ],
105 | "source": [
106 | "print(subjects[:2])\n",
107 | "#Prints the 1st & 2nd element (History)\n",
108 | "print(subjects[:2])"
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": 7,
114 | "metadata": {},
115 | "outputs": [
116 | {
117 | "name": "stdout",
118 | "output_type": "stream",
119 | "text": [
120 | "['History', 'Math', 'Physics', 'CS', 'DS']\n"
121 | ]
122 | }
123 | ],
124 | "source": [
125 | "subjects.append('DS')\n",
126 | "#Adds the item at the end of the list\n",
127 | "print(subjects)"
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": 8,
133 | "metadata": {},
134 | "outputs": [
135 | {
136 | "name": "stdout",
137 | "output_type": "stream",
138 | "text": [
139 | "['DBMS', 'History', 'Math', 'Physics', 'CS', 'DS']\n"
140 | ]
141 | }
142 | ],
143 | "source": [
144 | "subjects.insert(0,'DBMS')\n",
145 | "#Adds the item at index 0\n",
146 | "print(subjects)"
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": 9,
152 | "metadata": {},
153 | "outputs": [],
154 | "source": [
155 | "subjects_2 = ['Art', 'Design', 'Chemistry']"
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": 10,
161 | "metadata": {},
162 | "outputs": [],
163 | "source": [
164 | "#subjects.insert(0,subjects_2)\n",
165 | "#print(subjects)\n",
166 | "#Let's try to add elements of subject_2 to subjects\n",
167 | "#it got added as a seperate list in element 0, rather than splitting the items in subject_2 then adding them"
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": 11,
173 | "metadata": {},
174 | "outputs": [
175 | {
176 | "name": "stdout",
177 | "output_type": "stream",
178 | "text": [
179 | "['DBMS', 'History', 'Math', 'Physics', 'CS', 'DS', 'Art', 'Design', 'Chemistry']\n"
180 | ]
181 | }
182 | ],
183 | "source": [
184 | "subjects.extend(subjects_2)\n",
185 | "print(subjects)\n",
186 | "#Adds each element seperately to subject_2 at the end of the list"
187 | ]
188 | },
189 | {
190 | "cell_type": "code",
191 | "execution_count": 12,
192 | "metadata": {},
193 | "outputs": [
194 | {
195 | "name": "stdout",
196 | "output_type": "stream",
197 | "text": [
198 | "['DBMS', 'History', 'Physics', 'CS', 'DS', 'Art', 'Design', 'Chemistry']\n"
199 | ]
200 | }
201 | ],
202 | "source": [
203 | "subjects.remove('Math')\n",
204 | "#Finds & Removes Math from the list\n",
205 | "print(subjects)"
206 | ]
207 | },
208 | {
209 | "cell_type": "code",
210 | "execution_count": 13,
211 | "metadata": {},
212 | "outputs": [
213 | {
214 | "name": "stdout",
215 | "output_type": "stream",
216 | "text": [
217 | "['DBMS', 'History', 'Physics', 'CS', 'DS', 'Art', 'Design']\n",
218 | "Chemistry\n"
219 | ]
220 | }
221 | ],
222 | "source": [
223 | "popped = subjects.pop()\n",
224 | "#Removes the last item in the list\n",
225 | "\n",
226 | "print(subjects)\n",
227 | "\n",
228 | "print(popped)\n",
229 | "#Grabs the popped item"
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "execution_count": 14,
235 | "metadata": {},
236 | "outputs": [
237 | {
238 | "name": "stdout",
239 | "output_type": "stream",
240 | "text": [
241 | "['Design', 'Art', 'DS', 'CS', 'Physics', 'History', 'DBMS']\n"
242 | ]
243 | }
244 | ],
245 | "source": [
246 | "subjects.reverse()\n",
247 | "#Reverses the list\n",
248 | "print(subjects)"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": 15,
254 | "metadata": {},
255 | "outputs": [
256 | {
257 | "name": "stdout",
258 | "output_type": "stream",
259 | "text": [
260 | "['Art', 'CS', 'DBMS', 'DS', 'Design', 'History', 'Physics']\n",
261 | "[1, 2, 3, 4, 5, 6]\n"
262 | ]
263 | }
264 | ],
265 | "source": [
266 | "subjects.sort()\n",
267 | "#Sorts the list in alphabetical order\n",
268 | "print(subjects)\n",
269 | "\n",
270 | "num_list = [2,6,4,5,3,1]\n",
271 | "num_list.sort()\n",
272 | "print(num_list)"
273 | ]
274 | },
275 | {
276 | "cell_type": "code",
277 | "execution_count": 16,
278 | "metadata": {},
279 | "outputs": [
280 | {
281 | "name": "stdout",
282 | "output_type": "stream",
283 | "text": [
284 | "1\n",
285 | "6\n",
286 | "21\n"
287 | ]
288 | }
289 | ],
290 | "source": [
291 | "print(min(num_list))\n",
292 | "print(max(num_list))\n",
293 | "print(sum(num_list))"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": 17,
299 | "metadata": {},
300 | "outputs": [
301 | {
302 | "name": "stdout",
303 | "output_type": "stream",
304 | "text": [
305 | "1\n"
306 | ]
307 | }
308 | ],
309 | "source": [
310 | "print(subjects.index('CS'))\n",
311 | "#Returns the index value of the item"
312 | ]
313 | },
314 | {
315 | "cell_type": "code",
316 | "execution_count": 18,
317 | "metadata": {},
318 | "outputs": [
319 | {
320 | "name": "stdout",
321 | "output_type": "stream",
322 | "text": [
323 | "True\n"
324 | ]
325 | }
326 | ],
327 | "source": [
328 | "print('Art' in subjects)\n",
329 | "#Checks weather the item is present in the list or not"
330 | ]
331 | },
332 | {
333 | "cell_type": "code",
334 | "execution_count": 19,
335 | "metadata": {},
336 | "outputs": [
337 | {
338 | "name": "stdout",
339 | "output_type": "stream",
340 | "text": [
341 | "Art\n",
342 | "CS\n",
343 | "DBMS\n",
344 | "DS\n",
345 | "Design\n",
346 | "History\n",
347 | "Physics\n"
348 | ]
349 | }
350 | ],
351 | "source": [
352 | "for item in subjects:\n",
353 | " print(item)\n",
354 | "#Prints each item in new line "
355 | ]
356 | },
357 | {
358 | "cell_type": "code",
359 | "execution_count": 20,
360 | "metadata": {},
361 | "outputs": [
362 | {
363 | "name": "stdout",
364 | "output_type": "stream",
365 | "text": [
366 | "0 Art\n",
367 | "1 CS\n",
368 | "2 DBMS\n",
369 | "3 DS\n",
370 | "4 Design\n",
371 | "5 History\n",
372 | "6 Physics\n"
373 | ]
374 | }
375 | ],
376 | "source": [
377 | "for index,subject in enumerate(subjects):\n",
378 | " print(index,subject)\n",
379 | "#Print the items along with index numbers starting from 0. this is done by enumerate() function"
380 | ]
381 | },
382 | {
383 | "cell_type": "code",
384 | "execution_count": 21,
385 | "metadata": {},
386 | "outputs": [
387 | {
388 | "name": "stdout",
389 | "output_type": "stream",
390 | "text": [
391 | "1 Art\n",
392 | "2 CS\n",
393 | "3 DBMS\n",
394 | "4 DS\n",
395 | "5 Design\n",
396 | "6 History\n",
397 | "7 Physics\n"
398 | ]
399 | }
400 | ],
401 | "source": [
402 | "for index,subject in enumerate(subjects,start=1):\n",
403 | " print(index, subject)\n",
404 | "#Print the items along with index numbers starting from 1. this is done by enumerate() function"
405 | ]
406 | },
407 | {
408 | "cell_type": "code",
409 | "execution_count": 22,
410 | "metadata": {},
411 | "outputs": [
412 | {
413 | "name": "stdout",
414 | "output_type": "stream",
415 | "text": [
416 | "Art - CS - DBMS - DS - Design - History - Physics\n"
417 | ]
418 | }
419 | ],
420 | "source": [
421 | "subject_str = ' - '.join(subjects)\n",
422 | "print(subject_str)\n",
423 | "#turns list into '-' seperated string"
424 | ]
425 | },
426 | {
427 | "cell_type": "code",
428 | "execution_count": 23,
429 | "metadata": {},
430 | "outputs": [
431 | {
432 | "name": "stdout",
433 | "output_type": "stream",
434 | "text": [
435 | "['Art ', ' CS ', ' DBMS ', ' DS ', ' Design ', ' History ', ' Physics']\n"
436 | ]
437 | }
438 | ],
439 | "source": [
440 | "new_list = subject_str.split('-')\n",
441 | "print(new_list)\n",
442 | "#Creates the list, just reverse of .join()"
443 | ]
444 | },
445 | {
446 | "cell_type": "markdown",
447 | "metadata": {},
448 | "source": [
449 | "\n",
450 | "\n",
451 | "Tuple:
\n",
452 | "Doesn't have much supported functions as of list because tuple is immutable"
453 | ]
454 | },
455 | {
456 | "cell_type": "code",
457 | "execution_count": 24,
458 | "metadata": {},
459 | "outputs": [],
460 | "source": [
461 | "tup_1 = ('History', 'Math', 'Physics', 'CS')"
462 | ]
463 | },
464 | {
465 | "cell_type": "code",
466 | "execution_count": 25,
467 | "metadata": {},
468 | "outputs": [
469 | {
470 | "ename": "TypeError",
471 | "evalue": "'tuple' object does not support item assignment",
472 | "output_type": "error",
473 | "traceback": [
474 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
475 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
476 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtup_1\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'Art'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
477 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
478 | ]
479 | }
480 | ],
481 | "source": [
482 | "tup_1[0] = 'Art'"
483 | ]
484 | },
485 | {
486 | "cell_type": "code",
487 | "execution_count": 26,
488 | "metadata": {},
489 | "outputs": [
490 | {
491 | "name": "stdout",
492 | "output_type": "stream",
493 | "text": [
494 | "('History', 'Math', 'Physics', 'CS')\n"
495 | ]
496 | }
497 | ],
498 | "source": [
499 | "print(tup_1)"
500 | ]
501 | },
502 | {
503 | "cell_type": "code",
504 | "execution_count": 27,
505 | "metadata": {},
506 | "outputs": [],
507 | "source": [
508 | "#Only supports that functions that doesn't mutate the list"
509 | ]
510 | },
511 | {
512 | "cell_type": "markdown",
513 | "metadata": {},
514 | "source": [
515 | "\n",
516 | "\n",
517 | "\n",
518 | "SETS:
\n",
519 | "Doesn't care about order & dumps away duplicate item\n"
520 | ]
521 | },
522 | {
523 | "cell_type": "code",
524 | "execution_count": 28,
525 | "metadata": {},
526 | "outputs": [],
527 | "source": [
528 | "cs_courses = {'DS','DBMS','History','Math','Physics'}"
529 | ]
530 | },
531 | {
532 | "cell_type": "code",
533 | "execution_count": 29,
534 | "metadata": {},
535 | "outputs": [],
536 | "source": [
537 | "#Changes order every time the code executes"
538 | ]
539 | },
540 | {
541 | "cell_type": "code",
542 | "execution_count": 30,
543 | "metadata": {},
544 | "outputs": [
545 | {
546 | "name": "stdout",
547 | "output_type": "stream",
548 | "text": [
549 | "True\n"
550 | ]
551 | }
552 | ],
553 | "source": [
554 | "print('Math' in cs_courses)\n",
555 | "#Checks membership in the set"
556 | ]
557 | },
558 | {
559 | "cell_type": "code",
560 | "execution_count": 31,
561 | "metadata": {},
562 | "outputs": [],
563 | "source": [
564 | "art_courses = {'History','Math','Art','Design'}"
565 | ]
566 | },
567 | {
568 | "cell_type": "code",
569 | "execution_count": 32,
570 | "metadata": {},
571 | "outputs": [
572 | {
573 | "name": "stdout",
574 | "output_type": "stream",
575 | "text": [
576 | "{'Math', 'History'}\n"
577 | ]
578 | }
579 | ],
580 | "source": [
581 | "print(cs_courses.intersection(art_courses))\n",
582 | "#Returns common items in both sets"
583 | ]
584 | },
585 | {
586 | "cell_type": "code",
587 | "execution_count": 33,
588 | "metadata": {},
589 | "outputs": [
590 | {
591 | "name": "stdout",
592 | "output_type": "stream",
593 | "text": [
594 | "{'DS', 'DBMS', 'Physics'}\n"
595 | ]
596 | }
597 | ],
598 | "source": [
599 | "print(cs_courses.difference(art_courses))\n",
600 | "\n",
601 | "#Returns the items not present in second set"
602 | ]
603 | },
604 | {
605 | "cell_type": "code",
606 | "execution_count": 34,
607 | "metadata": {},
608 | "outputs": [
609 | {
610 | "name": "stdout",
611 | "output_type": "stream",
612 | "text": [
613 | "{'Math', 'History', 'Physics', 'DS', 'Design', 'Art', 'DBMS'}\n"
614 | ]
615 | }
616 | ],
617 | "source": [
618 | "print(cs_courses.union(art_courses))\n",
619 | "\n",
620 | "#Prints all the items from both sets dropping the duplicate items in a new set"
621 | ]
622 | },
623 | {
624 | "cell_type": "code",
625 | "execution_count": 35,
626 | "metadata": {},
627 | "outputs": [],
628 | "source": [
629 | "#Empty List\n",
630 | "empty_list = []\n",
631 | "empty_list = list()"
632 | ]
633 | },
634 | {
635 | "cell_type": "code",
636 | "execution_count": 36,
637 | "metadata": {},
638 | "outputs": [],
639 | "source": [
640 | "#Empty Tuples\n",
641 | "empty_tuple = ()\n",
642 | "empty_tuple = tuple()"
643 | ]
644 | },
645 | {
646 | "cell_type": "code",
647 | "execution_count": 37,
648 | "metadata": {},
649 | "outputs": [],
650 | "source": [
651 | "#Empty sets\n",
652 | "empty_set = {} #This will create an empty dictionary instead of empty sets\n",
653 | "empty_set = set()"
654 | ]
655 | },
656 | {
657 | "cell_type": "markdown",
658 | "metadata": {},
659 | "source": [
660 | "\n",
661 | "\n",
662 | "\n",
663 | "# Dictionary:-\n",
664 | "\n",
665 | "#### Playing with Key:Value pairs"
666 | ]
667 | },
668 | {
669 | "cell_type": "code",
670 | "execution_count": 39,
671 | "metadata": {},
672 | "outputs": [],
673 | "source": [
674 | "student = {'name':'harsh', 'age':25, 'course':['Math','Physics']}"
675 | ]
676 | },
677 | {
678 | "cell_type": "code",
679 | "execution_count": 40,
680 | "metadata": {},
681 | "outputs": [
682 | {
683 | "name": "stdout",
684 | "output_type": "stream",
685 | "text": [
686 | "{'name': 'harsh', 'age': 25, 'course': ['Math', 'Physics']}\n"
687 | ]
688 | }
689 | ],
690 | "source": [
691 | "print(student) \n",
692 | "#prints dictionary with keys & values"
693 | ]
694 | },
695 | {
696 | "cell_type": "code",
697 | "execution_count": 41,
698 | "metadata": {},
699 | "outputs": [
700 | {
701 | "name": "stdout",
702 | "output_type": "stream",
703 | "text": [
704 | "harsh\n"
705 | ]
706 | }
707 | ],
708 | "source": [
709 | "print(student['name'])\n",
710 | "#Prints values of specified key & gives error on the absence of the key"
711 | ]
712 | },
713 | {
714 | "cell_type": "code",
715 | "execution_count": 42,
716 | "metadata": {},
717 | "outputs": [
718 | {
719 | "name": "stdout",
720 | "output_type": "stream",
721 | "text": [
722 | "None\n"
723 | ]
724 | }
725 | ],
726 | "source": [
727 | "print(student.get('phone'))\n",
728 | "#Prints values of specified key & doesn't gives error on the absence of the key"
729 | ]
730 | },
731 | {
732 | "cell_type": "code",
733 | "execution_count": 43,
734 | "metadata": {},
735 | "outputs": [],
736 | "source": [
737 | "#Let's add a phone no to our dictionary\n",
738 | "student['phone'] = '999-9999'"
739 | ]
740 | },
741 | {
742 | "cell_type": "code",
743 | "execution_count": 45,
744 | "metadata": {},
745 | "outputs": [
746 | {
747 | "name": "stdout",
748 | "output_type": "stream",
749 | "text": [
750 | "{'name': 'akshit', 'age': 25, 'course': ['Math', 'Physics'], 'phone': '999-9999'}\n"
751 | ]
752 | }
753 | ],
754 | "source": [
755 | "student['name'] = 'akshit'\n",
756 | "print(student)\n",
757 | "#update a key\n"
758 | ]
759 | },
760 | {
761 | "cell_type": "code",
762 | "execution_count": 47,
763 | "metadata": {},
764 | "outputs": [
765 | {
766 | "name": "stdout",
767 | "output_type": "stream",
768 | "text": [
769 | "{'name': 'harsh', 'age': 21, 'course': ['Math', 'Physics'], 'phone': '111-1111'}\n"
770 | ]
771 | }
772 | ],
773 | "source": [
774 | "#Updating various keys in a single line of code\n",
775 | "\n",
776 | "student.update({'name':'harsh',\n",
777 | " 'age': 21,\n",
778 | " 'phone': '111-1111'})\n",
779 | "print(student)"
780 | ]
781 | },
782 | {
783 | "cell_type": "code",
784 | "execution_count": 48,
785 | "metadata": {},
786 | "outputs": [
787 | {
788 | "name": "stdout",
789 | "output_type": "stream",
790 | "text": [
791 | "dict_keys(['name', 'age', 'course', 'phone'])\n"
792 | ]
793 | }
794 | ],
795 | "source": [
796 | "#Prints keys from the dictionary\n",
797 | "\n",
798 | "print(student.keys())"
799 | ]
800 | },
801 | {
802 | "cell_type": "code",
803 | "execution_count": 50,
804 | "metadata": {},
805 | "outputs": [
806 | {
807 | "name": "stdout",
808 | "output_type": "stream",
809 | "text": [
810 | "dict_values(['harsh', 21, ['Math', 'Physics'], '111-1111'])\n"
811 | ]
812 | }
813 | ],
814 | "source": [
815 | "#Prints valuess from the dictionary\n",
816 | "\n",
817 | "print(student.values())"
818 | ]
819 | },
820 | {
821 | "cell_type": "code",
822 | "execution_count": 51,
823 | "metadata": {},
824 | "outputs": [
825 | {
826 | "name": "stdout",
827 | "output_type": "stream",
828 | "text": [
829 | "name\n",
830 | "age\n",
831 | "course\n",
832 | "phone\n"
833 | ]
834 | }
835 | ],
836 | "source": [
837 | "#Iterates and prints only keys from the dictionary\n",
838 | "\n",
839 | "for key in student:\n",
840 | " print(key)"
841 | ]
842 | },
843 | {
844 | "cell_type": "code",
845 | "execution_count": null,
846 | "metadata": {},
847 | "outputs": [],
848 | "source": [
849 | "#Iterates and prints only keys from the dictionary\n",
850 | "\n",
851 | "for key, in student:\n",
852 | " print(key)"
853 | ]
854 | }
855 | ],
856 | "metadata": {
857 | "kernelspec": {
858 | "display_name": "Python 3",
859 | "language": "python",
860 | "name": "python3"
861 | },
862 | "language_info": {
863 | "codemirror_mode": {
864 | "name": "ipython",
865 | "version": 3
866 | },
867 | "file_extension": ".py",
868 | "mimetype": "text/x-python",
869 | "name": "python",
870 | "nbconvert_exporter": "python",
871 | "pygments_lexer": "ipython3",
872 | "version": "3.7.3"
873 | }
874 | },
875 | "nbformat": 4,
876 | "nbformat_minor": 2
877 | }
878 |
--------------------------------------------------------------------------------