forked from Amarnath-Rao/python-data-viz-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49213b7
commit b16bcf6
Showing
13 changed files
with
7,715 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,330 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Flow Control: If, for loop, while loop" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## If, else, elif" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Basic if statement\n", | ||
"x = 0\n", | ||
"y = 5\n", | ||
"\n", | ||
"if x < y: \n", | ||
" print('yes')\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# use of membership operator for if statement\n", | ||
"if 'ele' in 'Electrical': \n", | ||
" print('yes')\n", | ||
"\n", | ||
"\n", | ||
"if 'item1' in ['item1', 'item2', 'item3']: \n", | ||
" print('item1 is present')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"num = input(\"Enter a number: \") # Taking input from user\n", | ||
"\n", | ||
"#print(type(num)) # The type of input is always string\n", | ||
"\n", | ||
"print(\"Entered number is \" + str(num))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Python program to check if the input number is odd or even.\n", | ||
"# A number is even if division by 2 give a remainder of 0.\n", | ||
"# If remainder is 1, it is odd number.\n", | ||
"\n", | ||
"num = input(\"Enter a number: \")\n", | ||
"\n", | ||
"\n", | ||
"# Fill the blank inside the if statement\n", | ||
"# You will require typecasting and Modulo operator\n", | ||
"\n", | ||
"if ():\n", | ||
" print(\"{0} is Even\".format(num))\n", | ||
"else:\n", | ||
" print(\"{0} is Odd\".format(num))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# use of elif\n", | ||
"# A better way of writing this same code is shown later.\n", | ||
"\n", | ||
"name = 'IIT Bombay'\n", | ||
"\n", | ||
"\n", | ||
"if name == 'IIT Bombay':\n", | ||
" print('Founded in 1958')\n", | ||
" \n", | ||
"elif name == 'IIT Kharagpur':\n", | ||
" print('Founded in 1950')\n", | ||
" \n", | ||
"elif name == 'IIT Kanpur':\n", | ||
" print('Founded in 1959')\n", | ||
" \n", | ||
"elif name == 'IIT Delhi':\n", | ||
" print('Founded in 1961')\n", | ||
" \n", | ||
"elif name == 'IIT Madras':\n", | ||
" print('Founded in 1959')\n", | ||
" \n", | ||
"elif name == 'IIT Guwahati':\n", | ||
" print('Founded in 1994')\n", | ||
" \n", | ||
"else:\n", | ||
" print(\"I don't know when it was founded\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Special:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"raining = True\n", | ||
"print(\"Let's go to the\", 'beach' if not raining else 'library')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"age = 21\n", | ||
"status = 'minor' if age < 27 else 'adult'\n", | ||
"status" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"x = 40\n", | ||
"y = 50\n", | ||
"increment = (1 + x) if x > y else (1 + y)\n", | ||
"increment" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Program to check if the input year is a leap year or not.\n", | ||
"# Fill up the blank to make the program work.\n", | ||
"# You may add more if else statements if you need.\n", | ||
"\n", | ||
"year = int(input(\"Enter a year: \"))\n", | ||
"\n", | ||
"if (year % 4) == 0:\n", | ||
" if():\n", | ||
" pass\n", | ||
" else:\n", | ||
" pass\n", | ||
"else:\n", | ||
" print(\"{0} is not a leap year\".format(year))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Loops - For loop" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"numbers = [1, 2, 3, 40, 50, 6, 7, 80, 9]\n", | ||
"\n", | ||
"# variable to store the sum\n", | ||
"add = 0\n", | ||
"\n", | ||
"# iterate over the list\n", | ||
"for num in numbers:\n", | ||
" add += num # add = add + num\n", | ||
"\n", | ||
"print(\"The sum is\", add)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can generate a sequence of numbers using range( ) function. range(50) will generate numbers from 0 to 49 (50 numbers). We can also define the start, stop and step size as range(start,stop,step size). step size defaults to 1 if not provided." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"print(list(range(0,10)))\n", | ||
"\n", | ||
"print(list(range(5,10)))\n", | ||
"\n", | ||
"print(list(range(0,10,2)))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"numbers = [1, 2, 3, 40, 50, 6, 7, 80, 9]\n", | ||
"\n", | ||
"# variable to store the sum\n", | ||
"add = 0\n", | ||
"\n", | ||
"# iterate over the list\n", | ||
"for i in range(len(numbers)):\n", | ||
" add += numbers[i] # add = add + num\n", | ||
"\n", | ||
"print(\"The sum is\", add)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"A for loop can have an **else** block as well. The else part is executed if the items in the sequence used in for loop exhausts." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"digits = [0, 1, 5]\n", | ||
"\n", | ||
"for i in digits:\n", | ||
" print(i)\n", | ||
"else:\n", | ||
" print(\"No items left.\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Python program to check if the input number is prime or not\n", | ||
"\n", | ||
"num = 79\n", | ||
"\n", | ||
"if num > 1:\n", | ||
" pass\n", | ||
"\n", | ||
"# Write a for loop to check if a number is prime\n", | ||
"\n", | ||
"else:\n", | ||
" print(num,\"is not a prime number\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## While loop" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"counter = 0\n", | ||
"\n", | ||
"while counter < 3:\n", | ||
" print(\"Inside loop\")\n", | ||
" counter = counter + 1\n", | ||
"else:\n", | ||
" print(\"Inside else\")" | ||
] | ||
}, | ||
{ | ||
"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.7.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.