Session 1: Computers, Code, & Functions - Module C: Intro to Python

1. Programs:

Merriam-Webster definition:
    A plan for the programming of a mechanism (such as a computer).
    A sequence of coded instructions that can be inserted into a mechanism (such as a computer).

They can take many shapes and forms, but programs power and control everything a computing system can do.

From YouTube to Google's self driving cars programs are everywhere and are incredibly powerful!

A program is usually a text document that has a set of instructions the cpu or gpu can read and perform.

Often compared to a Chef's recipe, but for the computer.
A recipe may list the necessary ingredients/resources needed and then the instructions the Chef needs to follow.

A computer takes the human written lines of code and transforms them into a language it can understand. This language is written in binary. "0010"

2. Languages:

In order for programmers around the world to write code that both they and the computer can understand programming languages developed.

Languages define a structure and a set of rules for the programmer to follow so that the computer can translate it correctly, as well as any other programmer who knows the language can read and understand what it does.

We will be working and writing code in the Python programming language.

3. Syntax

The rules for a language's keywords, operators, symbols, white-space, usage, and more.

Python has a special syntax similar to other programming languages, but with it's own subtle differences:

    if, and, not, in, def, while, is

    +, - , * , /, =, ==, <, >, <=, >=, %, **, //

More Operators Like Parentheses, Braces, Brackets:
    ( ), { }, [ ]

    In python indentation separates blocks of code. Can be spaces or tabs but you should be consistent.

Data Structures:
    Lists, Sets, Tuples, Dictionaries

All of these have a special meaning that the programmers who use the language have agreed to follow. Do not worry if this is confusing, we will be looking at all of these later on this week. For now just recognize that a programming language has predefined tools and rules for a programmer to use and follow.

4. Let's get started programming!

We will be working in the programming language called Python.

Open up a terminal window.
(this is done by clicking the black terminal icon on the bottom left of your screen)

As you saw in the first lesson you can run commands in the terminal window.
We will be running a program called IPython.

One of the cool things about the Python programming language is that it can be run in an interactive shell. Do not worry too much about what this is, it simply allows the programmer to run code instantaneously and in a live environment.

When you type a line of code like "x = 5" the instruction is interpreted immediately and if it has an action to execute, it does so.

It allows us to “play” with code without having to save and manage program files.
Follow along as we go, and to begin let’s look at the example line of code above.

Variable assignment:

Try typing

x = 5

"5" is an integer value. An integer is a positive or negative number without any decimal values.

"x" is a variable. We can store items like numbers, strings, and more in "x".

"x" is like a box that we have labeled as "x".

"=" is assignment. We want to assign what is on the right into what is on the left.

The computer will read this instruction as assign the value "5" to "x".

"x" can now be used as if it is "5".


Python can work with many different types of values, so variables can hold many different types. Let’s look at 4 examples:

x = 5 is assigning an integer “5” to x. Integers are positive/negative numbers without any decimal values.

x = 5.5 is assigning a floating point (or double) type value to x. These have decimal values, and are floating point numbers.

x = "dog" is assigning a string type value to x. A string is a series of characters, linked together.

x = true is assigning a boolean type value to x. A boolean is either true or false.


Let’s double check by seeing what is inside of "x"
Try typing print x

"print" is an instruction to print to the terminal window, and we have specified to print the contents of the variable "x".

Using Variables:

Setting x = 5 could be useful, but by itself does not show the real use of variables.
Since we can name variables "almost" anything we want, let’s try something more useful.

Try typing this series of lines:

cookies = 10.2
brownies = 7.5
people = 3
host = “Amy”
isThereFood = true

Now we have three variables, which are the counts of the items they represent.
This could be useful.

As well as knowing who the host of the party is and whether or not there is food left.


Now we can use this data

Let’s see how many desserts we have, try typing:

desserts = brownies + cookies
print desserts

Now three people each want to eat a cookie.

We can update our variables:

cookies = cookies – people
print cookies
print desserts

Notice desserts has not changed, so we need to update it as well, in the same way.

To infinity and beyond:

You can do more math than just addition and subtraction with variables you have created.

What will each of these print?

print 4 + cookies + 4
print desserts - people
print brownies / 2
print people * 3

Save a screenshot of your terminal for your webpage. You can take a screenshot by first clicking the camera icon of the bottom left of the screen, then selecting Grab the current window followed by Take Screenshot.

Give the screenshot a unique name and save it in your public_html directory.

Please answer these questions on your webpage.