python:dierbach:chapter_1_python_first_steps
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_1_python_first_steps [2016/07/28 02:08] – mithat | python:dierbach:chapter_1_python_first_steps [2016/07/28 02:54] – mithat | ||
---|---|---|---|
Line 3: | Line 3: | ||
====== Python first steps ====== | ====== Python first steps ====== | ||
Mithat Konar\\ | Mithat Konar\\ | ||
- | from Dierbach' | + | based on Dierbach' |
===== The Python language ===== | ===== The Python language ===== | ||
* Syntax is easy to read. | * Syntax is easy to read. | ||
- | * Interpreted | + | * Interpreted |
+ | * Can be executed interactively. | ||
* Huge community. | * Huge community. | ||
* Lots of support. | * Lots of support. | ||
Line 15: | Line 16: | ||
===== IDLE ===== | ===== IDLE ===== | ||
- | * IDLE is Python' | + | * [[https:// |
* Good for starting to learn Python. | * Good for starting to learn Python. | ||
* Many [[https:// | * Many [[https:// | ||
Line 53: | Line 54: | ||
* Python' | * Python' | ||
- | * Add functionality by '' | + | * Add functionality by importing |
* Lots of modules in the [[https:// | * Lots of modules in the [[https:// | ||
<code python> | <code python> | ||
Line 97: | Line 98: | ||
===== Using IDLE to develop a program ===== | ===== Using IDLE to develop a program ===== | ||
- | * Create a new program file with //File > New File// or // | + | * Create a new program file with //File > New File// or // |
- | * Write code: | + | * Write some code: |
<code python> | <code python> | ||
name = input(' | name = input(' | ||
Line 157: | Line 158: | ||
num_balloons = int(input(' | num_balloons = int(input(' | ||
</ | </ | ||
+ | |||
+ | ===== More about print() ===== | ||
+ | * You can print more than one thing with '' | ||
+ | * Separate each thing with a comma. | ||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | There are 99 balloons | ||
+ | </ | ||
+ | * Each " | ||
===== A slightly more substantial program ===== | ===== A slightly more substantial program ===== | ||
Line 170: | Line 181: | ||
> 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. |
===== Define the problem ===== | ===== Define the problem ===== | ||
- | * Observations: | + | * 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: | * Proposed interaction: | ||
< | < | ||
Line 242: | Line 256: | ||
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 263: | Line 277: | ||
</ | </ | ||
- | Success. | + | Success. But we can do better... |
+ | |||
+ | Code the solution (last time!) | ||
+ | ------------------------------ | ||
+ | <code python> | ||
+ | # Exponent Calculator | ||
+ | # Exponent Calculator | ||
+ | # Calculate a base to an exponent' | ||
+ | # Mithat Konar | ||
+ | |||
+ | base = int(input(' | ||
+ | exponent = int(input(' | ||
+ | result = base ** exponent | ||
+ | print(base, 'to the power of', exponent, ' | ||
+ | </ | ||
+ | |||
+ | ===== Test the solution ===== | ||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | </ | ||
+ | |||
+ | The code is easier to read and it still works. | ||
===== Document the solution ===== | ===== Document the solution ===== |
python/dierbach/chapter_1_python_first_steps.txt · Last modified: 2016/07/28 04:26 by mithat