Presentation is loading. Please wait.

Presentation is loading. Please wait.

Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering.

Similar presentations


Presentation on theme: "Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering."— Presentation transcript:

1 Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering

2 A variable role is the reason we are using the variable. Variables always remember data for later use. But why are we trying to remember something? What Are Variable Roles?

3 Certain reasons for using a variable come up over and over. Eight roles cover 90% of the variable use by first-year programmers. What Are Variable Roles?  Fixed  Most recent  Accumulator  Aggregator  Stepper  Walker  Best-so-far  One-way flag

4 Quick Summary  Fixed Assigned once  Most recent Assigned unpredictably  Accumulator Running total  Aggregator Running list  Stepper Predetermined sequence of values  Walker Elements of iterator  Best-so-far Record holder  One-way flag Won’t reset flag until after iteration

5 Roles say why we are using the variable. Roles are not syntax. Syntax is whether the variable is a parameter of a function, an index, a return value, is being assigned to, etc. Roles can each have various types; roles are different than type. Types include int, string, list, tuple. What Are Variable Roles?

6 Create a single place to tweak a number used throughout a program. Low maintenance! Make code easier to read: No wondering “why subtract 20 here?” Make it easy to add features: User decides on the constant. Variable Role: Fixed Why use a fixed variable?

7 Pattern: Assigned at the head of a program or at the head of a code block. That means you see it on the left of the single equal sign, the assignment operator. Used in any way later but never assigned again. Convention suggests all caps. Variable Role: Fixed 01 02 def calculate_total(bill): LOCAL_TIPPING_CUSTOM = 0.15

8 Retrieve or calculate once, use multiple times Remember state of a process Remember user input until needed Embed explanation Debug by printing Pattern: Appears on left of assignment and then in a variety of syntax Variable Role: Most-Recent Why use a most recent variable? 01 02 length = raw_input(‘How many feet? ’) length = int(length)*12

9 To keep a running total or cumulative value – could be multiplication, addition, net,... Syntax: 1.Assigned to initial value before loop, 2.Assigned with *= or += inside of loop 3.Result used after loop Variable Role: Accumulator Why use an accumulator variable?

10 Variable Role: Accumulator Pattern: Initialize-Accumulate-Report 01 02 03 04 05 accumulator = 0 # initialize for element in iterable: # accumulate accumulator += worth(element) print(accumulator) # report/use

11 To collect items and remember them all separately Common pattern: 1.Initialize to empty collection before a loop, 2.Append element to aggregate during iteration 3.The aggregate is used during or after the loop Variable Role: Aggregator Why use an aggregator variable?

12 Variable Role: Aggregator Pattern: Initialize-Aggregate-Report 01 02 03 04 05 06 aggregator = [] # initialize for element in iterable: if condition_met_by(element): # append aggregator.append(element) how_many = len(aggregator) # report/use

13 Iterate a specific number of times Know that 5 th or 7 th or n th iteration is being executed Move forward a specified chunk of memory Represent integers – for factorials, e.g. Common syntax: Variable Role: Stepper Why use a stepper variable? 01 02 for stepper in range(100): do_something()

14 Refer to members of a collection during iteration Common Patterns: As an element in a Python or Java™ iterator As an index of an array in Java/C/Python Variable Role: Walker Why use a walker variable? 01 02 for walker in range(len(humans)): do_something_with(humans[walker]) 01 02 for walker in humans: do_something_with(walker)

15 To remember the record holder while iterating across many opportunities to set the record Frequent pattern: 1.Initialize to worst-possible value before loop, 2.During iteration, compare something to best-so-far and maybe assign a new record. 3.After loop, best-so-far used as the true record-best from all iterations. Variable Role: Best-So-Far Why use a best-so-far variable?

16 01 02 03 04 05 06 07 08 09 # Start with a record that will be beat. tallest = 40 for person in group: # Check for record breaker. if person.height > tallest: # Set the new record! tallest = person.height # report/use print(‘Tallest person:’,tallest) Variable Role: Best-So-Far Pattern: Initialize Check & Set Record Report

17 To remember whether any of several opportunities meet a single condition. Common pattern: 1.“Clear” the flag (initialize) to say the opportunity has not yet been met. 2.Check for condition with each iteration and “raise” flag if true. 3.Flag is not cleared during iteration. 4.After loop, check if flag was raised during the iterations. Variable Role: One-Way Flag Why use a one-way-flag variable?

18 Variable Role: One-Way Flag Pattern: Initialize to clear flag Check & Raise Flag Report 01 02 03 04 05 06 07 08 09 10 # Initialize with lowered flag pirate = False for song in storage: # Check for flag-raising condition if is_stolen(song): # Raise the flag pirate = True # report/use if pirate: warn_or_prosecute_pirate()


Download ppt "Roles of Variables with Examples in Python ® © 2014 Project Lead The Way, Inc.Computer Science and Software Engineering."

Similar presentations


Ads by Google