Interacting with Python
Contents
Interacting with Python¶
Content modified under Creative Commons Attribution license CC-BY 4.0, code under BSD 3-Clause License © 2020 R.C. Cooper
These notebooks are a combination of original work and modified notebooks from Engineers Code learning modules. The learning modules are covered under a Creative Commons License, so we can modify and publish and give credit to L.A. Barba and N.C. Clementi.
Our first goal is to interact with Python and handle data in Python. But let’s also learn a little bit of background.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
What is Python?¶
Python was born in the late 1980s. Its creator, Guido van Rossum, named it after the British comedy “Monty Python’s Flying Circus.” His goal was to create “an easy and intuitive language just as powerful as major competitors,” producing computer code “that is as understandable as plain English.”
We say that Python is a general-purpose language, which means that you can use it for anything: organizing data, scraping the web, creating websites, analyzing sounds, creating games, and of course engineering computations.
Python is an interpreted language. This means that you can write Python commands and the computer can execute those instructions directly. Other programming languages—like C, C++ and Fortran—require a previous compilation step: translating the commands into machine language. A neat ability of Python is to be used interactively. [Fernando Perez](https://en.wikipedia.org/wiki/Fernando_Pérez_(software_developer) famously created IPython as a side-project during his PhD. The “I” in IPython stands for interactive: a style of computing that is very powerful for developing ideas and solutions incrementally, thinking with the computer as a kind of collaborator.
Why Python?¶
Because it’s fun! With Python, the more you learn, the more you want to learn. You can find lots of resources online and, since Python is an open-source project, you’ll also find a friendly community of people sharing their knowledge. And it’s free!
Python is known as a high-productivity language. As a programmer, you’ll need less time to develop a solution with Python than with most other languages. This is important to always bring up whenever someone complains that “Python is slow.” Your time is more valuable than a machine’s! (See the Recommended Readings section at the end of this lesson.) And if we really need to speed up our program, we can re-write the slow parts in a compiled language afterwards. Because Python plays well with other languages :–)
The top technology companies use Python: Google, Facebook, Dropbox, Wikipedia, Yahoo!, YouTube… Python took the No. 1 spot in the interactive list of The 2017 Top Programming Languages, by IEEE Spectrum (IEEE is the world’s largest technical professional society).
Python is a versatile language, you can analyze data, build websites (e.g., Instagram, Mozilla, Pinterest), make art or music, etc. Because it is a versatile language, employers love Python: if you know Python they will want to hire you. —Jessica McKellar, ex Director of the Python Software Foundation, in a 2014 tutorial.¶
Let’s get started¶
You could follow this first lesson using IPython. If you have it installed in the computer you’re using, you enter the program by typing ipython
on the command-line interface (the Terminal app on Mac OSX, and on Windows the PowerShell or a similar app).
A free service to try IPython online, right from your browser, is Python Anywhere. You can execute all the examples of this lesson in IPython using this service.
You can also use Jupyter: an environment that combines programming with other content, like text and images, to form a “computational narrative.” All of these lessons are written in Jupyter notebooks.
For this lesson, we will assume you have been guided to open a blank Jupyter notebook, or are working interactively with this lesson. On a blank Jupyter notebook, you should have in front of you the input line counter:
In[1]:
That input line is ready to receive any Python code to be executed interactively. The output of the code will be shown to you next to Out[1]
, and so on for successive input/output lines in IPython, or code cells in Jupyter.
Your first program¶
In every programming class ever, your first program consists of printing a “Hello” message. In Python, you use the print()
function, with your message inside quotation marks.
print("Hello world!!")
Hello world!!
Easy peasy!! You just wrote your first program and you learned how to use the print()
function. Yes, print()
is a function: we pass the argument we want the function to act on, inside the parentheses. In the case above, we passed a string, which is a series of characters between quotation marks. Don’t worry, we will come back to what strings are later on in this lesson.
Key concept: function¶
A function is a compact collection of code that executes some action on its arguments. Every Python function has a name, used to call it, and takes its arguments inside round brackets. Some arguments may be optional (which means they have a default value defined inside the function), others are required. For example, the print()
function has one required argument: the string of characters it should print out for you.
Python comes with many built-in functions, but you can also build your own. Chunking blocks of code into functions is one of the best strategies to deal with complex programs. It makes you more efficient, because you can reuse the code that you wrote into a function. Modularity and reuse are every programmer’s friends.
Python as a calculator¶
Try any arithmetic operation in IPython or a Jupyter code cell. The symbols are what you would expect, except for the “raise-to-the-power-of” operator, which you obtain with two asterisks: **
. Try all of these:
+ - * / ** % //
The %
symbol is the modulo operator (divide and return the remainder), and the double-slash is floor division.
2 + 2
4
1.25 + 3.65
4.9
5 - 3
2
2 * 4
8
7 / 2
3.5
2**3
8
Let’s see an interesting case:
9**1/2
4.5
Discuss with your neighbor:¶
What happened? Isn’t \(9^{1/2} = 3\)? (Raising to the power \(1/2\) is the same as taking the square root.) Did Python get this wrong?
Compare with this:
9**(1/2)
3.0
Yes! The order of operations matters!
If you don’t remember what we are talking about, review the Arithmetics/Order of operations. A frequent situation that exposes this is the following:
3 + 3 / 2
4.5
(3 + 3) / 2
3.0
In the first case, we are adding \(3\) plus the number resulting of the operation \(3/2\). If we want the division to apply to the result of \(3+3\), we need the parentheses.
Exercises:¶
Use Python (as a calculator) to solve the following two problems:
The volume of a sphere with radius \(r\) is \(\frac{4}{3}\pi r^3\). What is the volume of a sphere with diameter 6.65 cm?
For the value of \(\pi\) use 3.14159 (for now). Compare your answer with the solution up to 4 decimal numbers.
Hint: 523.5983 is wrong and 615.9184 is also wrong.
Suppose the cover price of a book is \(\$ 24.95\), but bookstores get a \(40\%\) discount. Shipping costs \(\$3\) for the first copy and \(75\) cents for each additional copy. What is the total wholesale cost for \(60\) copies? Compare your answer with the solution up to 2 decimal numbers.
print('V of r=6.65-cm sphere ={:.4f} cm^3'.format(4/3*3.14159*(6.65/2)**3))
print('total cost of 60 copies =${:.2f}'.format(60*24.95*0.6+59*0.75+3))
V of r=6.65-cm sphere =153.9796 cm^3
total cost of 60 copies =$945.45
To reveal the answers, highlight the following line of text using the mouse:
Answer exercise 1: 153.9796 Answer exercise 2: 945.45
945.45/60
15.7575
Variables and their type¶
Variables consist of two parts: a name and a value. When we want to give a variable its name and value, we use the equal sign: name = value
. This is called an assignment. The name of the variable goes on the left and the value on the right.
The first thing to get used to is that the equal sign in a variable assignment has a different meaning than it has in Algebra! Think of it as an arrow pointing from name
to value
.
We have many possibilities for variable names: they can be made up of upper and lowercase letters, underscores and digits… although digits cannot go on the front of the name. For example, valid variable names are:
x
x1
X_2
name_3
NameLastname
Keep in mind, there are reserved words that you can’t use; they are the special Python keywords.
OK. Let’s assign some values to variables and do some operations with them:
x = 3
y = 4.5
Exercise:¶
Print the values of the variables x
and y
.
Let’s do some arithmetic operations with our new variables:
x + y
7.5
2**x
8
y - 3
1.5
And now, let’s check the values of x
and y
. Are they still the same as they were when you assigned them?
print(x)
3
print(y)
4.5
String variables¶
In addition to name and value, Python variables have a type: the type of the value it refers to. For example, an integer value has type int
, and a real number has type float
. A string is a variable consisting of a sequence of characters marked by two quotes, and it has type str
.
z = 'this is a string'
w = '1'
What if you try to “add” two strings?
z + w
'this is a string1'
The operation above is called concatenation: chaining two strings together into one. Insteresting, eh? But look at this:
>> x + w
Error! Why? Let’s inspect what Python has to say and explore what is happening.
Python is a dynamic language, which means that you don’t need to specify a type to invoke an existing object. The humorous nickname for this is “duck typing”:
“If it looks like a duck, and quacks like a duck, then it’s probably a duck.”¶
In other words, a variable has a type, but we don’t need to specify it. It will just behave like it’s supposed to when we operate with it (it’ll quack and walk like nature intended it to).
But sometimes you need to make sure you know the type of a variable. Thankfully, Python offers a function to find out the type of a variable: type()
.
type(x)
int
type(w)
str
type(y)
float
More assignments¶
Here we assign a new variable to the result of an operation that involves other variables.
sum_xy = x + y
diff_xy = x - y
print('The sum of x and y is:', sum_xy)
print('The difference between x and y is:', diff_xy)
The sum of x and y is: 7.5
The difference between x and y is: -1.5
Notice what we did above: we used the print()
function with a string message, followed by a variable, and Python printed a useful combination of the message and the variable value. This is a pro tip! You want to print for humans. Let’s now check the type of the new variables we just created above:
type(sum_xy)
float
type(diff_xy)
float
Reflection point¶
When we created sum_xy
and diff_xy
two new variables were created that depended upon previously created variables x
and y
. How else can we accomplish this? Could we make a function? Could we combine the commands in one block as a script?
Special variables¶
Python has special variables that are built into the language. These are:
True
, False
, None
and NotImplemented
.
For now, we will look at just the first three of these.
Boolean variables are used to represent truth values, and they can take one of two possible values: True
and False
.
Logical expressions return a boolean. Here is the simplest logical expression, using the keyword not
:
not True
It returns… you guessed it… False
.
The Python function bool()
returns a truth value assigned to any argument. Any number other than zero has a truth value of True
, as well as any nonempty string or list. The number zero and any empty string or list will have a truth value of False
. Explore the bool()
function with various arguments.
bool(0)
False
bool('Do we need oxygen?')
True
bool('We do need oxygen')
True
None is not Zero: None
is a special variable indicating that no value was assigned or that a behavior is undefined. It is different than the value zero, an empty string, or some other nil value.
You can check that it is not zero by trying to add it to a number. Let’s see what happens when we try that:
a = None
b = 3
>>> a + b
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-71-ca730b97bf8a> in <module>
----> 1 a+b
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Logical and comparison operators¶
The Python comparison operators are: <
, <=
, >
, >=
, ==
, !=
. They compare two objects and return either True
or False
: smaller than, smaller or equal, greater than, greater or equal, equal, not equal. Try it!
x = 3
y = 5
x > y
False
We can assign the truth value of a comparison operation to a new variable name:
z = x > y
z
False
type(z)
bool
Logical operators are the following: and
, or
, and not
. They work just like English (with the added bonus of being always consistent, not like English speakers!). A logical expression with and
is True
if both operands are true, and one with or
is True
when either operand is true. And the keyword not
always negates the expression that follows.
Let’s do some examples:
a = 5
b = 3
c = 10
a > b and b > c
False
Remember that the logical operator and
is True
only when both operands are True
. In the case above the first operand is True
but the second one is False
.
If we try the or
operation using the same operands we should get a True
.
a > b or b > c
True
And the negation of the second operand results in …
not b > c
True
What if we negate the second operand in the and
operation above?
Note:¶
Be careful with the order of logical operations. The order of precedence in logic is:
Negation
And
Or
If you don’t rememeber this, make sure to use parentheses to indicate the order you want.
Exercise:¶
What is happening in the case below? Play around with logical operators and try some examples.
a > b and not b > c
True
What we’ve learned¶
Using the
print()
function. The concept of function.Using Python as a calculator.
Concepts of variable, type, assignment.
Special variables:
True
,False
,None
.Supported operations, logical operations.
Reading error messages.
References¶
Throughout this course module, we will be drawing from the following references:
Effective Computation in Physics: Field Guide to Research with Python (2015). Anthony Scopatz & Kathryn D. Huff. O’Reilly Media, Inc.
Python for Everybody: Exploring Data Using Python 3 (2016). Charles R. Severance. PDF available
Think Python: How to Think Like a Computer Scientist (2012). Allen Downey. Green Tea Press. PDF available
Problems¶
Calculate some properties of a rectangular box that is 12.5”\(\times\)11”\(\times\)14” and weighs 31 lbs
a. What is the volume of the box?
b. What is the average density of the box?
c. What is the result of the following logical operation,
volume>1000
(in inches^3)
V = 12.5*11*14
rho = 31/V
print('volume = {} in^3\ndensity = {} lb/in^3\nVolume>1000 in^3 =={}'.format(V,rho,V>1000))
volume = 1925.0 in^3
density = 0.016103896103896103 lb/in^3
Volume>1000 in^3 ==True
Use the variables given below,
str1
andstr2
, and check the followinga.
str1<str2
b.
str1==str2
c.
str1>str2
str1='Computational Mechanics'
str2='computational mechanics'
print('a. ',str1<str2)
print('b. ',str1==str2)
print('c. ',str1>str2)
print('d. ',str1.lower()==str2.lower())
a. True
b. False
c. False
d. True
The following code has an error, fix the error so that the correct result is returned:
y is 20 and x is less than y
x=1
y=20
if x<y and y==20:
print('y is 20 and x is less than y')
else:
print('x is not less than y')
Create a script that takes the clock hour in 24 hours and prints the clock hour with am or pm.
Hint: Use an if-else statement with
print
commands
test_time=10
if test_time<=12:
print(test_time,':00 AM')
elif test_time>12:
print(test_time-12,':00 PM')
10 :00 AM
test_time=16
if test_time<=12:
print(test_time,':00 AM')
elif test_time>12:
print(test_time-12,':00 PM')
4 :00 PM