Today's Topics

Designing a full program

Suppose we are given the following problem: Have the user enter two integers. Then, print out the sum of these integers. Print out their product too.

Here are some steps a computer scientist might use to solve the problem:

  1. First think about how you would solve the problem. no keyboards, mice, or even writing utensils. Just think.
  2. Write or sketch a solution on paper. Discuss you solution with a friend or neighbor. This solution does not have to be in python. pseudocode is fine.
  3. Write your solution in python using your editor (atom).
  4. Save, run, and test your solution. Go back and make changes if needed.

The real innovation is in steps 1 and 2. Steps 3 and 4 are sometimes skipped, argued logically/mathematically, or handed off to new hires, grad students, or little brothers. Always do step 4 if you do step 3.

When you do write python code, it's good practice to comment. You've seen large Python comment blocks enclosed in triple-quotes. You can also provide a single-line of comment by using the # symbol.

Programming Style

You will soon see in Python and other programming languages how to combine simple statements to create complex powerful code. This takes practice and experience. For example, consider the following snippet of code from last week:

# store user input in numtext as a string
numtext = input("Enter a number: ")
# convert user input to int, save in num
num = int(numtext)

The first line calls input to ask the user of your program (i.e., the person who is running the python program you wrote) for a number. This gets returned by the input function as a string and stored in numtext. input always returns a string, even if it is supposed to represent a number. The second line converts the string value contained in numtext, converts it to an int, and saves the value in a variable called num.

It's possible to combine the code into a single line of python code

# get int from user, store in num
#   don't forget to convert from str to int
num = int(input("Enter a number: "))

Both snippets of code are legal Python. Which is better or preferable is a matter of taste. Our general advice to use the former style until you're very comfortable with Python syntax. Beginning students tend to over-complicate code, which makes code harder to read and more likely to have errors. try to keep your code simple.

Python math operators

Mathematical calculations are one of the most common uses of computer programming. Python supports most of the common math operations, but because the syntax is specific.

You can also combine many operators in one statement e.g. 2*x+10

Quick practice

Take a minute or two, open up a python interpreter window, and play around with math operators. Do they do anything unexpected? Try to combine them in larger expressions. What happens?

$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> 5+2
7
>>> 5-2
3
>>> 5*2
10
>>> 5/2
2.5
>>> 5//2
2
>>> 2**5
32
>>> 2**32
4294967296
>>> 19/5
3.8
>>> 19//5
3
>>> 19%5
4
>>> 3*5 + 4
19
>>> quit()

String Operators

Some math operators can be used with strings: * Use + for string concatenation.

>>> hw = "hello" + "world!"
>>> print(hw)
helloworld!

Expressions, operator precedence

An expression is a combination of values, variables, operators, outputs of Python commands like int or input, and even other expressions! The evaluation of an expression produces a value. e.g. the evaluation of 2*19+3 is 41. This is why expressions can appear on the right-hand side of assignment statements. Large mathematical expressions are common in Python.

Import Math Library

Python supports many other useful mathematical functions, but these are stored in separate library. A library is a separate collection of functionality not part of the main Python language.

Programming Exercise

With the remaining time, write a program to compute the area of a circle. First, ask the user for a radius. Then, print out the area using the formula area = pi * r^2

Extra challenge: write a program that computes the radius of a circle, given the area.

$ python3 circle.py
Enter the radius of a circle: 3
The area of a circle of radius 3 equals: 28.274333882308138