Docsity

Python Variables, Constants, and Expressions - Prof. Ho, Study Guides, Projects, Research of Algorithms and Programming

Partial preview of the text

Download Python Variables, Constants, and Expressions - Prof. Ho and more Study Guides, Projects, Research Algorithms and Programming in PDF only on Docsity!

Variables, Expressions, and

Statements

Chapter 2

Python for Everybody www.py4e.com

Constants

  • (^) Fixed values such as numbers, letters, and strings, are called “constants” because their value does not change
  • (^) Numeric constants are as you expect
  • (^) String constants use single quotes (') or double quotes (") >>> print( 123 ) 123 >>> print(98.6) 98. >>> print('Hello world') Hello world

Variables

  • (^) A variable is a named place in the memory where a programmer can store data and later retrieve the data using the variable “name”
  • (^) Programmers get to choose the names of the variables
  • (^) You can change the contents of a variable in a later statement x^ 12. y^14

x = 12.

y = 14

Variables

  • (^) A variable is a named place in the memory where a programmer can store data and later retrieve the data using the variable “name”
  • (^) Programmers get to choose the names of the variables
  • (^) You can change the contents of a variable in a later statement x^ 12. y^14 x = 12.2 100

y = 14

x = 100

Mnemonic Variable Names

  • (^) Since we programmers are given a choice in how we choose our variable names, there is a bit of “best practice”
  • (^) We name variables to help us remember what we intend to store in them (“mnemonic” = “memory aid”)
  • (^) This can confuse beginning students because well-named variables often “sound” so good that they must be keywords http://en.wikipedia.org/wiki/Mnemonic

x1q3z9ocd = 35. x1q3z9afd = 12. x1q3p9afd = x1q3z9ocd * x1q3z9afd print(x1q3p9afd)

What is this bit of

code doing?

x1q3z9ocd = 35. x1q3z9afd = 12. x1q3p9afd = x1q3z9ocd * x1q3z9afd print(x1q3p9afd) hours = 35. rate = 12. pay = hours * rate print(pay) a = 35. b = 12. c = a * b print(c)

What are these bits

of code doing?

Sentences or Lines

x = 2

x = x + 2

print(x)

Variable Operator (^) Constant Function Assignment statement Assignment with expression Print statement

x = 3.9 * x * ( 1 - x ) x 0. The right side is an expression. Once the expression is evaluated, the result is placed in (assigned to) x.

A variable is a memory location used to store a value (0.6)

x = 3.9 * x * ( 1 - x ) x 0.6 0.

The right side is an expression. Once the expression is evaluated, the result is placed in (assigned to) the variable on the left side (i.e., x). A variable is a memory location used to store a value. The value stored in a variable can be updated by replacing the old value (0.6) with a new value (0.936).

Numeric Expressions

  • (^) Because of the lack of mathematical symbols on computer keyboards - we use “computer-speak” to express the classic math operations
  • (^) Asterisk is multiplication
  • (^) Exponentiation (raise to a power) looks different than in math Operator Operation + Addition - Subtraction
  • Multiplication / Division ** Power % Remainder

xx = 2 xx = xx + 2 print(xx) 4 yy = 440 * 12 print(yy) 5280 zz = yy / 1000 print(zz)

jj = 23 kk = jj % 5 print(kk) 3 print( 4 ** 3 )

64 Operator Operation

  • Addition
  • Subtraction
  • Multiplication / Division ** Power % Remainder
4 R 3

Numeric Expressions

Operator Precedence Rules

Highest precedence rule to lowest precedence rule:

  • (^) Parentheses are always respected
  • (^) Exponentiation (raise to a power)
  • (^) Multiplication, Division, and Remainder
  • (^) Addition and Subtraction
  • (^) Left to right Parenthesis Power Multiplication Addition Left to Right

1 + 2 ** 3 / 4 * 5 1 + 8 / 4 * 5 1 + 2 * 5 1 + 10 11

x = 1 + 2 ** 3 / 4 * 5 print(x)

Parenthesis Power Multiplication Addition Left to Right