User Tools

Site Tools


python:dierbach:chapter_1_python_first_steps-form-md

View page as slide show

Python first steps

Mithat Konar
based on Dierbach's Introduction to Computer Science using Python

The Python language

  • Syntax is easy to read.
  • Interpreted and can be executed interactively.
  • Huge community.
    • Lots of support.
    • Lots of open source modules.

IDLE

  • IDLE is Python’s ‘standard’ IDE.
  • Good for starting to learn Python.
  • Many other IDEs.

IDLE in interactive mode

  • Often called REPL (read-evaluate-print-loop) mode.
  • >>> is the shell prompt
>>> 4 + 6
10
>>> 3 * 7
21

Basic math

  • +, -, *, /
  • ** is exponentiation
  • Note the use of parenthesis in multiplication.
>>> 4 + 6
10
>>> 3 * 7
21
>>> 5 / 2
2.5
>>> 2 * (7 + 3)
20
>>> 2 ** 3
8

The standard library and import

  • Python’s core is limited.
  • Add functionality by importing modules.
  • Lots of modules in the standard library.
>>> import math
>>> 4 * math.pi
12.566370614359172
>>> math.factorial(4)
24

Variables

  • variable: a name associated with a value.
>>> r = 5
>>> 2 * r
10
  • Variables can store character string values as well.
>>> city = 'Jakarata'

Basic input and output

  • Use print() to generate output.
>>> print('Hello, there.')
Hello there.
>>> n = 5
>>> print(n)
5
  • Use input() to get input.
>>> name = input('What do I call you? ')
What do I call you? Brian
>>> print('Hi,', name)
Hi, Brian

Using IDLE to develop a program

  • Create a new program file with File > New File or Ctrl+N (Linux and Windows).
  • Write code:
name = input('What do I call you? ')
print('Hi,', name)
print('Eat more vegetables.')
  • Save the file: File > Save (Ctrl+S) or File > Save As … (Ctrl+Shift+S)
    • File extension .py will be added automatically.
  • Run: Run > Run Module (F5)

Comments

  • Comments let you write notes to yourself or the reader of your code.
  • Anything after a # will be ignored by the interpreter.
# This is my first Python program.
name = input('What do I call you? ')  # get user's name
print('Hi,', name)  # print name entered by user
print('Eat more vegetables.')

More about strings

  • The + operator can be used to concatenate (put together) two string values.
>>> name = 'Carol ' + 'Cleveland'
>>> print(name)
Carol Cleveland

Converting values

  • Convert a number to a string with str().
>>> x = 99
>>> message = 'Number of red balloons: ' + str(x)
>>> print(message)
Number of red balloons: 99
  • Convert a string to an integer with int().
>>> x = '99'
>>> x + 1 # error because x is not a number but 1 is.
>>> int(x) + 1
100

More about input()

  • input() in Python 3 will always return a string.
  • If you expect a number, convert it:
num_balloons = input('How many balloons? ')
num_balloons_converted = int(num_balloons) + 1
  • Combining getting input and converting:
num_balloons = int(input('How many balloons? '))

More about print()

  • You can print more than one thing with print().
  • Separate each thing with a comma.
>>> num = 99
>>> print("There are", num, "balloons.")
There are 99 balloons
  • Each “thing” will be converted into a string.

A slightly more substantial program

  1. Define the problem.
  2. Design a solution.
  3. Code the solution.
  4. Test the solution.
  5. Document the solution.

Define the problem

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.

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:
What base? 10
What power of 10? 4 
10 to the power of 4 is 10000

Design a solution

  1. Get the base from the user.
  2. Get the exponent from the user.
  3. Calculate base to the exponent power.
  4. Print “base to the power of exponent is result.”

Code the solution

# Exponent Calculator
# Calculate a base to an exponent's power.
# Mithat Konar
 
base = input('Enter the base: ')
exponent = input('Enter the exponent: ')
result = base ** exponent
print(base + ' to the power of ' + exponent + ' is ' + result)

Test the solution

>>> Enter the base: 10
>>> Enter the exponent: 4
Traceback (most recent call last):
  File "/home/mithat/exponent_calculator.py", line 7, in <module>
    result = base ** exponent
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'str'

Oops. Back to step 3.

Code the solution (again)

# 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)

Test the solution

>>> Enter the base: 10
>>> Enter the exponent: 4
Traceback (most recent call last):
  File "/home/mithat/lesson-dev.py", line 8, in <module>
    print(base + ' to the ' + exponent + 'power is ' + result)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Oops (again).

Code the solution (last time?)

# 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(str(base) + ' to the power of ' + str(exponent) + ' is ' + str(result))

Test the solution

>>> Enter the base: 10
>>> Enter the exponent: 4
>>> 10 to the power of 4 is 10000

Success. But we can do better…

Code the solution (last time!)

# 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)

Test the solution

>>> Enter the base: 10
>>> Enter the exponent: 4
>>> 10 to the power of 4 is 10000

The code is easier to read and it still works.

Document the solution

  • In this case, we began documenting the program right from the start.
    • Writing out details of the problem.
    • Writing out the design of the solution.
    • Using comments in the source code.
  • Do this.
  • Every. Single. Time.
python/dierbach/chapter_1_python_first_steps-form-md.txt · Last modified: 2016/07/28 04:48 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki