variables and expressions
play

Variables and Expressions Craig Zilles (Computer Science) - PowerPoint PPT Presentation

Variables and Expressions Craig Zilles (Computer Science) https://go.illinois.edu/cs105sp20 February 3, 2020 To Today 1. Objects, literals 2. Types and representation Integers Strings, Unicode, and Escaping Reals, floating point


  1. Variables and Expressions Craig Zilles (Computer Science) https://go.illinois.edu/cs105sp20 February 3, 2020

  2. To Today 1. Objects, literals 2. Types and representation • Integers • Strings, Unicode, and Escaping • Reals, floating point 3. Identifiers, assignment, immutability 4. Expressions and Operator Precedence • Division, Floor division, and modulo operator 5. Modules and Importing 6. Excel: relative and absolute references • Moving formulas between cells 2

  3. Yo You are in good company (m (muddy uddy pt pts.) • "I find it difficult to memorize all the different functions and commands and how to write them correctly. " • "Will we have to know the encoded values of each character in order to code?" • "I think having to remember the Unicode and escape sequences was pretty difficult and confusing" • "The mass amounts of vocabulary was very confusing and should be gone over." 3

  4. The The Pytho hon n interpr preter is yo your friend! Use it to play around with the language 4

  5. Al All data a in Python is stored in an an ob object • Every object has: • A type • A value • Literals are textual descriptions, read by Python to make objects • “hi there” • 17 • -203.5974 5

  6. We We’ve now met three ty types: • Integers: whole numbers of arbitrary precision • Strings: e.g., our string literals like “Hello CS 105!” • Floating point numbers: approximations of real numbers • Types are important, because it specifies how to store data • Computers represent everything as a finite number of 1’s and 0’s • The type says how to interpret the 1’s and 0’s 6

  7. How in Ho ints ar are stored Number Chunks Type of chunks (Store the number in binary) Small number int 1 000110101 Large number int 2 000101011 110101001 • Integers are usually used for counting things 7

  8. Ho How st strings s are e stored ed Number of Characters Type characters (Stored using Unicode encoding) ‘CS 105’ String 6 001000011 001010011 000100000 000110001 000110000 000110101 C S 1 0 5 8

  9. Whi Which o ch of t the he f followi wing ng ar are c cons nside dered ‘ d ‘whi whitespace pace’? A) Spaces B) Tabs C) Newlines D) Spaces and Tabs E) Spaces, Tabs, and Newlines 9

  10. Whi Which o ch of t the he f followi wing ng ar are c cons nside dered ‘ d ‘whi whitespace pace’? A) Spaces B) Tabs C) Newlines D) Spaces and Tabs E) Spaces, Tabs, and Newlines In computer programming, whitespace is any character or series of characters that represent horizontal or vertical space in typography. When rendered, a whitespace character does not correspond to a visible mark, but typically does occupy an area on a page. --Wikipedia 10

  11. Ho How st strings s are e stored ed Number of Characters Type characters (Stored using Unicode encoding) ‘CS 105’ String 6 001000011 001010011 000100000 000110001 000110000 000110101 C S 1 0 5 • Unicode can encode pretty much any character • Including many things that aren’t on your computer keyboard • How do we tell Python we want to use those characters? • Can specify the Unicode codepoint: e.g., 0394 is the Greek delta (Δ) • How do we distinguish a codepoint from a number? 11

  12. Esc Escapi ping ng • Treat slash (\) as a special character • \ means that the following characters should be interpreted differently • \u followed by a number is a code point • '\u0394’ is the Greek delta (Δ) • \” and \’ are quote characters that don’t end a string • \t encodes a tab • \n encodes a new line • \\ encodes a slash 12

  13. Ho How man any visible char arac acters ar are printed? • print('\\n\t\\t') A) 1 B) 2 C) 3 D) 4 E) 5 13

  14. Nu Number ers s beyond integ eger ers • Integers only represent whole numbers • Sometimes you need to represent numbers between integers • Often when measuring things (lengths, speeds, etc.) • Real numbers: • Mathematically, there are an infinite number of numbers between each integer • On computers, we can’t represent an infinite number of possible numbers with a finite number of bits • Can only approximate real numbers 14

  15. How fl Ho floats s are e stored ed • Like scientific notation: 6.02 x 10 23 • mantissa x 10 exponent Type Mantissa Exponent Float -3.76341 +22 Can specify in scientific notation • Fixed-size mantissa: finite precision • Normally hidden by python • format(0.1, '.17f') • Fixed-size exponent: limited range • 100.1 ** 200 15

  16. Wh Which isn’t a valid Python literal A) 1.000001 B) 1E-7 C) 1,097 D) -3.00 E) '\'\'\'' # Consists only of single quotes and slashes 16

  17. We We’ve now met three ty types: • Integers: whole numbers of arbitrary precision • Strings: e.g., our string literals like “Hello CS 105!” • Floating point numbers: approximations of real numbers • You can ask a value what its type is using: type( expression ) • You can convert between them with str() and int() and float() 17

  18. Va Variables • Useful to give names to objects • Names in Python have to follow certain rules: • Begin with letter or underscore • Contains only letters, numbers, or underscores • Avoid reserved words (also known as key words) • Python recommended style: Snake Case • lower_case_words_separated_by_underscores 18

  19. Wh Which are legal Pyt ython names? 1. ______ 2. 12monkeys 3. m0nk3yM4n A. 1 and 2 B. 1 and 3 C. 2 only D. 2 and 3 E. 1, 2, and 3 19

  20. As Assig ignment • Variable names are bound to values with assignment statements • Structure: variable_name = expression • What happens: 1. expression evaluated to compute a value 2. variable_name is bound to the value 20

  21. Wh What is the value of y af after this code ex executes x = 2 y = x + 3 x = 5 A) 2 B) 3 C) 5 D) 8 E) 10 21

  22. Wh What is the value of y af after this code ex executes x = 7 y = x x = x + 2 A) 2 B) 5 C) 7 D) 9 E) None of the above 22

  23. Im Immutability ility • strings, ints, and floats are all immutable • Once an object has been created, it can’t be changed • New ones must be made instead • Multiple variables can be bound to the same object • If object is immutable, updating one variable doesn’t affect the others 23

  24. Ga Garba bage e Collec ection • If an object isn’t bound to any variable, the Python interpreter gets rid of it. • Frees up space for new objects 24

  25. Lo Logistics cs • Lab next week: Excel + Spatial skills development • Bring plug-in headphones if you can • Homework on PrairieLearn: • More points available than you need • Report any problems with “Report an error in this question” • Code reading exercises using natural language processing • Exam 0 next week: Sign up on https://cbtf.engr.illinois.edu/sched/ • Meant to get comfortable in the CBTF • Drawn almost completely from pre-lecture 1 & 2 and HW 1 & 2 25

  26. Lo Logistics, cs, c cont. • Need help? • Sending me email is probably not the answer • Questions about course content? • Search Piazza (first) or ask question on Piazza (second) • Office Hours: Wohlers Hall Computer Lab • CS 105 Tutoring (TBA soon) • Gies Tutoring • Questions about course policies, grading -> Piazza • Questions/documents about DRES, excused absences? • cs105admin@Illinois.edu (our course administrator) 26

  27. 27

  28. Expr Expressi ssions ns • Any Python code fragment that produces a value • Can include: • Literals • Variables • Operators • Functions • Right-hand side of assignment can be arbitrary expression 28

  29. Or Order of Op Operations • Parentheses () highest precedence • Exponentiation ** • (unary) Positive, negative +x, -x • Multiplication, Division, Modulo *, /, % • Addition, Subtraction +, - lowest precedence Left-to-right within a precedence level 29

  30. Wh What is the value of this expression? -3 ** 2 A) -9 B) -8 C) 8 D) 9 E) None of the above 31

  31. Go Good d Styl yle e with h Expr Expressi essions ns • Put a single space between every variable, operator, and number • this_is + a_readable – expression • Break up complicated expressions total = num_machines * (cost_per_machine * (1 + tax_rate) + shipping rate) • vs machine_cost = num_machines * cost_per_machine machine_cost_with_tax = machine_cost * (1 + tax_rate) shipping_cost = num_machines * shipping_rate total = machine_cost_with_tax + shipping_cost • Be generous with parentheses 32

  32. Wh What would happen… • if I type the following into a brand new Python interpreter: x + y A) nothing B) SyntaxError C) NameError D) ValueError E) TypeError 33

  33. Ex Expression on types • Result type generally depends on types of values in expression: • an_integer + another_integer -> an integer • a_float + another_float -> a float • a_string + another_string -> a string • If you mix ints and floats, ints will be promoted to floats: • 3.0 + 7 -> 3.0 + 7.0 -> 10.0 • Generally can’t mix strings with either ints or floats 34

  34. Wh What (if any) kind of error is in this pr program? value = input("Input your fave number!\n") print("Your new fave number is", value + 1) A) No error B) SyntaxError C) NameError D) ValueError E) TypeError 35

Recommend


More recommend