Python Tutorial

Chapter 2
by Marshall Brain

In the first tutorial we learned how to write the simplest computer programs using the Python programming language. We learned how to create output from a computer program. In this turotial we will learn about variables and input. With input, we can ask the user questions and use the answers to control our programs.

Here is the simplest possible example. In Chapter 1 we wrote a program that printed "Hello World!" 20 times. What if we want to let the user control how many times "Hello World!" gets printed? It would look like this:

When you run this program, it will ask you to enter a number. If you enter 10, it will print "Hello World!" 10 times. If you enter 3, it will print it 3 times. And so on. Enter any number you like.

What is happening in the code? In the first line, "i" is called a variable. We have created it, and we can name it almost anything we like (except for special Python words, like "for" or "print"). A variable is a place where we can store a value we want to remember. In this case, we want to get input from the use and remember it in the place called "i". If we want (and in bigger programs we definitely want to do this to avoid confusion), we could use a longer, more descriptive names. In stead of "i" we could have called our variable "NumberEnteredByUser" if we want to be super descriptive. In this case, "i" is easier to type so we will use it.

The "input" command in Python prompts the user with a question (we can type anything we like for the question) and then waits for the user to type something on the keyboard. The user types a number and presses enter.

Then the for loop uses the user's input and prints "Hello World!" the right number of times.

What is the little piece of code that says, "int(i)"? This is called "type casting", and we will learn more about it in later tutorials. For Python to run this code, we need to make sure it has converted the variable i to an integer. The code "int(i)" does that.

What if we want to get two pieces of input instead of 1? That's easy, as shown here:

We simply declare a second variable j for the second input value, and declare k to hold the sum. The third line of code tells the computer to add i and j together and place the result into the variable k.

What if we want to print all 3 variables? We change the print line to print all three, like this:

We have 3 "%d" place holders in the print statement, and then three variables that we want to put in those placeholders. We typecast i and j, but why didn't we need to typecast k? It's because Python "knows" that k is an integer based on the value we put into it - the sum of two integers. You can typecast k to int if you want - that won't hurt anything. But it is not required like it is with i and j.

So then you might ask, "why is it required for i and j?" It's because, when the user typed in a number in the input statements, Python accepted the number as a string of characters. Python would accept a string of characters "abc" as well, or anything else the user types. We are hoping the user's string contains one or more valid digits, and then the "int(i)" statement converts that string of digits into an actual integer number for the addition statement. What if the user enters "abc" instead of a valid number like "5"? Try it!

This, by the way, is what "learning to program a computer" is about. To write a computers program in the Python language, you have to learn and then follow the rules of the Python language. Typecasting is one of those things that seems a little weird at first, but then you get used to it and it becomes second nature - you do it automatically.

Now that you can accept user input and create output for the user, you can do all sorts of things with a computer. One thing scientists and engineers do with computers is simulations. They simulate things like weather patterns, ocean currents, traffic flow, air flow through an engine, stresses on a building, etc. In the next tutorial, we will create the simplest possible simulation...

<<< Go to Tutorial 1   |   Intro   |   Go to Tutorial 3 >>>




You may also enjoy this in-depth interview, "Marshall Brain on Singularity 1on1: We're approaching humanity’s make or break period":

On the coming Singularity and Artificial Intelligence:

A Conversation with Marshall Brain from MIRI on Vimeo.

See Also

About the Author
Robotic Nation
Robots in 2015
Robotic Freedom
Robotic Nation FAQ
Robotic Nation Evidence
Basic Income
Discard your body
Manna - the book
Science on the Brain
Careful Parents
Star Wars
How God Works

How to make a million dollars

Reviews
Salon
Wired
LiveScience
LATimes Editorial
Geek of the week

Thanks for visiting today,

© Copyright 2017 by Marshall Brain. All rights reserved.