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, Immutability Expressions and Operator Precedence, Module Imports Excel Referencing and Moving Formulas Between Cells
Objects and Literals
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 ->
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
Video Question – Objects have a WHAT and a WHAT?
Types and Representations
How ints are stored Integers are usually used for counting things
How strings are stored
Which of the following are considered ‘whitespace’? A) Spaces B) Tabs C) Newlines D) Spaces and Tabs E) Spaces, Tabs, and Newlines
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
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?
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
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
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
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()
Video Question – How many visible characters are printed with print('\\n\t\\t')?
Identifiers, Assignments, Immutability
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
Danger Noodle is not the only case art by @allison_horst
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
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
Video Question – What is the value of y after this code executes? x = 2 y = x + 3 x = 5 2 3 5 8
Expressions and Operator Precedence, Modules
Expressions Any Python code fragment that produces a value Can include: Literals Variables Operators Functions Right-hand side of assignment can be arbitrary expression
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
Order of operations (full gory details) highest lowest
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
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
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).
Floor division and modulo example 31 dollars = product_cost_in_pennies // 100 cents = product_cost_in_pennies % 100
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
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
Video Question – What is the value the following expression? -3 ** 2 -9 -8 8 9
Excel – cell referencing, formula dragging
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
Video Question – If a relative reference is drug down in Excel, what changes?
Recommend
More recommend