630 likes | 744 Views
Computers and Programs. A Whirlwind tour. The Basics. Programs perform computation on data Variables provide temporary locations to store data while the program is running Statements are the instructions for performing computations on data. Getting The Lecture Code.
E N D
Computers and Programs A Whirlwind tour slides draw freely on those of previous Professors Flinn, Severance and Prakash
The Basics • Programs perform computation on data • Variables provide temporary locations to store data while the program is running • Statements are the instructions for performing computations on data
Getting The Lecture Code • In Eclipse, right-click on lectures project • Team • Update to HEAD
Variables • Programs perform computations on variables • A variable provides a location to store a value • Examples: • x = 3 • y = 'Paul' x 3 y 'Paul'
Variables • Programs perform computations on variables • A variable provides a location to store a value • Examples: • x = 3 • y = 'Paul' • Subsequent statements can change a variable’s value by storing a new value • x = 4 x 3 4 y 'Paul'
Variables (cont) • Each variable has: • A name (uniquely defines each variable) • A value (which can be changed) • A type, which defines: • A range of possible values • The set of operations that can be done to that variable
Variable Names / Identifiers • Must start with a letter or _ • Must consist of letters, numbers, and _’s • Good: spam eggs spam23 • Bad: 23spam #sign var.12 • Case SenSitiVe • Different: spam SpamSPAM Z-2.3.1
Reserved Words • You can not use reserved words as variable names / identifiers and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print Z-2.3.1
Check Your Understanding (CYU) • Which of the following are valid variable names? • Paul • paul • jason42 • 42jason • for • forever
Assignment and output • A statement can assign a value to a variable: • Syntax: <variable name> = <value> • Example: class_name = '182'
Types • Some examples of data types: • 3, 5 Integer (whole numbers) • 4.0, -1.345 Floating point (rational numbers) • 'EECS 182' String • These are all atomic types • Composite types also possible • [1, 2, 3]: a list of integers • In time, you’ll define your own complex types
Aside: Static vs. Dynamic Typing • Python is dynamically-typed • An assignment may change the type of a variable • Var = 3 (type is integer) • Var = "Hello, there!" (type is string) • I recommend you don’t do this! • Other languages (e.g., C) are statically-typed • Must declare variable’s type before using it • intVar = 3 (declares type as integer) • Var = "Hello, there!" (illegal statement)
Expressions • A statement can assign the value of an expression to a variable: • x = 3 + 4 • An expression can contain variables: • E.g., (x + 2) * 2 • The assigned variable can be in the expression • y = y + 1
Operations on Types • What did you learn?
Evaluation of Expressions y Right side is an expression. The assignment statement : Plugs values of variables into expression Evaluates the expression. Sets value of variable on left to the result x 0.6 y =3.9 * x * ( 1 - x )
Evaluation of Expressions y Right side is an expression. The assignment statement : Plugs values of variables into expression Evaluates the expression. Sets value of variable on left to the result x 0.6 0.6 0.6 y =3.9 * x * ( 1 - x )
Evaluation of Expressions y Right side is an expression. The assignment statement : Plugs values of variables into expression Evaluates the expression. Sets value of variable on left to the result x 0.6 0.6 0.6 y =3.9 * x * ( 1 - x ) 0.4 0.936
Evaluation of Expressions y 0.936 Right side is an expression. The assignment statement : Plugs values of variables into expression Evaluates the expression. Sets value of variable on left to the result x 0.6 0.6 0.6 y =3.9 * x * ( 1 - x ) 0.4 0.936
Assignment Happens in Slow Motion • We can use the same variable on the left and right side of an assignment statement • Remember that the right side is evaluated *before* the variable is updated Z-34
Evaluation of Expressions Right side is an expression. The assignment statement : Plugs values of variables into expression Evaluates the expression. Sets value of variable on left to the result x 0.6 x =3.9 * x * ( 1 - x )
Evaluation of Expressions Right side is an expression. The assignment statement : Plugs values of variables into expression Evaluates the expression. Sets value of variable on left to the result x 0.6 0.6 0.6 x =3.9 * x * ( 1 - x )
Evaluation of Expressions Right side is an expression. The assignment statement : Plugs values of variables into expression Evaluates the expression. Sets value of variable on left to the result x 0.6 0.6 0.6 x =3.9 * x * ( 1 - x ) 0.4 0.936
Evaluation of Expressions Right side is an expression. The assignment statement : Plugs values of variables into expression Evaluates the expression. Sets value of variable on left to the result x 0.6 0.936 0.6 0.6 x =3.9 * x * ( 1 - x ) 0.4 0.936
Incrementing • x = 10 • x = x + 1 • print x
Incrementing • x = 10 • x = x + 1 • print x x 10
Incrementing • x = 10 • x = x + 1 • print x x 10 11
Summary of Expressions • Programming languages have lots of expressions • Expression is anything that evaluates to a value • Can be a string, number or virtually anything • Can be a single value or computed from several values using operators Z-2.3.2
Examples of expressions • 2.0 * pi * r • 'My name is ' + first + ' ' + last • 17 • 'The temperature is ' + str ((fahrenheit – 32.0) * 5.0 / 9.0) + ' degrees Celsius' • And so on…
CYU • What are the values of these expression? • (3+7)*2 • 'Hello' + 'world' • 'Hello' + 3
Stored Programs • Like a recipe or installation instructions, a program is a sequence of steps (called statements) to be done in order • Some steps are conditional - they may be skipped • Sometimes a step or group of steps are to be repeated • Sometimes we store a set of steps to be used over and over as needed several places throughout the program Z-14
Output Statements • The print statement takes one or more expressions separated by commas and prints the expressions to the screen separated by spaces. x = 6 print 2 print 2 + x print “Hello”, 4+5 2 8 Hello 9 Z-2.4
CYU • What will the output be? x = 1 y = 2 x = y y = x print x, y x y
CYU • What will the output be? x = 1 y = 2 x = y y = x print x, y x 1 y
CYU • What will the output be? x = 1 y = 2 x = y y = x print x, y x 1 y 2
CYU • What will the output be? x = 1 y = 2 x = y y = x print x, y x 1 2 y 2
CYU • What will the output be? x = 1 y = 2 x = y y = x print x, y x 1 2 y 2 2
Conditional Steps x = 5 Output: Program: x = 5 if x < 10: print "Smaller" ifx > 20: print "Bigger" X < 10 ? print “Smaller” X > 20 ? print “Bigger” Z-199
Conditional Steps x = 5 Output: Program: x = 5 if x < 10: print "Smaller" if x > 20: print "Bigger" X < 10 ? print “Smaller” X > 20 ? print “Bigger” Z-199
Conditional Steps x = 5 Program: x = 5 if x < 10: print "Smaller" if x > 20: print "Bigger" Output: Smaller X < 10 ? print “Smaller” X > 20 ? print “Bigger” Z-199
Conditional Steps x = 5 Program: x = 5 if x < 10: print "Smaller" if x > 20: print "Bigger" Output: Smaller X < 10 ? print “Smaller” X > 20 ? print “Bigger” Z-199
Conditional Steps x = 5 Program: x = 5 if x < 10: print "Smaller" if x > 20: print "Bigger" Output: Smaller X < 10 ? print “Smaller” X > 20 ? print “Bigger” Z-199
Conditional statement • if <expression>: <one or more statements> Read this as: if and only if the logical expression evaluates to true, execute the statement(s)
Repeated Steps Output: Program: foriin range(5): print i i = 0 .. 4 print i Z-233
Repeated Steps Program: for i in range(5): print i Output: 0 1 2 3 4 i = 0 .. 4 print i Z-233
Definite Loops • Loops that run a fixed (i.e. definite) number of times • Loops that “iterate” through an ordered set • Loops that run “for” a number of times for abc in range(5): print abc Output: 0 1 2 3 4 Z-39
Looking at In... • range(n) produces a sequence of numbers from 0 to n-1 • The block of code is executed once for each value in the sequence • The iteration variable moves through all of the values in the sequence Five-element sequence [ 0, 1, 2, 3, 4] Iteration variable for val in range(5) : ... block of code ...
Stored (and reused) Steps Output: Program: def hello(): print "Hello" print "Fun" hello() hello() def print "Hello" print “Fun” hello() hello() We call these little stored chunks of code “subprograms” or “functions”or “procedures” or “methods”.
Functions: Stored (and reused) Steps Output: Hello Fun Hello Fun Program: def hello(): print "Hello“ print "Fun" hello() hello() def print “Hello” print “Fun” hello() hello() We call these little stored chunks of code “subprograms” or “functions”or “procedures” or “methods”.
Syntax: Code Blocks • Code blocks are started by colon • Each line of block is indented • Ends on a statement that is less indented def main(): print "This program illustrates a chaotic function" x = input("Enter a number between 0 and 1: ") for i in range(10): x = 3.9 * x * (1 - x) print x main() Fn block For block