Assignments and Loops Rose-Hulman Institute of Technology Computer - - PowerPoint PPT Presentation

assignments and loops
SMART_READER_LITE
LIVE PREVIEW

Assignments and Loops Rose-Hulman Institute of Technology Computer - - PowerPoint PPT Presentation

Assignments and Loops Rose-Hulman Institute of Technology Computer Science and Software Engineering Outline Basic number types: int and float Variables and assignments Definite loops Math library Accumulator problem Check


slide-1
SLIDE 1

Assignments and Loops

Rose-Hulman Institute of Technology Computer Science and Software Engineering

slide-2
SLIDE 2

Outline

  • Basic number types: int and float
  • Variables and assignments
  • Definite loops
  • Math library
  • Accumulator problem
slide-3
SLIDE 3

Check out 03-AssignmentsAndLoops

  • Go to SVN Repository view at bottom of the workbench

– Missing? Add it back: Window

  • Show View
  • Other
  • SVN
  • SVN Repositories
  • Browse SVN Repository view for

03-AssignmentsAndLoops

  • Right-click it and choose Checkout
  • Accept defaults in the dialog
  • Expand the 03-AssignmentsAndLoops project that appears

in Package Explorer (on the left-hand-side)

slide-4
SLIDE 4

PyDev Interpreter Console

1 2 3 4 5 Woot! Interpreter Shell

slide-5
SLIDE 5

Some numeric operations

Operator Operation + Addition

  • Subtraction

* Multiplication / Division ** Exponentiation % Remainder // Integer division Function Operation abs(x) Absolute value of x round(x, y) Round x to y decimal places int(x) Convert x to the int data type float(x) Convert x to the float data type

slide-6
SLIDE 6

Variables

  • Identifiers

(i.e.names) that refer to values stored in memory

  • Values can be of

any type

width = 4 temperature = 98.6 dogName = "fido" lost = [4, 8, 15, 16, 23, 42] triangleArea = width * height / 2 xyPoint = (r * cos(theta), r * sin(theta))

slide-7
SLIDE 7

Variables and Assignment

  • Assignment gives a variable a value

x = 6 * 7

– Python evaluates right-hand side (42) – Then variable on left “gets” the value

  • “Gets” not “Equals”

– x = 3.9 * x * (1 – x)

slide-8
SLIDE 8

How to Think About Variables

  • Variables as sticky notes
  • Example on board…

x = 10 x = x + 1

slide-9
SLIDE 9

Three Kinds of Assignment

  • Simple
  • Compound
  • Multiple (or simultaneous)
slide-10
SLIDE 10

Simple Assignment

  • <variable> = <expr>
  • Note:

– input(<string>) is an expression – input statements are a kind of assignment statement

Q1,2

slide-11
SLIDE 11

Compound Assignment

  • <var> <op=> <expr> means

<var> = <var> <op> <expr>

– where <op=> is +=, -=, *=, /=, //=, or %=

  • Example:

– total += 5 is the same as total = total + 5

slide-12
SLIDE 12

Simultaneous Assignment

  • <var>, <var>, … = <expr>, <expr>, …
  • Example:

– sum, diff = x + y, x - y

Q3

slide-13
SLIDE 13

Assignment Assignment

  • See assignmentsAndLoops.py module
  • Do the TODOs inside the

assignmentStatements() function

slide-14
SLIDE 14

Summary: Assignment Statements

  • Simple assignments: <variable> = <expr>
  • Compound assignments

– <var> <op=> <expr> means <var> = <var> <op> <expr> where <op=> is +=, -=, *=, /=, //=, or %=

  • Simultaneous (multiple) assignments

– <var>, <var>, … = <expr>, <expr>, …

slide-15
SLIDE 15

Sequence

  • A list of things
  • For example:

– [2, 3, 5, 7] – [“My”, “dog”, “has”, “fleas”]

  • Every for loop uses a list
slide-16
SLIDE 16

Definite Loops

  • Loop: a control structure for executing a portion of a

program multiple times

  • Definite loop: Python knows beforehand how many

times to repeat the body of the loop

  • Syntax:

for <var> in <sequence> : <body>

  • Semantics: Executes <body> once for every element
  • f <sequence>, with <var> set to that element.
slide-17
SLIDE 17

for i in [0, 1, 2, 3, 4, 5]: print(2**i) for b in ["John", "Paul", "George", "Ringo"]: print(b, "was a Beatle")

Some Definite Loops

Loop body Loop index Loop sequence

slide-18
SLIDE 18

The range Function

  • Creates a list that is an

arithmetic sequence

  • General formats for range

function:

– range(<expr>) – range(<expr>, <expr>) – range(<expr>, <expr>, <expr>)

  • Consider:

list(range(8)) list(range(1, 7)) list(range(3, 18, 2)) list(range(4,10,-1)) list(range(17, -5, -3))

Q4

slide-19
SLIDE 19

Accumulator Loop

  • Accumulator combines

parts of a list

  • Common technique!
  • Consider:

More items in <seq>? <var> = next item <body> a = 0 for j in [1, 2, 3, 4]: a = a + j print(a)

yes no Continue after loop Q5

slide-20
SLIDE 20

Another loop with an accumulator

  • Find the sum of all of the positive odd

numbers that are ≤ 13

  • Do it together as a class, in function

sumOddPositiveLessThan()

slide-21
SLIDE 21

More math library components

Python Mathematic s English

pi π Approximation of pi e e Approximation of e sin(x) sin x The sine of x cos(x) cos x The cosine of x tan(x) tan x The tangent of x atan2(y, x) tan-1 y/x Arc tangent of angle of line from (0,0) to (x, y) log(x) ln x The natural (base e) log of x log10(x) log10x The base 10 log of x exp(x) ex The exponential of x

slide-22
SLIDE 22
  • Quadratic formula to find real roots for quadratic

equations of the form ax2 + bx + c = 0

  • Solution:
  • Write out the Python expression for the first

formula.

  • If time permits, test it in Eclipse

Math library functions

a ac b b x 2 4

2 −

+ − = a ac b b x 2 4

2 −

− − =

Q6

slide-23
SLIDE 23

HW2 due Wednesday at 8:00 AM, HW3 due Thursday at 8:00 AM

Work Time