{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Values, expressions and statements\n", "\n", "\"Open\n", "\n", "\n", "- http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html\n", "\n", "## Topics\n", "- data values and types\n", "- variables\n", "- expressions and statements\n", "- numeric operators and basic computations\n", "- order of operations\n", "- standard input and output\n", "- type casting\n", "- Composition and algorithm\n", "\n", "## 2.1 Values and data types\n", "- a value is one of the fundamental things or data -- like a letter or number -- that a program manipulates \n", "- these values are clssified into types\n", "- Python fundamentally supports two major data types: numbers and strings\n", "- Boolean (True/False) is also supported type\n", "\n", "### Numbers\n", "- Integer (int) \n", " - +/- whole numbers: 199, -98, 0\n", "- Float \n", " - +/- real numbers - numbers with decimal points: 99.99, -0.01\n", "\n", "\n", "### Strings\n", "- Python uses **str** abbreviation for String type\n", "- strings are one or more characters represent using single, double or tripple quotes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.2 First program - hello world!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world!\n" ] } ], "source": [ "#----------------------------------------------------------\n", "# hello world program\n", "# by: John Doe\n", "# Jan 1, 2017\n", "# Copyright: Anyone may freely copy or modify this program\n", "#----------------------------------------------------------\n", "print('hello world!') # say hello to the beautiful world!\n", "# In python 2: print is a statement not a function\n", "# print 'Hello World!' " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.3 Comments\n", "- Programming languages provide a way to write comment along with the codes\n", "- Python uses \\# symbol to write a single line comment\n", "- comments are for humans/programmers to convey/explain what the code is doing or supposed to do\n", "- Python interpreter ignores the comments" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.4 Data Types\n", "- built-in type() function can be used to know type of data\n", "- functions will be covered in Chapter 04" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(100)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(-9)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(1000.99345435)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(-2.345)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type('Hello World!')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type('A')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "print(\"hello\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"17\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"\"\"Triple double quoted data\"\"\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type('''Type of Triple single quote data is''')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "a = \"hello\"" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.5 Type conversion/casting\n", "- data needs to be converted from one type to another as needed\n", "- the process is called type casting\n", "- use built-in functions such as str(), int() and float()\n", "- **str(value)** - converts any value into string\n", "- **int(value)** - converts numeric value into int\n", "- **int(value)** - converts numeric value into float" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "data = 'hello' # can't conver it to int or float types" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(data)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = '100'\n", "type(data)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'100'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num = int(data)\n", "type(num)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "price = float('500.99')\n", "type(price)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "could not convert string to float: 'hi'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'hi'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'hi'" ] } ], "source": [ "float('hi')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num = 99.99\n", "strNum = str(num)\n", "type(strNum)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(True)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.6 Statements\n", "- a **statement** is an instruction that the Python interpreter can execute\n", "- we've seen assignment statements so far\n", "- we'll later explore for, if, import, while and other statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.7 Expressions\n", "- an **expression** is a combination of values, variables, operators, and calls to functions\n", "- expressions are evaluated giving a results" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+2" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len('hello')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14\n" ] } ], "source": [ "print(2+3*4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.8 Standard Output\n", "- printing or writing output to common/standard output such as monitor\n", "- way to display the results and interact with the users of your program\n", "- use print() function" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"Oh no\", she exclaimed, \"Ben's bike is broken!\"\n" ] } ], "source": [ "print('''\"Oh no\", she exclaimed, \"Ben's bike is broken!\"''')" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "34.55\n" ] } ], "source": [ "print(34.55)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "print(2+2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.9 Escape Sequences\n", "- some letters or sequence of letters have special meaning to Python\n", "- single, double and tripple single or double quotes represent string data\n", "- use backslash \\\\ to represent these escape sequences, e.g.,\n", " - \\\\n - new line\n", " - \\\\\\\\ - back slash\n", " - \\\\t - tab\n", " - \\\\r - carriage return\n", " - \\\\' - single quote\n", " - \\\\\" - double quote" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What's up\n", " Shaq O'Neal?\n" ] } ], "source": [ "print('What\\'s up\\n Shaq O\\'Neal?')" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello \there...\n", " how are you?\n" ] } ], "source": [ "print('hello \\there...\\n how are you?')" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "how many back slahses will be printeted? \\\\\n" ] } ], "source": [ "print('how many back slahses will be printeted? \\\\\\\\')" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "how many back slahses will be printeted? \\\\\\\\\n" ] } ], "source": [ "print(r'how many back slahses will be printeted? \\\\\\\\')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.10 Variables\n", "- variables are identifiers that are used to store values which can be then easily manipulated\n", "- variables give names to data so the data can be easily referenced by their names over and again\n", "- rules and best practices for creating identifiers and variable names:\n", " - can't be a keyword -- what are the built-in keywords?\n", " - can start with only alphabets or underscore ( _ )\n", " - can't have symbols such as $, %, &, white space, etc.\n", " - can't start with a digit but digits can be used anywhere else in the name\n", " - use camelCase names or use _ for_multi_word_names\n", " - use concise yet meaningul and unambigous names for less typing and avoid typos" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Here is a list of the Python keywords. Enter any keyword to get more help.\n", "\n", "False class from or\n", "None continue global pass\n", "True def if raise\n", "and del import return\n", "as elif in try\n", "assert else is while\n", "async except lambda with\n", "await finally nonlocal yield\n", "break for not \n", "\n" ] } ], "source": [ "help('keywords')" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'x' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# variable must be defined/declared before you can use\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined" ] } ], "source": [ "# variable must be defined/declared before you can use\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "x = 'some value'" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "some value\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "# Exercise: Define a bunch of variables to store some values of different types\n", "var1 = -100\n", "num = -99.99\n", "name = 'John'\n", "lName = 'Smith'\n", "MI = 'A'\n", "grade = 10.5\n", "Name = 'Jake'\n", "grade = 19.9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.11 Dynamic Typing\n", "- type of variables are dynamically evaluated in Python when the assignment statement is executed\n", "- same variable can be uesd to hold different data types" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "var = 100" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "var = 'hello'" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "var = 99.89" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "99.89" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.12 Visualize variable assignments in pythontutor.com\n", "[Click Here](http://pythontutor.com/visualize.html#code=var1%20%3D%20100%0Anum%20%3D%2099.99%0Aname%20%3D%20'John'%0AlName%20%3D%20'Smith'%0AMI%20%3D%20'A'%0Agrade%20%3D%2010.5%0Agrade%20%3D%2019.9%0AName%20%3D%20'Jake'&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.13 Computation - operators and operands\n", "- **operators** are special tokens/symbols that represent computations like addition, multiplication and division \n", "- the values an operator uses are called **operands**\n", "- some binary operators that take two operands\n", " - addition: 10 + 20\n", " - subtraction: 20 - 10 \n", " - true division: 10 / 3\n", " - multiplication: 7 * 9\n", " - integer division: 10 // 3\n", " - remainder or modulus operator: 10 % 2\n", " - power: 2 ** 3" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "# Exercise: play with some examples of various operators supported by Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.14 Order of operations\n", "- depends on the rules of precedence\n", "\n", "**uses PEMDAS rule from high to low order**\n", "\n", "1. Parenthesis\n", "2. Exponentiation\n", "3. Multiplication and Division (left to right)\n", "4. Addition and Subtraction (left to right)\n" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "18\n" ] } ], "source": [ "# some examples\n", "print(2 * 3 ** 2)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-9\n" ] } ], "source": [ "x = 1\n", "y = 2\n", "z = 3\n", "ans = x+y-z*y**y\n", "print(ans)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.15 Operations on strings\n", "- $+$ and $*$ operators also work on strings\n", "- Chapter 08 covers more on string data type and operations" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "JohnSmith\n" ] } ], "source": [ "# some examples\n", "fname = \"John\"\n", "lname = \"Smith\"\n", "fullName = fname + lname\n", "print(fullName)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "gene = \"AGT\"*10" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AGTAGTAGTAGTAGTAGTAGTAGTAGTAGT\n" ] } ], "source": [ "print(gene)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.16 Standard input\n", "- read data from standard or common input such as keyboards\n", "- allows your program to receive data during program execution facilitating user interactions\n", "- input values will have type string even if numbers are entered\n", "- use variables to store the data read from standard input" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What is your name? \n" ] } ], "source": [ "name = input('What is your name? ')" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello, \n" ] } ], "source": [ "print('hello,', name)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number =>\n", "You entered: \n", "type of = \n" ] } ], "source": [ "num = input('Enter a number =>')\n", "print('You entered: ', num)\n", "print('type of', num, '=', type(num))" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: ''", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# str must be casted into int or float depending on the value required\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mnum\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'type of'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'='\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" ] } ], "source": [ "# str must be casted into int or float depending on the value required\n", "num = int(num)\n", "print('type of', num, '=', type(num))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.17 Composition\n", "- break a problem into many smaller sub-problems or steps using high-level algorithm steps\n", "- incrementally build the solution using the sub-problems or steps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.18 Algorithm\n", "- step by step process to solve a given problem\n", " - like a recipe for a food menu\n", "- what are the steps you'd give a martian to buy grocery on earth?\n", " 1. Make a shopping list \n", " - Drive to a grocery store\n", " - Park your car\n", " - Find items in the list\n", " - Checkout\n", " - Load grocery\n", " - Drive home" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.19 Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. area and perimeter of a rectangle\n", "\n", " - write a python script that calculates area and perimeter of a rectangle" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "# Demonstrate composition step by step\n", "# Algorithm steps\n", "# 1. Get length and width of a rectangle\n", "# a. hard coded values OR\n", "# b. prompt user to enter length and width values\n", "# i. convert length and width into right data types\n", "# 2. Find area = length x width\n", "# 3. Find perimeter = 2(length + width)\n", "# 4. Display calculated results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. area and circumference of a circle\n", "- write a python script that calculates area and circumference of a circle\n", "- area of a circle is calculated using equation: $\\pi r^{2}$\n", "- perimeter of a circel is calculated using equation: $2 \\pi r$\n", "- you can use $\\pi=3.14159$" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "# Demonstrate composition step by step\n", "# Algorithm steps" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Body Mass Index (BMI)\n", "- write a python script that calculates BMI of a person\n", "- BMI is body mass in kg divided by the square of body height in meter with the units of $kg/m^2$\n", "- https://www.nhlbi.nih.gov/health/educational/lose_wt/BMI/bmicalc.htm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. area and perimeter of a triangle\n", "- write a python script that finds area and perimeter of a triangle given three sides\n", "- Hint: Google and use Heron's formula to find area of triangle" ] }, { "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }