《Py4Inf-02-ExpressionsPy4Inf-02-Expressions》由会员分享,可在线阅读,更多相关《Py4Inf-02-ExpressionsPy4Inf-02-Expressions(33页珍藏版)》请在金锄头文库上搜索。
1、Variables, Expressions, and StatementsChapter 2Python for Informatics: Exploring IUnless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0 License.http:/creativecommons.org/licenses/by/3.0/.Copyright 2010- Charles R. SeveranceConstantsFixed val
2、ues such as numbers, letters, and strings are called “constants” - because their value does not changeNumeric constants are as you expectString constants use single-quotes ()or double-quotes () print 123123 print 98.698.6 print Hello worldHello worldVariablesA variable is a named place in the memory
3、 where a programmer can store data and later retrieve the data using the variable “name”Programmers get to choose the names of the variablesYou can change the contents of a variable in a later statement12.2x14 yx = 12.2y = 14100x = 100Python Variable Name RulesMust start with a letter or underscore
4、_ Must consist of letters and numbers and underscoresCase SensitiveGood: spam eggs spam23 _speedBad: 23spam #sign var.12Different: spam Spam SPAMReserved WordsYou can not use reserved words as variable names / identifiersand del for is raise assert elif from lambda return break else global not try c
5、lass except if or while continue exec import pass yield def finally in print Sentences or Linesx = 2x = x + 2print xVariable OperatorConstant Reserved WordAssignment StatementAssignment with expressionPrint statementAssignment StatementsWe assign a value to a variable using the assignment statement
6、(=)An assignment statement consists of an expression on the right hand side and a variable to store the resultx = 3.9 * x * ( 1 - x )x = 3.9 * x * ( 1 - x )0.6xRight side is an expression. Once expression is evaluated, the result is placed in (assigned to) x.0.60.60.40.93A variable is a memory locat
7、ion used to store a value (0.6).x = 3.9 * x * ( 1 - x )0.6 0.93xRight side is an expression. Once expression is evaluated, the result is placed in (assigned to) the variable on the left side (i.e. x).0.93A variable is a memory location used to store a value. The value stored in a variable can be upd
8、ated by replacing the old value (0.6) with a new value (0.93).Numeric ExpressionsBecause of the lack of mathematical symbols on computer keyboards - we use “computer-speak” to express the classic math operationsAsterisk is multiplicationExponentiation (raise to a power) looks different from in math.
9、Operator Operation+ Addition- Subtraction* Multiplication/ Division* Power% RemainderNumeric Expressions xx = 2 xx = xx + 2 print xx4 yy = 440 * 12 print yy5280 zz = yy / 1000 print zz5 jj = 23 kk = jj % 5 print kk3 print 4 * 364Operator Operation+ Addition- Subtraction* Multiplication/ Division* Po
10、wer% Remainder5 234 R 3203Order of EvaluationWhen we string operators together - Python must know which one to do firstThis is called “operator precedence”Which operator “takes precedence” over the othersx = 1 + 2 * 3 - 4 / 5 * 6Operator Precedence RulesHighest precedence rule to lowest precedence r
11、uleParenthesis are always respectedExponentiation (raise to a power)Multiplication, Division, and RemainderAddition and SubtractionLeft to rightParenthesisPowerMultiplicationAdditionLeft to RightParenthesisPowerMultiplicationAdditionLeft to Right1 + 2 * 3 / 4 * 51 + 8 / 4 * 51 + 2 * 51 + 1011 x = 1
12、+ 2 * 3 / 4 * 5 print x11 ParenthesisPowerMultiplicationAdditionLeft to Right x = 1 + 2 * 3 / 4 * 5 print x11 1 + 2 * 3 / 4 * 51 + 8 / 4 * 51 + 2 * 51 + 1011Note 8/4 goes before 4*5 because of the left-right rule.Operator PrecedenceRemember the rules top to bottomWhen writing code - use parenthesisW
13、hen writing code - keep mathematical expressions simple enough that they are easy to understandBreak long series of mathematical operations up to make them more clearParenthesisPowerMultiplicationAdditionLeft to RightExam Question: x = 1 + 2 * 3 - 4 / 5Python Integer Division is Weird!Integer divisi
14、on truncatesFloating point division produces floating point numbers print 10 / 25 print 9 / 24 print 99 / 1000 print 10.0 / 2.05.0 print 99.0 / 100.00.99This changes in Python 3.0Mixing Integer and FloatingWhen you perform an operation where one operand is an integer and the other operand is a float
15、ing point the result is a floating pointThe integer is converted to a floating point before the operation print 99 / 1000 print 99 / 100.00.99 print 99.0 / 1000.99 print 1 + 2 * 3 / 4.0 - 5-2.5 What does “Type” Mean?In Python variables, literals, and constants have a “type”Python knows the difference between an integer number and a stringFor example “+” means “addition” if something is a num