python:dierbach:chapter_2_data_and_expressions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python:dierbach:chapter_2_data_and_expressions [2016/07/28 21:06] – mithat | python:dierbach:chapter_2_data_and_expressions [2016/07/29 03:03] (current) – mithat | ||
---|---|---|---|
Line 12: | Line 12: | ||
===== Literals ===== | ===== Literals ===== | ||
- | {{http://thecatapi.com/api/images/get? | + | {{http://mkonar.org/assets/img/cat-silhouette-01-trans-64px.png}} |
===== What is a literal? ===== | ===== What is a literal? ===== | ||
* **literal**: | * **literal**: | ||
- | * **numeric literal**: contains only the digits | + | * **numeric literal**: contains only the digits |
* integer versus floating point value | * integer versus floating point value | ||
* '' | * '' | ||
Line 25: | Line 25: | ||
* Floating point values have limited range and precision. | * Floating point values have limited range and precision. | ||
- | * IEEE 754: ±10< | + | * IEEE 754: ±10< |
- | * **arithmetic overflow**: results when operation causes number to be “too bigâ€. | + | * **arithmetic overflow**: results when operation causes number to be “too big”. |
<code python> | <code python> | ||
Line 32: | Line 32: | ||
inf | inf | ||
</ | </ | ||
- | * **arithmetic underflow**: | + | * **arithmetic underflow**: |
<code python> | <code python> | ||
Line 131: | Line 131: | ||
' | ' | ||
</ | </ | ||
- | * Doesn’t work to break up a string. | + | * Doesn’t work to break up a string. |
===== Explicit Line Joining ===== | ===== Explicit Line Joining ===== | ||
- | * **explicit joining** (backslash) can be used in some situations where implicit joining | + | * **explicit joining** (backslash) can be used in some situations where implicit joining |
<code python> | <code python> | ||
Line 141: | Line 141: | ||
num_minutes * 60 | num_minutes * 60 | ||
</ | </ | ||
- | * Also doesn’t work to break up strings. | + | * Also doesn’t work to break up strings. |
===== Silly encoding example ===== | ===== Silly encoding example ===== | ||
Line 157: | Line 157: | ||
===== Variables and Identifiers ===== | ===== Variables and Identifiers ===== | ||
- | {{http://thecatapi.com/api/images/get? | + | {{http://mkonar.org/assets/img/cat-silhouette-01-trans-64px.png}} |
===== What is a variable? ===== | ===== What is a variable? ===== | ||
+ | |||
+ | * **variable**: | ||
+ | |||
+ | < | ||
+ | | ||
+ | num --> | ||
+ | | ||
+ | </ | ||
+ | ===== Assignment ===== | ||
+ | |||
+ | * **assignment**: | ||
+ | * **assignment operator**: '' | ||
+ | * '' | ||
+ | |||
+ | <code python> | ||
+ | foo = 7 | ||
+ | num = 1 + foo | ||
+ | foo = foo + 1 | ||
+ | </ | ||
+ | ===== Variable reassignment ===== | ||
+ | |||
+ | <code python> | ||
+ | num = 10 # +--------+ | ||
+ | k = num # num --> | ||
+ | # k --> | ||
+ | # +--------+ | ||
+ | </ | ||
+ | <code python> | ||
+ | k = 20 # +--------+ | ||
+ | # num --> | ||
+ | # +--------+ | ||
+ | # +--------+ | ||
+ | # k --> | ||
+ | # +--------+ | ||
+ | </ | ||
+ | ===== id() ===== | ||
+ | |||
+ | * Use '' | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | k = num | ||
+ | >>> | ||
+ | ??? # some number | ||
+ | >>> | ||
+ | ??? # same number as above | ||
+ | k = 20 | ||
+ | >>> | ||
+ | ??? # same as before | ||
+ | >>> | ||
+ | ??? # different | ||
+ | </ | ||
+ | ===== What is an identifier? ===== | ||
+ | |||
+ | * **identifier**: | ||
+ | * **identifier**: | ||
+ | * Rules of the name: | ||
+ | * letters, digits, and the underscore character only (no spaces) | ||
+ | * can’t start with a digit | ||
+ | * don’t use underscore for the first character for now | ||
+ | * can be as long as you want | ||
+ | |||
+ | ===== Keywords and help ===== | ||
+ | |||
+ | * **keyword** an identifier that has predefined meaning in a programming language. | ||
+ | * You can’t use a keyword as your own identifier in Python. | ||
+ | * Get a list of keywords: | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | help> keywords | ||
+ | # table of keywords appears | ||
+ | help> quit | ||
+ | >>> | ||
+ | </ | ||
+ | * Things you think should be keywords are not! | ||
+ | * Check “special” words using '' | ||
+ | |||
+ | ===== Example: Restaurant Tab Calculation ===== | ||
+ | |||
+ | RestaurantTab.py | ||
+ | |||
+ | <code python> | ||
+ | # Restaurant Tab Calculation Program | ||
+ | # This program will calculate a restaurant tab with a gift certificate | ||
+ | |||
+ | # initialization | ||
+ | tax = 0.08 | ||
+ | |||
+ | # program greeting | ||
+ | print(' | ||
+ | print(' | ||
+ | |||
+ | # get amount of gift certificate | ||
+ | amt_certificate = float(input(' | ||
+ | |||
+ | # cost of ordered items | ||
+ | print(' | ||
+ | |||
+ | appetizer_per1 = float(input(' | ||
+ | entree_per1 = float(input(' | ||
+ | drinks_per1 = float(input(' | ||
+ | dessert_per1 = float(input(' | ||
+ | |||
+ | print(' | ||
+ | |||
+ | appetizer_per2 = float(input(' | ||
+ | entree_per2 = float(input(' | ||
+ | drinks_per2 = float(input(' | ||
+ | dessert_per2 = float(input(' | ||
+ | |||
+ | # total items | ||
+ | amt_person1 = appetizer_per1 + entree_per1 + drinks_per1 + dessert_per1 | ||
+ | amt_person2 = appetizer_per2 + entree_per2 + drinks_per2 + dessert_per2 | ||
+ | |||
+ | # compute tab with tax | ||
+ | items_cost = amt_person1 + amt_person2 | ||
+ | tab = items_cost + items_cost * tax | ||
+ | |||
+ | # display amount owe | ||
+ | print(' | ||
+ | print(' | ||
+ | print(' | ||
+ | print(' | ||
+ | |||
+ | </ | ||
+ | ===== Operators ===== | ||
+ | |||
+ | {{http:// | ||
+ | |||
+ | ===== What Is an Operator? ===== | ||
+ | |||
+ | **operator**: | ||
+ | |||
+ | ===== Arithmetic operators ===== | ||
+ | |||
+ | ^symbol | ||
+ | |-x |negation | ||
+ | |x + y | ||
+ | |x - y | ||
+ | |x * y | ||
+ | |x / y | ||
+ | |x %%//%% y |truncating div. |25 %%//%% 10 |2 | | ||
+ | | | |25 %%//%% 10.0 |2.0 | | ||
+ | |x % y | ||
+ | |x %%**%% y |exponentiation | ||
+ | |||
+ | ===== Example: Your Place in the Universe ===== | ||
+ | |||
+ | PlaceInUniverse.py | ||
+ | |||
+ | <code python> | ||
+ | # Your Place in the Universe Program | ||
+ | |||
+ | # This program will determine the approximate number of atoms that a person is | ||
+ | # person consists of and the percent of the universe that they comprise | ||
+ | |||
+ | # Initialization | ||
+ | num_atoms_universe = 10e80 | ||
+ | weight_avg_person = 70 # 70 kg (154 lbs) | ||
+ | num_atoms_avg_person = 7e27 | ||
+ | |||
+ | # Program greeting | ||
+ | print(' | ||
+ | |||
+ | # Prompt for user's weight | ||
+ | weight_lbs = int(input(' | ||
+ | |||
+ | # Convert weight to kilograms | ||
+ | weight_kg = 2.2 * weight_lbs | ||
+ | |||
+ | # Determine number atoms and percentage of universe | ||
+ | num_atoms = (weight_kg / weight_avg_person) * num_atoms_avg_person | ||
+ | percent_of_universe = (num_atoms / num_atoms_universe) * 100 | ||
+ | |||
+ | # Display results | ||
+ | print(' | ||
+ | print(' | ||
+ | '% of the universe' | ||
+ | |||
+ | </ | ||
+ | ===== Expressions and Data Types ===== | ||
+ | |||
+ | {{http:// | ||
+ | |||
+ | ===== What is an expression? ===== | ||
+ | |||
+ | * **expression**: | ||
+ | * '' | ||
+ | * Has two subexpressions: | ||
+ | * '' | ||
+ | * '' | ||
+ | * Has two **subexpressions**: | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== Operator precedence and associativity ===== | ||
+ | |||
+ | * **operator precedence** determines which operations happen first. | ||
+ | * **operator associativity** determines the order when two operands have the same precedence. | ||
+ | |||
+ | ===== Python’s rules ===== | ||
+ | |||
+ | * Similar to math. | ||
+ | * So far we have: | ||
+ | |||
+ | ^operator | ||
+ | |%%**%% | ||
+ | |- (negation) | ||
+ | |*, /, %%//%%, %|left to right | | ||
+ | |+, - |left to right | | ||
+ | |||
+ | ===== Examples ===== | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | 5 | ||
+ | >>> | ||
+ | 1.5 | ||
+ | >>> | ||
+ | 8 | ||
+ | >>> | ||
+ | 512 | ||
+ | </ | ||
+ | ===== What is a data type? ===== | ||
+ | |||
+ | * Python differentiates between types of data. | ||
+ | * So far we have seen character strings and numbers. | ||
+ | * **data type**: a set of values and a set of operators that may be applied to those values. | ||
+ | * '' | ||
+ | * can’t do '' | ||
+ | |||
+ | ===== Python’s basic types ===== | ||
+ | |||
+ | * Python has many [[https:// | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * Use '' | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | <class ' | ||
+ | >>> | ||
+ | <class ' | ||
+ | >>> | ||
+ | <class ' | ||
+ | >>> | ||
+ | <class ' | ||
+ | </ | ||
+ | ===== Static versus dynamic typing ===== | ||
+ | |||
+ | * Python is a **dynamically typed** language. | ||
+ | * The same variable can store different type values at different times. | ||
+ | |||
+ | <code python> | ||
+ | x = 41 + 1 | ||
+ | print(x) | ||
+ | x = 'I ate a donut.' | ||
+ | print(x) | ||
+ | x = False | ||
+ | print(x) | ||
+ | </ | ||
+ | * In **statically typed** languages, once a variable is associated with a type, the type can’t be changed. | ||
+ | |||
+ | ===== Mixed-type expressions ===== | ||
+ | |||
+ | * Example: '' | ||
+ | |||
+ | ===== Type coercion ===== | ||
+ | |||
+ | * **type coercion**: the implicit (and automatic) conversion of one type to another. | ||
+ | |||
+ | '' | ||
+ | |||
+ | ===== Type conversion ===== | ||
+ | |||
+ | * **type conversion**: | ||
+ | |||
+ | <code python> | ||
+ | >>> | ||
+ | ' | ||
+ | >>> | ||
+ | 42 | ||
+ | >>> | ||
+ | 3 | ||
+ | >>> | ||
+ | 99.0 | ||
+ | >>> | ||
+ | Traceback (most recent call last): | ||
+ | File "< | ||
+ | ValueError: invalid literal for int() with base 10: ' | ||
+ | </ | ||
+ | ===== Example: Temperature Conversion Program ===== | ||
+ | |||
+ | TempConversion.py | ||
+ | |||
+ | <code python> | ||
+ | # Temperature Conversion Program (Fahrenheit to Celsius) | ||
+ | |||
+ | # This program will convert a temperature entered in Fahrenheit | ||
+ | # to the equivalent degrees in Celsius | ||
+ | |||
+ | # program greeting | ||
+ | print(' | ||
+ | |||
+ | # get temperature in Fahrenheit | ||
+ | fahrenheit = float(input(' | ||
+ | |||
+ | # calc degrees Celsius | ||
+ | celsius = (fahrenheit - 32) * 5 / 9 | ||
+ | |||
+ | # output degrees Celsius | ||
+ | print(fahrenheit, | ||
+ | | ||
+ | |||
+ | </ | ||
python/dierbach/chapter_2_data_and_expressions.1469739976.txt.gz · Last modified: 2016/07/28 21:06 by mithat