+
, -
, *
, /
//
**
, mod %
+
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:
atom
).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.
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.
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.
+
, -
, *
, and /
represent addition, subtraction, multiplication, and division. When you add, subtract, or multiply two integers, you get an integer. When you add, subtract, or multiply two floats, you get a float. Any addition, subtraction, multiplication, or division that involves one float and one integer results in a float answer.
Division using '/' in python3 always results in a float, regardless of the input types. Perhaps this makes sense to you. Why wouldn't 3/2 be 1.5? If this is the case, python3
agrees with you, but in many other programming languages, including C and C++ used in CS31 and C35, the rules for division are a bit different.
Sometimes, you might want to divide an integer by another and get an integer instead of a decimal number. The //
operator rounds the value down returning an integer, e.g. 3//2 returns 1.
Exponents. To raise a number to a power, use the exponential operator **
.
mod. Python also supports division with remainder using the "mod" operator %
. We'll explain this more when we need it.
You can also combine many operators in one statement e.g. 2*x+10
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()
Some math operators can be used with strings: * Use +
for string concatenation.
>>> hw = "hello" + "world!"
>>> print(hw)
helloworld!
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.
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.
from math import pi, sin, cos, sqrt
once at the top of your program, even before the def main():
blocksqrt(64)
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