A Beginner's Python Tutorial/Variables, Scripts

Well, we can make one-liner programs. So what? You want to send programs to other people, so that they can use them, without knowing how to write them.

Editing in Notepad
Writing programs in Python to a file is very easy. Python programs are simply text documents—you can open them up in Notepad (or another text editor), and have a look at them, just like that. So, go and open Notepad. Type the following:


 * Code Example 1 – mary.py

Keep this exactly the same, down to where the commas are placed. Save the file as mary.py—and make sure Notepad doesn't add .txt to the end of the filename (you will have to tell it to save as any file to avoid this). Turn off 'Hide known file extensions' in Windows Explorer, if it makes it easier.

Using the IDLE environment
Now, open up the Python IDLE program (should be in your start menu). Click 'File > Open' and find mary.py and open it. If you can't find mary.py, set the open dialogue to 'Files of type: All Files (*)'. A new window will open, showing the program you just wrote. To run your program, click 'Run > Run Module' (or just press F5). Your program will now run in the main Python screen (titled 'Python Shell') and will look like this:

Mary had a little lamb, its fleece was white as snow; and everywhere that Mary went her lamb was sure to go.
 * Code Example 2 – mary.py output

You can also use IDLE to create Python programs, like what you did in Notepad. Simply click 'File > New'. We will be writing all of our programs now in the Python IDLE program—the Notepad thing is just a demonstration to tell you that a .py file is just a simple text file, which anyone can see.

Something to note is that the comment wasn't shown. That is good, because remember—comments aren't compiled. (Try compiling it after removing the #—it comes out messy.)

You can also run the program from your command line program (e.g., cmd or Terminal). Open the prompt up, type  (  for macOS/*nix) then type. Your program will now execute in the command line.

Variables
Now let's start introducing variables. Variables store a value, that can be looked at or changed at a later time. Let's make a program that uses variables. Open up IDLE and click 'File > New Window'. A new window now appears, and it is easy to type in programs. Type the following (or just copy and paste—just read very carefully, and compare the code to the output that the program will make):


 * Code Example 3 – Variables

Note that if you just want to modify a variable's value with respect to itself, there are shortcuts. These are called augmented assignment operators:

Strings
As you can see, variables store values, for use at a later time. You can change them at any time. You can put in more than numbers, though. Variables can hold things like text. A variable that holds text is called a string. Try this program:


 * Code Example 4 – Strings

The output will be:

Good morning Good morning to you too!
 * Code Example 5 – String output

As you see, the variables above were holding text. Variable names can also be longer than one letter—here, we had word1, word2, and word3. As you can also see, strings can be added together to make longer words or sentences. The comma adds a space and then the variable to the string, so  outputs , but with the + it would print.