cs 105
play

CS 105: VARIABLES AND EXPRESSIONS Max Fowler (Computer Science) - PowerPoint PPT Presentation

CS 105: VARIABLES AND EXPRESSIONS Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/ June 14, 2020 Video Series Two Topics Objects, Literals Types and Representation Identifiers, Assignment,


  1. CS 105: VARIABLES AND EXPRESSIONS Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/ June 14, 2020

  2. Video Series Two Topics  Objects, Literals  Types and Representation  Identifiers, Assignment, Immutability  Expressions and Operator Precedence, Module Imports  Excel Referencing and Moving Formulas Between Cells

  3. Objects and Literals

  4. All data in Python is stored in an object int  Objects have:  a type 5  a value  Literals are textual descriptions, read by Python to make an object string literal  "Hello there!" is a -> integer literal  5 is a -> floating point (float) literal  23.32 is a ->

  5. Three types we've met  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. Video Question – Objects have a WHAT and a WHAT?

  7. Types and Representations

  8. How ints are stored  Integers are usually used for counting things

  9. How strings are stored

  10. Which of the following are considered ‘whitespace’? A) Spaces B) Tabs C) Newlines D) Spaces and Tabs E) Spaces, Tabs, and Newlines

  11. Which of the following are considered ‘whitespace’? 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

  12. How strings are stored  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?

  13. Escaping  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

  14. Numbers beyond integers  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

  15. How floats are stored  Like scientific notation: 6.02 x 10 23  mantissa x 10 exponent 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

  16. We’ve now met three 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. Video Question – How many visible characters are printed with print('\\n\t\\t')?

  18. Identifiers, Assignments, Immutability

  19. What's a variable?  A variable is effectively a name for an object  Names in Python have rules…  Begin with letter or underscore  Contain only letters, numbers, underscores  While not a rule, AVOID reserved words (key words)  Python recommends Snake Case  snek_case_uses_underscores

  20. Danger Noodle is not the only case art by @allison_horst

  21. Assignment?  Variables names are bound to values with assignment statements  Structure: variable_name = expression  How does this work?  First, expression is evaluated to a value  Second, variable_name is bound to the value

  22. Immutability  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. Video Question – What is the value of y after this code executes? x = 2 y = x + 3 x = 5  2  3  5  8

  24. Expressions and Operator Precedence, Modules

  25. Expressions  Any Python code fragment that produces a value  Can include:  Literals  Variables  Operators  Functions  Right-hand side of assignment can be arbitrary expression

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

  27. Order of operations (full gory details) highest lowest

  28. Good style with expressions  Put a single space between every variable, operator, and number  this_is + a_readable – expression  Be generous with parentheses – almost no such thing as too much  Break up complicated expressions total = num_machines * (cost_per_machine * (1 + tax_rate) + shipping rate) 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

  29. Expression 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

  30. Division, Floor Division, and Modulo  Division operator (/) gives best approximation to true result  always results in a float  Floor Division (//) rounds down to closest whole number  Uses normal type rules for result  Modulo operator (%) performs a division and returns a remainder  Uses normal type rules for result  For any numbers x and y , the following equality holds: y = (y // x) * x + (y % x).

  31. Floor division and modulo example 31 dollars = product_cost_in_pennies // 100 cents = product_cost_in_pennies % 100

  32. Modules 32  Very few real computer programs are written from scratch  Too inefficient  Frequently use previously written code  Libraries  Python functions you previously wrote  We call both of these modules

  33. Importing modules 33  import command puts module in your program’s namespace  Access functions and variables in module with qualified name : math.sin(7.3)  Access documentation with help() and tab completion

  34. Video Question – What is the value the following expression? -3 ** 2  -9  -8  8  9

  35. Excel – cell referencing, formula dragging

  36. Excel: Relative and Absolute References  Every cell in Excel has a name: e.g., C7 (column C, row 7)  When written in an Excel expression, this is a relative reference  If moved/copied to another cell, it will change proportionally  If you move = 2 * C7 down two rows, it will become = 2 * C9  You can make absolute references by adding $ before row and/or column  $C$7 moved anywhere stays $C$7  $C7 moved two down and two to the right becomes $C9  C$7 moved two down and two to the right becomes E$7

  37. Video Question – If a relative reference is drug down in Excel, what changes?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend