Introduction to Github with Runpython - Runpython-IntroProgramming/Course-Syllabus GitHub Wiki

Now that you have had some experience working with the Python console, it is time to create some actual programs!

So far, using the console, you have typed in a Python command or statement, and immediately seen the result of it. If you wanted to perform a multi-step calculation, you could type series of Python statements, one after the other, and then use the final result. The problem is, if you want to repeat that process, then you have to type the statements in all over again.

A Python program is just a saved sequence of Python statements that you can execute over and over again.

Visit Runpython. You will see two large panes. The left-hand pane is a place where you can type Python statements. The right-hand pane is where Python will show you the results. Click on the line of code that is already in the left-hand pane and delete it so you have an empty window to work with.

Unlike the Python console, the results of your Python statements will not appear immediately after each statement in the program. Instead, they will appear in the right-hand pane.

Let's try a simple example:

In the left-hand pane, type the number 10, then enter to make a new line:

10

Right away you should notice that Python doesn't respond with:

10

on the next line. In fact, it doesn't do anything at all!

Running Programs

You can enter all the Python commands you want in the left-hand pane, but Python won't even look at them until you press the run button at the top of the screen to run the program.

Press the run button!

Uh oh.. did nothing happen again?

The second important difference between running Python programs and using the Python console is that Python programs don't keep regurgitating the values of objects that you create. So your program created the number 10, but didn't tell you anything.

Displaying Output

If you want your program to tell you something, you have to use the Python print function. Try entering this code in the left-hand pane (delete whatever was already there, first):

a = 10
print(a)

Now run the program. You should see the number 10 appearing in the right-hand pane.

A BIG Idea

One super important idea to remember about your programs is that Python starts working at the TOP of your program and goes DOWN from there, line by line, interpreting your Python commands as it goes. Python commands are not executed simultaneously, or in some random order. Always top to bottom.


Questions

In each case, make your own prediction, then try it out. Were you right? If not, make sure you understand why!

  1. Consider the following program:
a = 1
b = 2
c = a + b
print(c)

What (if anything) will be displayed in the right-hand pane?

  1. Consider the following program:
product = x * y
x = 15
y = 10
print(product)

What (if anything) will be displayed in the right-hand pane?


Accepting Input

So far, your programs haven't been interactive. That is, they behave in a very predictable way and always display the same thing. Programs get a lot more interesting if the person running the program gets to interact with it somehow.

Ask the user for input by using the input function.

Delete any code in the left-hand pane and copy/paste the following program:

name = input("What is your name? ")
print("Hi, " + name + ". I like you :)")

Run the program and see what happens.

input

The input function stops Python and waits for the person running the program to type something on the keyboard (followed by enter). The function returns a string object that is the same text that the user typed. In the example above, the text the user typed is assigned to the variable called name using the = (remember: the variable name on the left becomes a substitute for whatever is on the right).

Here is a larger example that uses both input and print. Erase your current program (when you're ready) and copy/paste this one in and try it out:


a = 99 # Start with the usual number of bottles

# What do we want to say
s = "{0} bottles on the wall. {0} bottles. Take one down, now there are only {1} bottles left"
print(s.format(a, a-1))
a = 98  # Now say it with a few different numbers to start with
print(s.format(a, a-1))
a = 97
print(s.format(a, a-1))

user = input("Please tell me your first name: ")

print("Hello, {0}. You are looking quite frisky today!".format(user))

print("Good bye!")

If you look at this example you will notice something new: format

This example shows how some Python objects, like strings, have functions associated with them. In this example, s was used to refer to a string ("{0} bottles..."). When s was printed, the parts of the string with {0} were replaced by values from other variables.

print(s.format(a, a-1))

The format method can be used on any string. The arguments passed to the format method (a and a-1 in this case) will replace the {0} and {1} sections inside the original string.

If you are not completely sure how this works, try making up some simple examples of your own.


Questions

In each case, make your own prediction, then try it out. Were you right? If not, make sure you understand why!

  1. Consider the following program:
s = "{0}"
print(s.format(1))

What (if anything) will be displayed in the right-hand pane?

  1. Consider the following program:
s = "{0} and {1} and {0}"
x = 15
y = 10
print(s.format(x,y))

What (if anything) will be displayed in the right-hand pane?

  1. Consider the following program:
age = input("What is your age?")
s1 = "You are {0} years old."
s2 = "In 5 years you will be {0} years old."
print(s1.format(age))
print(s2.format(age + 5))

Try this out in Runpython. Why doesn't it work? What would you change to fix it?


Saving Your Work on Github

You may have noticed that Runpython has a refresh button and a login button. How can you save your programs to run later? This is where Github comes in.

Before you continue, you will have to create a free account on Github. Go ahead and do this now if you haven't already. I'll wait for you...

  1. Log in to your Github account. Your main page should display a button labeled "New repository". If the "New repository" button doesn't appear then you should also see a "+" in the top right corner of the page. Ask for help if you don't see this. Press this button.

  2. Give your repository a name (keep it short and sweet). "test" might be a good name.

  3. Check the box that says, "Initialize this repository with a README". This is important!

  4. Press the button that says, "Create repository".

  5. You should now see your created repository (a collection of files) with one file, README.md, listed there. Click the "Create new file" button.

  6. In the box that says "Name your file...", type a name for the file. You will be creating a Python program, so the name should end in ".py". The name "test.py" might be a good one to start with for your first program.

  7. If everything has gone well, you should now be in an editor for the file you created. You can enter something short for now, like:

# test program by Joe Programmer
  1. Save, or commit the changed file by pressing the big button at the bottom of the page that reads, "Commit new file". You can (optionally) add a comment about the new file before pressing the button.

  2. Github will now show you a list of files in your repository. It should include README.md and test.py. Click on test.py.

  3. Github now shows you the file you just created. There is a button that allows you to edit it again, if you wish. Click on the URL, highlight it, then copy it using command-c or ctrl-c.

  4. Return to Runpython. Press the LOGIN button and give Github permission to work with runpython.org. Now when you return to runpython.org, you should see a COMMIT button at the top of the screen.

  5. Paste your Github file URL into the edit window at the upper left of the page and press LOAD. If everything is working correctly, you should see a copy of your new program in the left-hand pane. Now you can edit and improve your program in Runpython.

  6. As you continue to work on your program you can save your work back to Github at any time by pressing the COMMIT button. Try making a change in Runpython now, COMMIT it and verify that the changes appeared on the Github web site.

ALL of your work in this course will be saved to Github in this way.