A program is a set of instructions executed by a computer. Like functions in math, programs can take input and produce some kind of output. A program on your personal computer might take the buttons you press on your keyboard or mouse as input, and produce some kind of output on your screen or through your speakers. A program on your Metro board might take a button in your circuit as input, and produce output in the form of turning on a light.
For these tutorials, we are going to be coding directly on the website using datacamp interactive Python widgets. They look like this:
The window on the right labeled IPython Shell is called the command line or the terminal. If you type an instruction here and hit enter on your keyboard, Python will run the instruction and print the result. One of the simplest uses of Python is as an interactive calculator. Try typing 1 + 2 into the terminal and hit enter on your keyboard. Don't worry about the window on the left, or any of the buttons just yet.
Make sure you are typing your commands into the window labeled IPython Shell
Don't click any of the buttons on the left, instead hit "Enter" on your keyboard
The widget might take a bit to fully load. Wait for the little circle to turn green before hitting "Enter"
Try clicking inside of the IPython Shell window before hitting "Enter"
If your program runs and red text appears, you're getting an error. Make sure you've typed 1 + 2 exactly, and that there are no spaces or tabs before it.
Once you've gotten your first command to run, take a look at the table below. This table shows the arithmetic operators you can use in Python. Try running a few more expressions in the command line using some of these operators. Just like in math, Python obeys the order of operations when evaluating expressions and you can use parentheses () to give parts of the expression priority.
The command line is great, but it only lets us give the computer one instruction at a time. As our programs get more complicated, you can see how this becomes a problem. That's why we have scripts. A script is just a file that contains a series of Python instructions. By running the script we tell Python to follow each instruction in order.
Unlike instructions in the command line, running a script doesn't necessarily display anything. To display something in Python, we use a function called print(). We'll learn more about functions later. For now, copy the following line of code into the script.py window in the widget below and click the green "submit" button. Note that it might take a little bit of time for the button to load.
print("Hello world!")
Make sure you type your code into the script.py window
The submit button might take a while to load. Wait for the circle next to the button to turn green.
Make sure the code is on its own line, and that there are no spaces or tabs before the word print
Now we know how to display data, but what about saving it? One of the most useful aspects of computer programming is the ability to save data for later use. In Python, we can save data like numbers, letters, words, and True/False values in variables. Just like in math, a variable in Python has a name, and an associated value that can be changed. To declare a variable, we use the assignment operator = .
In the exercise below, we've defined four variables, all with different values. Try printing each variable so that we can see the data they hold. We've already done the first two for you.
(Note: notice that "Alice" is in quotation marks. Strings of characters, like words or sentences, in Python must be surrounded by double or single quotation marks so that Python knows where they start and end, "like this" or 'like this'. Variable names, however, do not go in quotation marks.)
By convention, variables in Python are all lowercase. If more than one word is used in a variable name, we separate them with an underscore like_this . This style of typing is called snake case. Variable names cannot start with a number, and can only contain letters, numbers, and underscores (A-z, 0-9, and _ ). Good variable names are concise, descriptive, and easy to read and understand.
A constant is a variable that is declared once in a program and never changed. Constants are useful in situations where one value is repeatedly used throughout a program. For example, if you want to use the value 3.1415926535 several times throughout your program, you might want to declare a variable called PI that you can use instead. By convention, constants are written in all capital letters, like so:
PI = 3.1415926535
GRAVITY = 9.81
LIGHTSPEED = 299792458
If you're coding in Python, try to stick to snake case for regular variables, and all capitals for constants. Because this is only convention, you might see some other styles of variables when looking at other peoples' code. Here are a few styles you might see.
One of the most important and useful things about variables is that they can be changed throughout your program. In Python, variables can be reassigned to any other value, regardless of data type. When you use the variable later, you will be using the most recently assigned value.
In this example, we've defined a variable called animal . First we assigned it the value "Dog" and printed it out. Then we changed it to "Cat" and printed it. Now it's your turn. Change the value one more time to "Frog", and print!
What if we want to save the results of one of the arithmetic expressions we practiced with earlier? Easy! Save it in a variable. Variables can even be used within expressions. When Python encounters a variable in evaluating an expression, it will look and find the most recently assigned value of that variable and replace it in the expression. For example, take a look at this script:
apples = 2 * 2 # apples = 4
fruit = apples + 2 # fruit = 6
fruit = fruit * 2 # fruit = 12 (We can use variables in their own assignments!)
fruit += 1 # fruit = 13 (This is the same as fruit = fruit + 1)
Let's practice arithmetic operators by calculating the prices of some fruit.
Have you noticed the statements in our code that # look like this ? Those are comments! By putting a pound sign (#) before a line in Python, we tell the computer to ignore everything after it. This lets us add information into our code to make it easier for humans to read and understand, without affecting the rest of the code.
# Check out this example!
# Python will ignore this line
print("But not this one")
print("This will still execute") # but none of this will
# Comments are so useful! :)