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 revisionNext revisionBoth sides next revision | ||
python:dierbach:chapter_2_data_and_expressions [2016/07/29 00:09] – mithat | python:dierbach:chapter_2_data_and_expressions [2016/07/29 01:36] – mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
~~SLIDESHOW~~ | ~~SLIDESHOW~~ | ||
~~NOTOC~~ | ~~NOTOC~~ | ||
- | |||
====== Data and Expressions ====== | ====== Data and Expressions ====== | ||
Mithat Konar\\ | Mithat Konar\\ | ||
based on Dierbach' | based on Dierbach' | ||
- | |||
===== Contents ===== | ===== Contents ===== | ||
Line 17: | Line 15: | ||
===== Literals ===== | ===== Literals ===== | ||
- | {{http:// | + | {{http:// |
===== What is a literal? ===== | ===== What is a literal? ===== | ||
Line 162: | Line 160: | ||
===== Variables and Identifiers ===== | ===== Variables and Identifiers ===== | ||
- | {{http:// | + | {{http:// |
===== 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: | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | help> keywords | ||
+ | ... | ||
+ | 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 ===== | ||
+ | |||
+ | Demonstration of simple table syntax. | ||
+ | ^ Right^Left | ||
+ | | | ||
+ | | 123|123 | ||
+ | | 1|1 | ||
python/dierbach/chapter_2_data_and_expressions.txt · Last modified: 2016/07/29 03:03 by mithat