View page as slide show

Python first steps

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

The Python language

IDLE

IDLE in interactive mode

>>> 4 + 6
10
>>> 3 * 7
21

Basic math

>>> 4 + 6
10
>>> 3 * 7
21
>>> 5 / 2
2.5
>>> 2 * (7 + 3)
20
>>> 2 ** 3
8

The standard library and import

>>> import math
>>> 4 * math.pi
12.566370614359172
>>> math.factorial(4)
24

Variables

>>> r = 5
>>> 2 * r
10
>>> city = 'Jakarata'

Basic input and output

>>> print('Hello, there.')
Hello there.
>>> n = 5
>>> print(n)
5
>>> name = input('What do I call you? ')
What do I call you? Brian
>>> print('Hi,', name)
Hi, Brian

Using IDLE to develop a program

name = input('What do I call you? ')
print('Hi,', name)
print('Eat more vegetables.')

Comments

# 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

>>> name = 'Carol ' + 'Cleveland'
>>> print(name)
Carol Cleveland

Converting values

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

More about input()

num_balloons = input('How many balloons? ')
num_balloons_converted = int(num_balloons) + 1
num_balloons = int(input('How many balloons? '))

More about print()

>>> num = 99
>>> print("There are", num, "balloons.")
There are 99 balloons

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.

Define the problem

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