python:dierbach:chapter_1_python_first_steps

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
Last revisionBoth sides next revision
python:dierbach:chapter_1_python_first_steps [2016/07/28 00:06] mithatpython:dierbach:chapter_1_python_first_steps [2016/07/28 04:20] – [More about print()] mithat
Line 1: Line 1:
-~~SLIDESHOW yatil-mfk~~+~~SLIDESHOW~~ 
 +~~NOTOC~~ 
  
 ====== Python first steps ====== ====== Python first steps ======
 Mithat Konar\\  Mithat Konar\\ 
-from Dierbach'"Introduction to Computer Science using Python"+based on Dierbach'//[[http://www.wiley.com/WileyCDA/WileyTitle/productCd-EHEP002046.html|Introduction to Computer Science using Python]]//.
  
 ===== The Python language ===== ===== The Python language =====
  
   * Syntax is easy to read.   * Syntax is easy to read.
-  * Interpreted and can be executed interactively.+  * Interpreted 
 +  * Can be executed interactively.
   * Huge community.   * Huge community.
     * Lots of support.     * Lots of support.
Line 15: Line 17:
 ===== IDLE ===== ===== IDLE =====
  
-  * IDLE is Python''standardIDE.+  * [[https://docs.python.org/3/library/idle.html|IDLE]] is Python'"standardIDE.
   * Good for starting to learn Python.   * Good for starting to learn Python.
   * Many [[https://wiki.python.org/moin/IntegratedDevelopmentEnvironments)|other IDEs]].   * Many [[https://wiki.python.org/moin/IntegratedDevelopmentEnvironments)|other IDEs]].
Line 53: Line 55:
  
   * Python's core is limited.   * Python's core is limited.
-  * Add functionality by ''import''ing *modules*.+  * Add functionality by importing modules.
   * Lots of modules in the [[https://docs.python.org/3/library/)|standard library]].   * Lots of modules in the [[https://docs.python.org/3/library/)|standard library]].
 <code python> <code python>
Line 97: Line 99:
 ===== Using IDLE to develop a program ===== ===== Using IDLE to develop a program =====
  
-  * Create a new program file with //File > New File// or //Ctrl+N// on Linux and Windows. +  * Create a new program file with //File > New File// or //Ctrl+N// (Linux and Windows)
-  * Write code:+  * Write some code:
 <code python> <code python>
 name = input('What do I call you? ') name = input('What do I call you? ')
Line 157: Line 159:
 num_balloons = int(input('How many balloons? ')) num_balloons = int(input('How many balloons? '))
 </code> </code>
 +
 +===== More about print() =====
 +  * You can print more than one thing with ''print()''.
 +  * Separate each thing with a comma.
 +<code python>
 +>>> num = 99
 +>>> print("There are", num, "balloons.")
 +There are 99 balloons
 +</code>
 +  * Each "thing" will be converted into a string.
  
 ===== A slightly more substantial program ===== ===== A slightly more substantial program =====
Line 170: Line 182:
 > I need an exponent calculator. > I need an exponent calculator.
  
-Solution: Write a program that allows the user to enter any integer base and integer exponent, and displays the value of the base raised to that exponent.+  * Solution: Write a program that allows the user to enter any integer base and integer exponent, and displays the value of the base raised to that exponent.
  
-Observations: The user will enter the values from the keyboard. It would be good if the user got feedback when the result is printed. I will use Python because it will be easy to implement the solution with it. 
  
-===== Proposed interaction =====+ 
 +===== Define the problem ===== 
 +  * Observations: 
 +    * The user will enter the values from the keyboard. 
 +    * It would be good if the user got feedback when the result is printed. 
 +    * I will use Python because it will be easy to implement the solution with it. 
 +  * Proposed interaction:
 <code> <code>
 What base? 10 What base? 10
Line 204: Line 221:
  
 <code python> <code python>
-Enter the base: 10 +>>> Enter the base: 10 
-Enter the exponent: 4+>>> Enter the exponent: 4
 Traceback (most recent call last): Traceback (most recent call last):
   File "/home/mithat/exponent_calculator.py", line 7, in <module>   File "/home/mithat/exponent_calculator.py", line 7, in <module>
Line 230: Line 247:
  
 <code python> <code python>
-Enter the base: 10 +>>> Enter the base: 10 
-Enter the exponent: 4+>>> Enter the exponent: 4
 Traceback (most recent call last): Traceback (most recent call last):
   File "/home/mithat/exponent_calculator.py", line 8, in <module>   File "/home/mithat/exponent_calculator.py", line 8, in <module>
Line 240: Line 257:
 Oops (again). Oops (again).
  
-===== Code the solution (last time) =====+===== Code the solution (last time?) =====
  
 <file python exponent_calculator.py> <file python exponent_calculator.py>
Line 256: Line 273:
  
 <code python> <code python>
-Enter the base: 10 +>>> Enter the base: 10 
-Enter the exponent: 4 +>>> Enter the exponent: 4 
-10 to the power of 4 is 10000+>>> 10 to the power of 4 is 10000 
 +</code> 
 + 
 +Success. But we can do better... 
 + 
 +===== Code the solution (last time!) ===== 
 + 
 +<file python exponent_calculator.py> 
 +# Exponent Calculator 
 +# Exponent Calculator 
 +# Calculate a base to an exponent's power. 
 +# Mithat Konar 
 + 
 +base = int(input('Enter the base: ')) 
 +exponent = int(input('Enter the exponent: ')) 
 +result = base ** exponent 
 +print(base, 'to the power of', exponent, 'is', result) 
 +</file> 
 + 
 +===== Test the solution ===== 
 +<code python> 
 +>>> Enter the base: 10 
 +>>> Enter the exponent: 4 
 +>>> 10 to the power of 4 is 10000
 </code> </code>
  
-Success.+The code is easier to read and it still works
  
 ===== Document the solution ===== ===== Document the solution =====
  
-  * In this case, we began **documenting the program right from the start**:+  * In this case, we began **documenting the program right from the start**.
     * Writing out details of the problem.     * Writing out details of the problem.
     * Writing out the design of the solution.     * Writing out the design of the solution.
python/dierbach/chapter_1_python_first_steps.txt · Last modified: 2016/07/28 04:26 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki