Mithat Konar
based on Dierbach's Introduction to Computer Science using Python.
>>>
is the shell prompt>>> 4 + 6 10 >>> 3 * 7 21
+
, -
, *
, /
**
is exponentiation>>> 4 + 6 10 >>> 3 * 7 21 >>> 5 / 2 2.5 >>> 2 * (7 + 3) 20 >>> 2 ** 3 8
>>> import math >>> 4 * math.pi 12.566370614359172 >>> math.factorial(4) 24
>>> r = 5 >>> 2 * r 10
>>> city = 'Jakarata'
print()
to generate output.>>> print('Hello, there.') Hello there. >>> n = 5 >>> print(n) 5
input()
to get input.>>> name = input('What do I call you? ') What do I call you? Brian >>> print('Hi,', name) Hi, Brian
name = input('What do I call you? ') print('Hi,', name) print('Eat more vegetables.')
.py
will be added automatically.#
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.')
+
operator can be used to concatenate (put together) two string values.>>> name = 'Carol ' + 'Cleveland' >>> print(name) Carol Cleveland
str()
.>>> x = 99 >>> message = 'Number of red balloons: ' + str(x) >>> print(message) Number of red balloons: 99
int()
.>>> x = '99' >>> x + 1 # error because x is not a number but 1 is. >>> int(x) + 1 100
input()
in Python 3 will always return a string.num_balloons = input('How many balloons? ') num_balloons_converted = int(num_balloons) + 1
num_balloons = int(input('How many balloons? '))
print()
.>>> num = 99 >>> print("There are", num, "balloons.") There are 99 balloons
I need an exponent calculator.
What base? 10 What power of 10? 4 10 to the power of 4 is 10000
# 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)
>>> 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.
# 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)
>>> Enter the base: 10 >>> Enter the exponent: 4 Traceback (most recent call last): File "/home/mithat/exponent_calculator.py", line 8, in <module> print(base + ' to the ' + exponent + 'power is ' + result) TypeError: unsupported operand type(s) for +: 'int' and 'str'
Oops (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(str(base) + ' to the power of ' + str(exponent) + ' is ' + str(result))
>>> Enter the base: 10 >>> Enter the exponent: 4 >>> 10 to the power of 4 is 10000
Success. But we can do better…
# 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)
>>> 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.