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

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
python:dierbach:chapter_2_data_and_expressions [2016/07/28 21:03] mithatpython:dierbach:chapter_2_data_and_expressions [2016/07/29 01:46] mithat
Line 1: Line 1:
 ~~SLIDESHOW~~ ~~SLIDESHOW~~
 ~~NOTOC~~ ~~NOTOC~~
 +
 +====== 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 12: Line 16:
 ===== Literals ===== ===== Literals =====
  
-<HTML> +{{http://mkonar.org/assets/img/cat-silhouette-01-trans-64px.png}} 
-<iframe width="560" height="315" src="https://www.youtube.com/embed/pxf_EVTJtPg" frameborder="0" allowfullscreen> +
-</iframe> +
-</HTML>+
 ===== What is a literal? ===== ===== What is a literal? =====
  
   * **literal**: a sequence of one or more characters that stands for itself.   * **literal**: a sequence of one or more characters that stands for itself.
-  * **numeric literal**: contains only the digits 0–9, a sign character (1 or 2) and a optional decimal point.+  * **numeric literal**: contains only the digits 0–9, a sign character (1 or 2) and a optional decimal point.
     * integer versus floating point value     * integer versus floating point value
     * ''%%e%%'' for scientific notation     * ''%%e%%'' for scientific notation
Line 27: Line 29:
  
   * Floating point values have limited range and precision.   * Floating point values have limited range and precision.
-  * IEEE 754: Â±10<sup>-308</sup> to Â±10<sup>308</sup> with 16 to 17 digits of precision. +  * IEEE 754: ±10<sup>-308</sup> to ±10<sup>308</sup> with 16 to 17 digits of precision. 
-  * **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 34: Line 36:
 inf inf
 </code> </code>
-  * **arithmetic underflow**: results when operation causes number to be â€œtoo small”.+  * **arithmetic underflow**: results when operation causes number to be too small”.
  
 <code python> <code python>
Line 133: Line 135:
       'Number of Credits:', total_credits, 'GPA:', current_gpa)       'Number of Credits:', total_credits, 'GPA:', current_gpa)
 </code> </code>
-  * 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 won’t.+  * **explicit joining** (backslash) can be used in some situations where implicit joining won’t.
  
 <code python> <code python>
Line 143: Line 145:
               num_minutes * 60                num_minutes * 60 
 </code> </code>
-  * Also doesn’t work to break up strings.+  * Also doesn’t work to break up strings.
  
 ===== Silly encoding example ===== ===== Silly encoding example =====
Line 159: Line 161:
 ===== Variables and Identifiers ===== ===== Variables and Identifiers =====
  
-<HTML> +{{http://mkonar.org/assets/img/cat-silhouette-01-trans-64px.png}} 
-<iframe width="560" height="315" src="https://www.youtube.com/embed/pxf_EVTJtPg" frameborder="0" allowfullscreen> +
-</iframe> +
-</HTML>+
 ===== 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