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/29 01:36] – mithat | python:dierbach:chapter_2_data_and_expressions [2016/07/29 03:03] (current) – mithat | ||
---|---|---|---|
Line 2: | Line 2: | ||
~~NOTOC~~ | ~~NOTOC~~ | ||
- | ====== Data and Expressions ====== | ||
- | Mithat Konar\\ | ||
- | based on Dierbach' | ||
===== Contents ===== | ===== Contents ===== | ||
Line 231: | Line 228: | ||
* Get a list of keywords: | * Get a list of keywords: | ||
- | < | + | < |
>>> | >>> | ||
help> keywords | help> keywords | ||
- | ... | + | # table of keywords appears |
help> quit | help> quit | ||
>>> | >>> | ||
Line 287: | Line 284: | ||
print(' | print(' | ||
print(' | print(' | ||
+ | |||
</ | </ | ||
===== Operators ===== | ===== Operators ===== | ||
Line 298: | Line 296: | ||
===== Arithmetic operators ===== | ===== Arithmetic operators ===== | ||
- | Demonstration of simple table syntax. | + | ^symbol |
- | ^ Right^Left | + | |-x |negation |
- | | 12|12 | + | |x + y |
- | | 123|123 | 123 | + | |x - y |
- | | 1|1 | | + | |x * y |multiplication |
+ | |x / y |division | ||
+ | |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.txt · Last modified: 2016/07/29 03:03 by mithat