Chapter 1: Don't Panic

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


What is The Hitchhiker's Guide to Python?
The Hitchhiker's Guide to Python is a collection of Jupyter Notebooks that I have created during my time in college studying Computer Science. This guide has served me quite well, and I hope that it can help some fellow nerds out there learning Python in an entertaining and insightful manner. Each chapter will feature a Douglas Adams quote, followed by an introduction into a core concept of Computer Science.

How can I create my own Jupyter Notebooks?
If you would like to create your own Jupyter Notebooks, you can get up and running by creating a JuliaBox. I also created a Jupyter Notebook about Jupyter Notebooks here (Jupyter-ception!!) if you want to get more familiar with them. If you just want to learn some Computer Science, keep reading on!

What is Computer Science?
Admittedly, this can often-times be a challenging question to answer. One of the problems with answering this question is that "computers" really do not have much to do with Computer Science. Although computers are the tools Computer Scientists generally use, Computer Science is more about solving problems. Computer Scientists solve problems by creating algorithms. An algorithm is simply a step-by-step list of instructions for solving any instance of a problem. Therefore, in my opinion Computer Science is better defined as the study of the existence and nonexistence of algorithms.

Definition: Algorithm

An abstract, step-by-step list of instructions for solving any instance of a problem.

Print

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().

Definition: Print

In Python, the print() function displays output to the computer screen.

Definition: Function

A function is a piece of pre-written code that performs an operation, usually on an argument.

Example: Print Function
In [1]:
#Print Function

print("Don't Panic")
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.

Definition: Argument

Data that is given to a function.

Variables

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.

Definition: Variable

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.

Definition: 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.

Definition: Operator

An operator performs operations on data, referred to as operands.

Definition: Operand

Operands are data to the left and right of operators.

Example: Variables
In [2]:
#Variable

myVariable = "Don't Panic"

print(myVariable)
Don't Panic

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.

Definition: String

A string is a sequence of characters. Strings are created in Python by using quotation marks.

Definition: Camel-Casing

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.

In [3]:
#Type

myVariable = "Don't Panic"

print(type(myVariable))
<class 'str'>

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.

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).

In [4]:
#String

myString = "Don't Panic"

print(type(myString))
print(myString)
<class 'str'>
Don't Panic
  • As stated above, a string is a way to represent words or characters. Strings are very helpful for describing data that we output to the screen.
  • In [5]:
    #Integer
    
    myInteger = 42
    
    print(type(myInteger))
    print("The answer to Life, the Universe, and Everything is", myInteger)
    
    <class 'int'>
    The answer to Life, the Universe, and Everything is 42
    
  • Here we have an assignment statement that sets a variable equal to an integer. An integer is simply a whole number, or in other words, a number without any decimal places. Integers can be both positive and negative.
  • Also note that I used a comma to seperate two values in the print statement.
  • In [6]:
    #Float
    
    myFloat = 3.14
    
    print(type(myFloat))
    print("The approximate value of pi is", myFloat)
    
    <class 'float'>
    The approximate value of pi is 3.14
    
  • This assignment statement sets our variable equal to a float. A float is a real number with decimal points. A float can be positive and negative as well.
  • 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.

    In [7]:
    #Boolean
    
    myBoolean = True
    
    print(type(myBoolean))
    print(myBoolean,"that!")
    
    <class 'bool'>
    True that!
    
    In [8]:
    #Boolean
    
    myBoolean = False
    
    print(type(myBoolean))
    print('The statement, "Vogon poetry sounds beautiful is..."',myBoolean)
    
    <class 'bool'>
    The statement, "Vogon poetry sounds beautiful is..." False
    
  • It is important to note that, in the program above, our string uses single quotes on the outside and double quotes on the inside. Python provides this neat functionality because you need a way to output a quote without it being interpreted as a string. Similarly, if you need to print an apostraphe you can surround your string with double quotes. There are also other ways to acheive this functionality (escape characters), and we will study them further on.
  • Primitive Operators

    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.

    In [9]:
    #Addition
    
    operandOne = 21
    operandTwo = 21
    
    answerToTheUltimateQuestion = operandOne + operandTwo
    
    print(answerToTheUltimateQuestion)
    
    42
    
    In [10]:
    #Subtraction
    
    operandOne = 84
    operandTwo = 42
    
    answerToTheUltimateQuestion = operandOne - operandTwo
    
    print(answerToTheUltimateQuestion)
    
    42
    
    In [11]:
    #Multiplication
    
    operandOne = 21
    operandTwo = 2
    
    answerToTheUltimateQuestion = operandOne * operandTwo
    
    print(answerToTheUltimateQuestion)
    
    42
    
    In [12]:
    #Exponentiation
    
    operand = 3
    operandSquared = operand**2
    
    print(operandSquared)
    
    9
    
    In [13]:
    #Division
    
    operandOne = 84
    operandTwo = 2
    
    answerToTheUltimateQuestion = operandOne / operandTwo
    
    print(answerToTheUltimateQuestion)
    
    42.0
    
  • Note that our answer has a decimal point in it, yet our operands are integer data types. This is because Python actually provides us with two different division operators. Floating point division ( / ) returns a float, and integer division( // ) returns an integer.
  • In [14]:
    #Floating Point Division
    
    operandOne = 84
    operandTwo = 2
    
    answerToTheUltimateQuestion = operandOne / operandTwo
    
    print(type(answerToTheUltimateQuestion))
    print(answerToTheUltimateQuestion)
    
    <class 'float'>
    42.0
    
    In [15]:
    #Integer Division
    
    operandOne = 84
    operandTwo = 2
    
    answerToTheUltimateQuestion = operandOne // operandTwo
    
    print(type(answerToTheUltimateQuestion))
    print(answerToTheUltimateQuestion)
    
    <class 'int'>
    42
    
    In [16]:
    #Integer Division Quirks
    
    operandOne = 3
    operandTwo = 2
    
    answerToTheUltimateQuestion = operandOne // operandTwo
    
    print(type(answerToTheUltimateQuestion))
    print(answerToTheUltimateQuestion)
    
    <class 'int'>
    1
    
  • It is important to remember that integer division ONLY returns an integer. This means that your answer will always be rounded down to the nearest whole number. You need to be careful what division sign you use, because if you are expecting a float, you will lose the decimal precision if you use integer division (Rhyme!).
  • 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.

    In [17]:
    #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.")
    
    5.291666666666667
    5
    7
    127 hours is: 5 Days and 7 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.