Notebook file for the more intermediate concepts
parent
49791e2721
commit
f29ecbd58f
|
@ -0,0 +1,637 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Statements"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Money is a social construct.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"loc = 'Bank'\n",
|
||||
"\n",
|
||||
"if loc == 'Auto Shop':\n",
|
||||
" print ('Cars 4 in the making.')\n",
|
||||
"elif loc == 'Bank':\n",
|
||||
" print ('Money is a social construct.')\n",
|
||||
"else: \n",
|
||||
" print ('Where are you now?')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"my_list = [1,2,3,4,5,6,7,8,9]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1 is odd \n",
|
||||
"Number is even: 2\n",
|
||||
"3 is odd \n",
|
||||
"Number is even: 4\n",
|
||||
"5 is odd \n",
|
||||
"Number is even: 6\n",
|
||||
"7 is odd \n",
|
||||
"Number is even: 8\n",
|
||||
"9 is odd \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for num in my_list:\n",
|
||||
" # Check if even\n",
|
||||
" if num % 2 == 0:\n",
|
||||
" print (f'Number is even: {num}')\n",
|
||||
" else:\n",
|
||||
" print ('{n} is odd '.format(n = num))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"H\n",
|
||||
"e\n",
|
||||
"l\n",
|
||||
"l\n",
|
||||
"o\n",
|
||||
" \n",
|
||||
"W\n",
|
||||
"o\n",
|
||||
"r\n",
|
||||
"l\n",
|
||||
"d\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for letter in 'Hello World':\n",
|
||||
" print (letter)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"(1, 2, 3)\n",
|
||||
"(4, 5, 6)\n",
|
||||
"(7, 8, 9)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"mylist = [(1,2,3),(4,5,6),(7,8,9)]\n",
|
||||
"\n",
|
||||
"for item in mylist:\n",
|
||||
" print (item)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2\n",
|
||||
"5\n",
|
||||
"8\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"#tuple unpacking\n",
|
||||
"\n",
|
||||
"for a,b,c in mylist:\n",
|
||||
" print (b)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)]"
|
||||
]
|
||||
},
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"mylist.append((10,11,12))\n",
|
||||
"mylist"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"k1\n",
|
||||
"k2\n",
|
||||
"k3\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"d = {'k1':1,'k2':2,'k3':3}\n",
|
||||
"\n",
|
||||
"for item in d:\n",
|
||||
" print(item)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"k1\n",
|
||||
"1\n",
|
||||
"k2\n",
|
||||
"2\n",
|
||||
"k3\n",
|
||||
"3\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"#tuple unpacking for dictionaries\n",
|
||||
"\n",
|
||||
"for key,value in d.items():\n",
|
||||
" print (key)\n",
|
||||
" print (value)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1\n",
|
||||
"2\n",
|
||||
"3\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for value in d.values():\n",
|
||||
" print (value)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### while loops"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"The value of x is 0\n",
|
||||
"The value of x is 1\n",
|
||||
"The value of x is 2\n",
|
||||
"\n",
|
||||
"x is greater than 3\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = 0\n",
|
||||
"\n",
|
||||
"while x < 3:\n",
|
||||
" print (f'The value of x is {x}')\n",
|
||||
" x += 1\n",
|
||||
"else:\n",
|
||||
" print ('\\nx is greater than 3')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### break, continue and pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Do nothing at all, placeholder\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"item = [1,2,3]\n",
|
||||
"\n",
|
||||
"for i in item:\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"print ('Do nothing at all, placeholder')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"mystring = 'Mehul'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"M\n",
|
||||
"e\n",
|
||||
"u\n",
|
||||
"l\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# see the difference between continue and break\n",
|
||||
"\n",
|
||||
"for letter in mystring:\n",
|
||||
" if letter == 'h':\n",
|
||||
" continue\n",
|
||||
" print(letter)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"M\n",
|
||||
"e\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for letter in mystring:\n",
|
||||
" if letter == 'h':\n",
|
||||
" break\n",
|
||||
" print(letter)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Useful operators"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[0, 3, 6, 9]"
|
||||
]
|
||||
},
|
||||
"execution_count": 36,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# range\n",
|
||||
"\n",
|
||||
"range_list = list(range(0,11,3))\n",
|
||||
"range_list"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"(0, 'W')\n",
|
||||
"(1, 'a')\n",
|
||||
"(2, 'k')\n",
|
||||
"(3, 'a')\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# enumerate {hint - typle unpacking}\n",
|
||||
"\n",
|
||||
"word = 'Waka'\n",
|
||||
"for item in enumerate(word):\n",
|
||||
" print (item)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 39,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[(1, 'Fra'), (2, 'Bra'), (3, 'Ger'), (4, 'Esp')]"
|
||||
]
|
||||
},
|
||||
"execution_count": 39,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# zip {only zips till the shortest list}\n",
|
||||
"\n",
|
||||
"list1 = [1,2,3,4]\n",
|
||||
"list2 = ['Fra','Bra','Ger','Esp']\n",
|
||||
"\n",
|
||||
"list(zip(list1,list2))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 43,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# random library \n",
|
||||
"\n",
|
||||
"from random import shuffle, randint\n",
|
||||
"\n",
|
||||
"new_list = list(range(10,100,1))\n",
|
||||
"\n",
|
||||
"lowest = min(new_list)\n",
|
||||
"highest = max(new_list)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 44,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"10 99\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print (lowest,highest)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"shuffle(new_list)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 48,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[95, 73, 66, 10, 45]"
|
||||
]
|
||||
},
|
||||
"execution_count": 48,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# display first 5 objects in the list\n",
|
||||
"new_list[0:5]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 49,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"11"
|
||||
]
|
||||
},
|
||||
"execution_count": 49,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"randint(0,40)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 51,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 51,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"43 in new_list"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 52,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"What is your age?24\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# input from user\n",
|
||||
"\n",
|
||||
"result = input('What is your age?')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 53,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"result is 24\n",
|
||||
"type is <class 'str'>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print (f'result is {result}')\n",
|
||||
"print (f'type is {type(result)}')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 55,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"What is your age?24\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"result = int(input('What is your age?'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 57,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"result is 24\n",
|
||||
"type is <class 'int'>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print ('result is {}'.format(result))\n",
|
||||
"print ('type is {}'.format(type(result)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"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.6.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
Loading…
Reference in New Issue