User Tools

Site Tools


python:dierbach:chapter_2_data_and_expressions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
python:dierbach:chapter_2_data_and_expressions [2016/07/28 20:42] – created mithatpython:dierbach:chapter_2_data_and_expressions [2016/07/29 01:46] mithat
Line 3: Line 3:
  
 ====== Data and Expressions ====== ====== Data and Expressions ======
 +Mithat Konar\\ 
 +based on Dierbach's //[[http://www.wiley.com/WileyCDA/WileyTitle/productCd-EHEP002046.html|Introduction to Computer Science using Python]]//.
  
 ===== Contents ===== ===== Contents =====
Line 14: Line 16:
 ===== Literals ===== ===== Literals =====
  
-{{http://lorempixel.com/480/360/cats}} +{{http://mkonar.org/assets/img/cat-silhouette-01-trans-64px.png}}
  
 ===== What is a literal? ===== ===== What is a literal? =====
Line 160: Line 161:
 ===== Variables and Identifiers ===== ===== Variables and Identifiers =====
  
-{{http://lorempixel.com/480/360/cats}}+{{http://mkonar.org/assets/img/cat-silhouette-01-trans-64px.png}}
  
 ===== What is a variable? ===== ===== What is a variable? =====
 +
 +  * **variable**: a name associated with a value.
 +
 +<code>
 +       +--------+
 +num -->  10   |
 +       +--------+
 +</code>
 +===== Assignment =====
 +
 +  * **assignment**: take the value resulting from the expression on the right and copy it into the variable on the left.
 +  * **assignment operator**: ''%%=%%'' in Python.
 +    * ''%%=%%'' is //not// “equals”!
 +
 +<code python>
 +foo = 7
 +num = 1 + foo
 +foo = foo + 1
 +</code>
 +===== Variable reassignment =====
 +
 +<code python>
 +num = 10    #        +--------+
 +k = num     # num -->  10   |
 +            #   k -->       | 
 +            #        +--------+
 +</code>
 +<code python>
 +k = 20      #        +--------+
 +            # num -->  10   |
 +            #        +--------+
 +            #        +--------+
 +            #   k -->  20   |
 +            #        +--------+
 +</code>
 +===== id() =====
 +
 +  * Use ''%%id()%%'' to see if two variables are pointing to the same thing.
 +
 +<code python>
 +>>> num = 10
 +k = num
 +>>> id(num)
 +??? # some number
 +>>> id(k)
 +??? # same number as above
 +k = 20
 +>>> id(num)
 +??? # same as before
 +>>> id(k)
 +??? # different
 +</code>
 +===== What is an identifier? =====
 +
 +  * **identifier**: a sequence of one or more characters used to provide a name for a given program element.
 +  * **identifier**: names of things.
 +  * 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()
 +help> keywords
 +# table of keywords appears
 +help> quit
 +>>> 
 +</code>
 +  * Things you think should be keywords are not!
 +  * Check “special” words using ''%%'testword' in dir(__builtins__)%%''
 +
 +===== 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('This program will calculate a restaurant tab for a couple with')
 +print('a gift certificate, with a restaurant tax of', tax * 100, '%\n')
 +
 +# get amount of gift certificate
 +amt_certificate = float(input('Enter amount of the gift certificate: '))
 +
 +# cost of ordered items
 +print('Enter ordered items for person 1')
 +
 +appetizer_per1 = float(input('Appetizier: '))
 +entree_per1 = float(input('Entree: '))
 +drinks_per1 = float(input('Drinks: '))
 +dessert_per1 = float(input('Dessert: '))
 +
 +print('\nEnter ordered items for person 2')
 +
 +appetizer_per2 = float(input('Appetizier: '))
 +entree_per2 = float(input('Entree: '))
 +drinks_per2 = float(input('Drinks: '))
 +dessert_per2 = float(input('Dessert: '))
 +
 +# 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('\nOrdered items: $', format(items_cost, '.2f'))
 +print('Restaurant tax: $', format(items_cost * tax, '.2f'))
 +print('Tab: $', format(tab - amt_certificate, '.2f'))
 +print('(negative amount indicates unused amount of gift certificate)')
 +</code>
 +===== Operators =====
 +
 +{{http://mkonar.org/assets/img/cat-silhouette-01-trans-64px.png}}
 +
 +===== What Is an Operator? =====
 +
 +**operator**: a symbol that represents an operation that may be performed on one or more operands. **binary operator**: takes two operands **unary operator**: takes one operand
 +
 +===== Arithmetic operators =====
 +
 +^Operator  ^               ^Example       ^Result  ^
 +|-x        |negation       |-10           |-10     |
 +|x + y     |addition       |10 + 25       |35      |
 +|x - y     |subtraction    |10 - 25       |-15     |
 +|x * y     |multiplication |10 * 5        |50      |
 +|x / y     |division       |25 / 10       |2.5     |
 +|x %%//%% y|truncating div.|25 %%//%% 10  |2       |
 +|x % y     |modulus        |25 % 10       |2.0     |
 +|x %%**%% y|exponentiation |10 %%**%% 2   |100     |
 +
  
python/dierbach/chapter_2_data_and_expressions.txt · Last modified: 2016/07/29 03:03 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki