variables, expressions, and standard functions
DESCRIPTION
Variables, Expressions, and Standard Functions. Topics. Basic calculation Expressions, variables, and operator precedence Data types Input / Output Examples. A calculator. We can use a computer as a calculator. Just type expressions into the Python Shell. Python Shell in Wing IDE. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/1.jpg)
Variables, Variables, Expressions, and Expressions, and
Standard Standard FunctionsFunctions
![Page 2: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/2.jpg)
TopicsTopics•Basic calculation•Expressions, variables, and
operator precedence•Data types•Input / Output•Examples
2
![Page 3: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/3.jpg)
A calculatorA calculator•We can use a computer as a
calculator.•Just type expressions into the
Python Shell
3
Python Shell in Wing IDE
![Page 4: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/4.jpg)
A calculatorA calculator•Try this.
4
>>> 10 * 5
50
>>> 1 + 2 + 3 + 4
10
>>> 1+2+3+4
10
>>> 1 * 4 + 5 ** 2
29
Type it into Python Shell
The answer
Spaces are irrelevant
** is for exponentiation
![Page 5: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/5.jpg)
ExpressionExpression•What we have just typed into
the Python Shell is called expressions.
•After the shell reads each expression, the shell evaluate it and reports the result.
5
![Page 6: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/6.jpg)
Easy calculationEasy calculation•An object moves with the
starting speed of 10 m/s with an acceleration of 2 m/s2. After 5 seconds, how far is the object from its starting position?
6
s = ut + at2/2
10 * 5 + 2 * (5*5) / 2
![Page 7: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/7.jpg)
Operators (1)Operators (1)•In previous examples, we use
many operators such as +, -, or /, to tell Python to perform various computations with the data.
•An operator tells Python what operation to perform with its operands.
7
10 * 5
operator
operands
![Page 8: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/8.jpg)
Operators (2)Operators (2)•Operators can be
Binary operators that work with two operands, e.g., +, -, or *.
5 * 3 10 – 2 15*7
Unary operators that work with a single operand, e.g, –.
-3 +2 -5 * 7
8
![Page 9: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/9.jpg)
Operators (2)Operators (2)• Basic mathematical operators are
shown in the table below
9
Operator Meaning
Examples
+ addition
3+5
- subtraction
4-2
* multiplication
4.5*10
/ division see next page
% modulo see next page
** exponentiation
3**4
![Page 10: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/10.jpg)
Division in PythonDivision in Python•There are two operators
related to division
10
expression
result
4/2 2.0
3/2 1.5
10/7 1.4286
3.0/2 1.5
10/7.0 1.4286
expression
result
4%2 0
3%2 1
10%7 3
3.0%2 1.0
10%7.0 3.0
divisionmodulo – find the remainder
![Page 11: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/11.jpg)
Numbers in Numbers in PythonPython
•There are two types for numbers in Python: integer (type int) and floating points (type float)
11
Integer
expressio
n
Value Floating-
point expr.
Values
10 10 10.0 10.03-2 1 3.0-2 1.019*5 95 19*5.2 98.8
![Page 12: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/12.jpg)
Expressions Values2 + 3 * 6 20(2+3) *6 303/5*2 1.23*5.0/2 7.5
Quick testQuick test
12
![Page 13: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/13.jpg)
Integers v.s. Integers v.s. Floating-pointsFloating-points
• If you write a number without a "dot", it will be treated as an integer.
•Results Every mathematical operation between integer and integer returns an integer, except for division.
Division returns floating-point numbers. Any operations with floating-point numbers return floating-point numbers.
13
![Page 14: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/14.jpg)
3/5*23/5*2•Evaluation is usually done
from left to right
14
((3/5)*2)
( 0.6 *2)
![Page 15: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/15.jpg)
Operator Operator precedenceprecedence
•But operators have different precedence, e.g., * or / have higher precedence over + or -.
15
2+3*6
2+(3*6)
![Page 16: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/16.jpg)
This is just….This is just….•High-school
math!
16
![Page 17: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/17.jpg)
Operator Operator precedenceprecedence
•Evaluation order is from left-to-right for operators with the same precedence, except **.
17
Operators Examples
1 () (3+4)2 ** (expo.) 2**33 -,+ (unary) -5, +104 *,/,% 3*4, 7%25 -,+ 2+7, 3-4
![Page 18: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/18.jpg)
Try this (1)Try this (1)
18
2+5*6/3+(7-2*3)What is the result?
13.0
![Page 19: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/19.jpg)
Try thisTry this (2)(2)
19
2 ** 2 ** 3What is the result?
28 = 256
2 ** (2 ** 3)
![Page 20: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/20.jpg)
Reusing valuesReusing values•A force of 2.5 newton pushes
a rock with mass 1 kg to the left. Where is the rock after 1 second, 5 second and 15 second?
20
(1.0/2.5)*1*1/2(1.0/2.5)*5*5/2
(1.0/2.5)*15*15/2
Redundan
t
![Page 21: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/21.jpg)
VariablesVariables•We can use variables to refer
to result from previous computation.
21
a*1*1/2a*5*5/2
a*15*15/2
a = 1.0/2.5
a 0.4
![Page 22: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/22.jpg)
VariablesVariables
•A variable is used to refer to various data.•Use "=" to assign a value to a variable.•When we refer to that variable, we get the
value that the variable is referring to.
22
a = 1.0/2.50.4
a
![Page 23: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/23.jpg)
Variables can be Variables can be "modified" (1)"modified" (1)
23
a = 10a * 5b = 3a + ba = 7a + ba = b + 5aa + b
50
13
10
8
11
![Page 24: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/24.jpg)
Variables can be Variables can be "modified" (2)"modified" (2)
24
a = 10a = a + 1
11
![Page 25: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/25.jpg)
Variables can be Variables can be "modified" (3)"modified" (3)
25
x = 10x = x * 2
20
![Page 26: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/26.jpg)
Variables can be Variables can be "modified" (4)"modified" (4)
26
x = 10x = x * 2 + 5
25
![Page 27: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/27.jpg)
Working onWorking on Wing Wing IDEIDE
27
![Page 28: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/28.jpg)
Typing programs Typing programs inin WingIDEWingIDE
28
![Page 29: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/29.jpg)
A programA program•A program is a sequence of
commands (or statements)
29
a = 10b = a + 5a - bc = 12b = a + cc = a*ba + b + c1 + a - c
Try totype this into
Wing IDE
![Page 30: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/30.jpg)
ResultResult
30
Empty
•Because the program does not have statements that output anything.
![Page 31: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/31.jpg)
PrintingPrinting•We can use function
printto display results
31
![Page 32: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/32.jpg)
A programA program•A program is a sequence of
commands (or statements)
32
a = 10b = a + 5print(a – b)c = 12b = a + cc = a*bprint(a + b + c)print(1 + a – c)
Add"print"
to display the valueof the required
expressions
![Page 33: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/33.jpg)
See the output See the output after hitting "Run"after hitting "Run"
33
![Page 34: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/34.jpg)
Function callsFunction calls
34
print(10)print(10)
Function nameArguments
![Page 35: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/35.jpg)
What's going on?What's going on?
•The expressions on the parameter list are evaluated before sending the result to the function.
35
print(a + b * 2)print(a + b * 2)
2020Assume thatAssume that
a = 5a = 5
b = 10b = 10 2525
print(25)print(25)print(25)print(25)
![Page 36: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/36.jpg)
A simple A simple calculation calculation programprogram•We have the following coins
5 one-baht coins 7 ten-baht coins 2 twenty-baht notes 3 hundred-baht notes
36
sum1 = 1 * 5sum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3sum = sum1+sum5+sum20+sum100print(sum)
How muchmoney do
wehave ?
![Page 37: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/37.jpg)
A simple programA simple program (2)(2)
•Or we can even remove variable sum
37
sum1 = 1 * 5sum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3print(sum1+sum5+sum20+sum100)
![Page 38: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/38.jpg)
Meaningful namesMeaningful names
38
a = 1 * 5b = 10 * 7c = 20 * 2d = 100 * 3e = a + b + c + dprint(e)
sum1 = 1 * 5sum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3sum = sum1+sum5+sum20+sum100print(sum)
Theyperform thesame taskWhich oneis
easier to
understand?
Compare
these two
programs
![Page 39: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/39.jpg)
SuggestionsSuggestions•Write programs
for people to read At the very
least, one of the audience is yourself.
39
![Page 40: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/40.jpg)
Comments (#)Comments (#)•To make a program easier to
read, we can add comments to the program
•Everything after the # symbol is a comment.
40
![Page 41: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/41.jpg)
A program with A program with commentscomments
41
# this program calculates total money# from the amount of each type of coins or# bank notes that you havesum1 = 1 * 5 # value of 1-baht coinssum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3print(sum1+sum5+sum20+sum100)
![Page 42: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/42.jpg)
”Hello
StringsStrings•A computer can work with many types of
data.•A string is another data type which is very
important. It is a type for texts or messages.•Formally, a string is a sequence of characters.
42
”Hello, world”
![Page 43: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/43.jpg)
String constantsString constants•We can either use single or double
quotes to specify strings, e.g., ”Hello” ’World’
•However, the starting quotes and the ending quotes must match.
•We can also have special characters inside a string. They will start with backslash " \ ".
43
![Page 44: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/44.jpg)
Examples (1)Examples (1)
44
print("hello") hello
print('hello') hello
print("I'm 9") I'm 9
print('I'm 9') ERROR
print('I\'m 9') I'm 9
print("I\'m 9") I'm 9
![Page 45: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/45.jpg)
Examples (2)Examples (2)
45
print("123") 123
print(123) 123
print("12" + "3") 123
print(12 + 3) 15
print("12" + '3') 123
print("12" + 3) ERROR
![Page 46: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/46.jpg)
A slightly better A slightly better programprogram
46
sum1 = 1 * 5sum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3sum = sum1+sum5+sum20+sum100print("The total is",sum)
The total is 415
![Page 47: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/47.jpg)
A slightly even A slightly even better programbetter program
47
sum1 = 1 * 5sum10 = 10 * 7sum20 = 20 * 2sum100 = 100 * 3sum = sum1+sum5+sum20+sum100print("The total is",sum,"bath.")
The total is 415 bath.
![Page 48: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/48.jpg)
Sidenote: printing Sidenote: printing and new linesand new lines
•We can display data using function print.
•It will always add a new line at the end.
•If we want to avoid the new line, we can add an additional option "end" to print.
48
print(10)print(20)print(10,end='')print(20)
10201020
10201020
This tells print to end this output with an empty string, instead of a new line.
![Page 49: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/49.jpg)
Reading inputsReading inputs•We can use function input to
read data from the user
input
•The function returns a string that the user types into the shell.
49
![Page 50: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/50.jpg)
Examples in the Examples in the Python ShellPython Shell
>>> name = input()Somchai>>> print("Hello", name)Hello Somchai>>> a = input()10>>> b = input()100>>> print(a+b)10100
50
![Page 51: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/51.jpg)
RemarksRemarks•Consider this statement
print(a+b)
•Since both variables a and b are strings from function input. When we add two strings, we only get the concatenation of them.
51
![Page 52: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/52.jpg)
??????????•How are we going to do
calculations when we can only read strings from the user?
52
![Page 53: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/53.jpg)
ConversionConversion•We can use function int, float, and str to convert between various data types
53
int("10")
float("10")
float(10)
10
10.0
10.0
int(10.6) 10
![Page 54: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/54.jpg)
Type conversionType conversion
54
int("10")+10
float("10")+10
float(10)+int(5)
20
20.0
15.0
str(10)+str(5) 105
![Page 55: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/55.jpg)
Conversion between Conversion between float and int (1)float and int (1)
55
int(10.2)
int(10.9)
10
10
Always return the integers without the fractional
parts.
int(-10.1) -10
![Page 56: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/56.jpg)
Conversion between Conversion between float and int (2)float and int (2)
56
round(10.2)
round(10.9)
10
11
We can also use function round that returns the closest
integers.
round(-10.1) -10
![Page 57: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/57.jpg)
Adding two Adding two numbersnumbers
57
# This program adds two numbersastr = input()a = int(astr)bstr = input()b = int(bstr)print("The result is",a+b)
![Page 58: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/58.jpg)
Nested function Nested function calls (1)calls (1)
•Consider this part of the program
•We use variable astr to refer to an input string. We then convert it to an integer and assign the result to variable a.
•We can avoid using variable astr:
58
astr = input()a = int(astr)
a = int(input())
![Page 59: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/59.jpg)
Nested function Nested function calls (2)calls (2)
•This is how it works.
59
a = int(input())
input()
int"12345"
12345
a = 12345a
![Page 60: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/60.jpg)
Two additional Two additional important important functionsfunctions
60
Functions Return valuesabs(x) Returns the absolute
value of xpow(x,y) Returns the value of
xy
![Page 61: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/61.jpg)
Money calculationMoney calculation ((improvedimproved))
61
# This program calculates total amount# of money from numbers of bank notesp1 = int(input())sum1 = 1 * p1p5 = int(input())sum5 = 10 * p5p20 = int(input())sum20 = 20 * p20p100 = int(input())sum100 = 100 * p100sum = sum1+sum5+sum20+sum100print("The total is",sum,"bath.")
![Page 62: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/62.jpg)
A promptA prompt•We can tell function input to
display a prompt before reading input by providing a string argument to input
62
Enter X: 100
110
![Page 63: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/63.jpg)
Thinking cornerThinking corner•An object, initially sitting still,
starts moving with an acceleration of a m/s2 for t seconds.
•Write a program that reads the acceleration and the duration and computes the displacement.
63
![Page 64: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/64.jpg)
SolutionSolution
64
a = float(input("Enter a: "))t = float(input("Enter t: "))print("Total distance =", a*t*t/2)
![Page 65: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/65.jpg)
Thinking cornerThinking corner
65
Enter length in inch: 320It is 26 feet, 8 inch.Enter length in inch: 320It is 26 feet, 8 inch.
x = int(input("Enter length in inch"))xf = int(x/12)xi = x – xf * 12print("It is", xf, "feet,",xf,"inch.")
x = int(input("Enter length in inch"))xf = int(x/12)xi = x – xf * 12print("It is", xf, "feet,",xf,"inch.")
![Page 66: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/66.jpg)
Volume Volume CalculationCalculation
•Compute the volume of a cylinder
66
hh
r
r2 x h
![Page 67: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/67.jpg)
•We can use 22/7 (which is
quite inaccurate).•We can use a closer
estimation, in module math.
67
3.1415926535897933.141592653589793
![Page 68: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/68.jpg)
TheThe math modulemath module•In Python, functions are
categorized into various groups, called modules. To use functions from a module, we have to declare that by using the import statement.
•Then all functions can be referred to by prefixing with the module name.
68
import math
print("Pi is", math.pi)
import math
print("Pi is", math.pi)
3.1415926535897933.141592653589793
![Page 69: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/69.jpg)
Thinking cornerThinking corner•Write a program
that reads r and h and compute the volume of a cylinder.
69
hh
r
r2 x h
![Page 70: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/70.jpg)
SolutionSolution
70
import math
r = float(input("Enter r: "))h = float(input("Enter h: "))print("Volume =", math.pi*r*r*h)
import math
r = float(input("Enter r: "))h = float(input("Enter h: "))print("Volume =", math.pi*r*r*h)
![Page 71: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/71.jpg)
Important Important functions infunctions in math math
modulemodule
71
Functions Goalsfabs(x) The absolute value of xsin(x), cos(x), tan(x)
Trigonometric functions of x (the angles are specified in radian)
pi Constant Pie Constant elog(x),log10(x) Natural logarithm,
logarithm base 10exp(x) The value of ex
sqrt(x) Square root of x
![Page 72: Variables, Expressions, and Standard Functions](https://reader035.vdocuments.site/reader035/viewer/2022081506/56815156550346895dbf77b7/html5/thumbnails/72.jpg)
Another exampleAnother example•Projection
72
t
fimport math# radian angles
fy = f * math.sin(t)fx = f * math.cos(t)
import math# radian angles
fy = f * math.sin(t)fx = f * math.cos(t)
# don't forget to import math# recall that the angle must be in radian
r = t * math.pi/180fy = f * math.sin(r)fx = f * math.cos(r)
# don't forget to import math# recall that the angle must be in radian
r = t * math.pi/180fy = f * math.sin(r)fx = f * math.cos(r)