Reference: Chapter 1 of Computational Nuclear Engineering and Radiological Science Using Python, R. McClarren (2018)
After studying this notebook, completing the activities, and asking questions in class, you should be able to:
In almost any computer language the first thing you do is create a program called "Hello world!".
This notebook is an interactive session. We just type the command print("Hello World.")
and the press Shift+Enter to execute.
print("Hello World.")
Hint: Use \n
to create a new line.
Unicode Characters
print("The temperature was 56 °F this morning.")
Above, we used the interactive mode, which means the result of each line is displayed when I enter the line.
It is more common to put your code into a separate file and then execute it by typing
python codename.py
where codename.py
is the name of your file.
How to access a Python interpreter?
python
Anaconda is the easiest free Python environment to install. It is already available in B19.
Hint: Start at https://www.anaconda.com/distribution/#download-section. Ask questions in tutorial if you get stuck.
Comments are your friend. They can be time consuming to add, but never have I looked back at old code and regreted adding them.
A comment is a annotation in your code to
This is not to punish you, but to help you.
You use the pound (aka hashtag) to comment out the rest of a line: everything that follows the # is ignored by python. Therefore, you can put little notes to yourself or others about what is going on in the code.
#This next line should compute 9*9 + 19 = 100
print(9*9 + 19)
#You can also make comments inside a line
9*9 #+ 19
There are also ways to make multiline comments by use a triple quote '''
'''The beginning of a multiline comment.
This comment will be followed by meaningless
code. Enjoy '''
print("I am the very model of a modern major general.")
'''
'''
#
r = 1.0
#
pi = 3.14159265359
# Calculate sphere area (m^2)
area = 4 * pi * r**2
print("Sphere surface area:",area,"m^2")
#
vol = 4/3 * pi * r**3
print("Sphere volume:",vol,"m^3")
We call errors in computer programs bugs. Even experienced programmers write code with bugs. We will learn best practice procedures to try to rid a code of bugs, but even the most sophisticated software quality assurance techniques will not catch every one. Let's look at a good bug and a bad bug.
Good bug: A syntax mistake that Python will catch and scream about.
Example
#This is a good bug because the python interpreter complains
9*9 +
Bad bug: A flaw in the logic of the computer program. These are much harder to catch.
Example: We want to compute
$$(3 + 5)^2 = 8^2 = 64$$But we input:
3 + 5**2
#You don't get the correct answer, and no one tells you that you're wrong.
(3 + 5)**2
This is an example of the power and feebleness of computers. A computer can do anything you tell it to, but it doesn't necessarily do what you want it to. Always keep this in mind: just because the computer did something, that doesn't mean it did what you wanted.
Later we will talk in more detail about bugs, finding bugs (called debugging), and testing of code.
Python is, by design, very picky about how you lay out your code. It requires that code blocks be properly indented. We'll discuss what code blocks are later, but your code needs to be properly aligned to make this work.
#If I improperly indent, my code won't work
print("Not indented")
print("indented, but it shouldn't be")
Notice that none of my code executed because of the indentation error. An indent tells the Python interpreter the indented code is part of a code block and needs to be executed in a special way. Only at certain times can one indent and it makes sense. We will revisit this in a few lectures.
print("Not indented")
print("indented, but it shouldn't be")
You can prompt the user for input using the input command:
user_value = input("Enter a number: ")
user_value = float(user_value)
print("2 *", user_value,"=", 2*user_value)
user_value = input("Enter a number (just type 7): ")
user_value = int(user_value)
if user_value == 7:
print("Correct, we have a winner")
print("Claim your prize")
else:
print("You guessed poorly.")
Notice that in this example we used an if-else statement. The if-else statement is a way to execute different parts of the code based on some conditional expression. This type of execution is sometimes called branching or selection. We will discuss this in detail below.
Also notice the indentation in the if-else block.
Answer to Home Activity:
Input that printed "claim your prize":
Input that printed "You guessed poorly":
Information, or data, is stored in variables. Common types of variables include floating point numbers (decimals), integer numbers, and text to name a few. For a complete list of the python data types, see https://docs.python.org/3.4/library/stdtypes.html
Integers are whole numbers, including the negatives. They never have a fractional, or decimal, part and should only be used for data that is a count.
To assign an integer to a variable use the equals sign.
#assign the value 2 to x
x = 2
print(x*2.0)
#check that x is an integer
type(x)
Integers are useful for things that can be counted: perhaps that number of times we execute a loop, the number of elements of a vector, or the number of students in a class.
#assign the value 2 to x
x = 2
print(x*2)
#check that x is an integer
type(x)
Floating point numbers are numbers that do have a fractional part. Most numbers in engineering calculations are floating point types.
#Now make some others floating point variables
y = 4.2
print("y =",y)
print(type(y))
#note that exponentiation is **
z = (x / y)**3
print("(2 / 4.2)**3 =",z)
The way that floating point numbers are represented on a computer has only a finite precision. That means we cannot represent a number exactly in many cases. We'll see later an example of how floating point accuracy can make a difference in a calculation.
Hint: 1E-20
is shorthand for $1 \times 10^{-20}$, which is sometimes called scientific notation.
x = 1E-10 + 1E-20
print(x)
There are almost every common mathematical function you might need for python already built in. To use them you have to import that math functions using the command import. In the code below, I set it up so that to use a math function you use the syntax math.[function] where [function] is the name of the function you want to call.
See https://docs.python.org/3.4/library/math.html for the complete list of built-in mathematical functions.
import math as m
#take cosine of a number close to pi
theta = 3.14159
trig_variable = m.cos(theta)
print("cos(",theta,") =",trig_variable)
#use the exponential to give me e
e = m.exp(1)
print("The base of the natural logarithm is",e)
#python has a built-in pi as well
print("The value of pi is",m.pi)
Notice how in the print statements, if I give it multiple arguments, it prints each with a space in between. The natural logarithm is just math.log and the base 10 logarithm is math.log10.
print("The natural log of 10 is",m.log(10))
print("The log base-10 of 10 is",m.log10(10))
There are two non-obvious mathematical operators, integer division: //, and the modulus (or remainder): %
# 7 / 3 is 2 remainder 1
print("7 divided by 3 is",7//3,"remainder",7%3)
print("851 divided by 13 is",851//13,"remainder",851%13)
# integer division
print("7 // 3 = ",7//3)
# floating division
print("7 / 3 = ",7/3)
Write a sentence to explain difference between integer and floating point division:
Let's also look at how to generate a random number.
import random
print("A random number between 0 and 1: ",random.random())
import random
print("A random number between 0 and 100: ",100*random.random())
A string is a data type that is a collection of characters, and needs to be inside quotes:
#This is a string
aString = "Coffee is for closers."
print(aString)
print("aString")
Anything inside the quotes is taken literally by python. That is why the second print statement above just printed aString.
You can also subset, that is get some of the characters in a string, using brackets. Putting a single number in a bracket gives you the character in that position. Note, python starts numbering at 0 so that 0 is the first character in a string.
aString[0]
aString[5]
You can also get a range of characters in a string using the colon. The colon operator is funny in a way in that [a:b] says give me the elements in the string from position a to position b-1.
aString[1:6]
Python defines its ranges this way so that if the string is of length N, [0:N] returns the whole string.
Negative subsets start at the end of the string (i.e., -1 is the last character of the string).
aString[-1]
aString[-5:-2]
aString[-8:-1]
With characters (and strings) the + operator is overloaded. What we mean by overloaded, is that it is defined so that the idea of what addition means is conferred to strings. Therefore,
'Negative Ghostrider: ' + 'the pattern is full'
'a' + 'b' + 'c'
The + operator concatenates (or smushes together) the strings/characters it operates on.
The multiplication operator is similarly overloaded, though it is not as general. It is simple to think about 'The roof, ' * 3
'The roof, ' * 3
However, 'The roof, ' * 3.15 is not defined and will give an error:
'The roof, ' * 3.15
The upshot is that only an integer times a character/string makes sense. The order of operations is respected
'The roof, ' * 3 + 'is on fire...'
Minus makes sense, but only sometimes, so it is not allowed.
'The roof'
"row "*3+"your boat gently down the stream"