In many of the more relaxed civilizations on the Outer Eastern Rim of the Galaxy, the Hitchhiker's Guide has already supplanted the great Encyclopaedia Galactica as the standard repository of all knowledge and wisdom, for though it has many omissions and contains much that is apocryphal, or at least wildly inaccurate, it scores over the older, more pedestrian work in two important respects. First, it is slightly cheaper; and secondly it has the words DON'T PANIC inscribed in large friendly letters on its cover.
-Douglas Adams, The Hitchhiker's Guide to the Galaxy
An abstract, step-by-step list of instructions for solving any instance of a problem.
One advantage of using algorithms to solve problems is that algorithms are an abstract solution, which means we can represent them using programming languages. This is where Python comes in. Python is a programming language that provides MANY different ways to represent the instructions of an algorithm. One way Python does this is through the use of functions. One of the functions that Python provides us is a way to display data to the screen. The equivalent Python function for this algorithm is called print().
In Python, the print() function displays output to the computer screen.
A function is a piece of pre-written code that performs an operation, usually on an argument.
#Print Function
print("Don't Panic")
In the example above, we are telling Python to pass the argument "Don't Panic" to the print() function and display it to the screen as output.
Data that is given to a function.
Often when one is designing a program to solve a problem, it is advantageous to have a way to store data. Python allows us to store data through the use of variables.
A variable is a means to hold a reference to a data object. A variable has a name, value, and data type.
The way we associate a variable with a value is by using an assignment statement.
An assignment statement takes a variable on the left side of an operator and associates it with the data and it's type on the right.
An operator performs operations on data, referred to as operands.
Operands are data to the left and right of operators.
#Variable
myVariable = "Don't Panic"
print(myVariable)
Let's analyze the example above. On the first line we are describing our code with a #comment. Comments provide a way to communicate to others what our program is doing. To create a comment in Python, you use the pound symbol (#), followed by a semantic description of your code.
On line two, we are creating a variable called myVariable and setting it equal to the string "Don't Panic". Notice that the first word of the variable, "my" is lowercase, and the next word is capitalized. This convention is referred to as camel-casing, and it is a good way to delineate your variables from other parts of your code.
The final line of our code should look similar to our example for the print() function. Instead of hard-coding our string however, we have simply passed in our variable as the argument.
A string is a sequence of characters. Strings are created in Python by using quotation marks.
Camel-Casing is a convention for creating variables. Variables that are camel-cased start lowercase, and capitalize the first letter of any following word.
Let's explore strings some more. As one can infer from the definition above, strings are often words or single characters. Strings are a primitive data type in Python, meaning that they are provided for us by the programming language. We will look at many more properties of strings in the future, but for now let's verify the data type of our variable by using the type() function.
#Type
myVariable = "Don't Panic"
print(type(myVariable))
If you are confused by the output above, remember our motto. "Don't Panic." We will take a look at classes later on, and 'str' is simply the abbreviation for string. Now let's take a look at some other primitive data types.
A primitive data type is a data object provided to a programmer by Python. In addition to the string data type, Python provides us with integers, floats, and booleans. Each data type describes not only the format of the data, but what the data is capable of as well. As an example, a string is displayed and interpreted as text, but it's characters actually correspond to ASCII values (A topic we shall revisit later).
#String
myString = "Don't Panic"
print(type(myString))
print(myString)
#Integer
myInteger = 42
print(type(myInteger))
print("The answer to Life, the Universe, and Everything is", myInteger)
#Float
myFloat = 3.14
print(type(myFloat))
print("The approximate value of pi is", myFloat)
It is often very useful to be able to state if a condition is True or False in a program. A boolean data type is one that can only have a value of True or False.
#Boolean
myBoolean = True
print(type(myBoolean))
print(myBoolean,"that!")
#Boolean
myBoolean = False
print(type(myBoolean))
print('The statement, "Vogon poetry sounds beautiful is..."',myBoolean)
In addition to primitive data types, Python also provides us with primitive operators. In our previous examples, we used the = operator to assign variables to some primitive data types. Along with assignment operators, we are also able to perform calculations on operands or data.
#Addition
operandOne = 21
operandTwo = 21
answerToTheUltimateQuestion = operandOne + operandTwo
print(answerToTheUltimateQuestion)
#Subtraction
operandOne = 84
operandTwo = 42
answerToTheUltimateQuestion = operandOne - operandTwo
print(answerToTheUltimateQuestion)
#Multiplication
operandOne = 21
operandTwo = 2
answerToTheUltimateQuestion = operandOne * operandTwo
print(answerToTheUltimateQuestion)
#Exponentiation
operand = 3
operandSquared = operand**2
print(operandSquared)
#Division
operandOne = 84
operandTwo = 2
answerToTheUltimateQuestion = operandOne / operandTwo
print(answerToTheUltimateQuestion)
#Floating Point Division
operandOne = 84
operandTwo = 2
answerToTheUltimateQuestion = operandOne / operandTwo
print(type(answerToTheUltimateQuestion))
print(answerToTheUltimateQuestion)
#Integer Division
operandOne = 84
operandTwo = 2
answerToTheUltimateQuestion = operandOne // operandTwo
print(type(answerToTheUltimateQuestion))
print(answerToTheUltimateQuestion)
#Integer Division Quirks
operandOne = 3
operandTwo = 2
answerToTheUltimateQuestion = operandOne // operandTwo
print(type(answerToTheUltimateQuestion))
print(answerToTheUltimateQuestion)
Now that we have looked at the intuitive primitive operators, let's look at one that may be new to a strag (strag: nonhitchhiker). Say we are out rock climbing, when all of the sudden (a sudden? I never know) we slip and get our arm jammed in a crevace. Hypothetically, if we were stuck there for 127 hours, how would we display the amount of hours in an amount that is meaningful to the average user? Python provides us with a neat operator named modulus ( % ) to help us out which will return the remainder of a division operation.
#Modulus
day = 24
hours = 127
print(hours/day)
print(hours//day)
print(hours % day)
print(hours,"hours is:",hours//day,"Days and",hours%day,"hours.")
You made it!
That's it for chapter 1. In the next chapter we will look at some basic programming concepts like input, processing and output. For now, go grab a pan galactic gargle blaster and celebrate. You deserve it.