Basic Programming Tutorial

258

6

3

This tutorial will teach you how to write a simple program in Python. It’s designed for people with basic computer skills who have never programmed before.

You’ll need the following:

1. A Windows computer.

2. The ability to install software.

Image source:

https://www.synopsys.com/content/dam/synopsys/sig-assets/images/sig-custom-python-logo.jpg.imgo.jpg

Step 1: Step 0: Install Python

If you already have Python 3.4 installed in your computer, you
can skip this step.

If you don’t know, or aren’t sure, it won’t hurt to try. Start at this link.

Scroll down to “Files” near the bottom of the page.

Click on “Windows x86 MSI installer” at the bottom of the table.

Click “Save File”

Go to your downloads folder and double-click on “python-3.4.3.msi”

Set up Python using the installer. (The default settings will be fine for most people; you can just click “Next” until it begins the install.)

When you click “Finish”, you’re done installing Python.

Step 2: Step 1: Open the Editor

Python has a useful text editor that makes writing code easier than if you were coding from the command line. It’s called IDLE, and it should be located in the Start Menu for Windows users.

To find it, open the Start Menu in the bottom-left corner of the screen. Scroll down until you see a folder named “Python” (NOT Python 3.4). IDLE should be inside, and can be opened by clicking on it.

Once IDLE is open, look at the top of the window and click “File”, and then “New File”. A new window should open. You can close the window titled “Python 3.4.3 Shell”, or leave it open if you prefer.

Step 3: Step 2: Start Writing Code

Before we begin, there’s an important detail that people who are new to programming will need to remember. Python uses indentation to keep track of which lines of code belong together. This tutorial won’t be using multiple levels of indentation to keep things as simple as possible, but you must be careful not to indent anything unless directed to. Otherwise, your code will not run.

When this step is finished, the program should ask the user for their name and then print it out. It uses the “input” function, which is built in to Python, to prompt for input.

To begin, make sure you’re on the window called “Untitled”, and write the following:

name = input("Please enter your name: ")
print(name)

Believe it or not, this is all that’s needed for your first program.

Step 4: Step 3: Test Your Program

To run what we have so far, click on the window named “*Untitled*” and press the “F5” key on your keyboard. A window should pop up saying “Source must be saved”. Click “OK” and another window will prompt you to pick a file name. I suggest using the name “first” and clicking the “Desktop” button on the side, so you’ll easily be able to find your program.

Click “Save” when you’ve named your file.

Now, the Python shell will open (if you closed it previously), or will be switched to (if you didn’t close it), and you’ll see a prompt for your name in blue text.

If you see something else, you likely have an error. Go back to step 2 and make sure your code looks exactly like the picture.

Type your name and press enter. The program should print your name on a new line, in blue.

Step 5: Step 4: More Code?

Python is, of course, capable of so much more than this. Let’s add something slightly more complex: a loop. Loops repeat whatever lines of code are in them a certain number of times. Because of how they’re written, however, this is the part where indentation will really start to matter.

Add the following lines after the first two:

for letter in name:
   print(letter)

You may notice a few things. One: after you press enter at the end of the first line, the editor automatically indents for you. Make sure to keep this indentation. Two: in case you didn’t see this before, the editor is highlighting certain words in different colors. These words are known as ‘reserved words’ for various reasons, and they are highlighted to let you know they are special and can only be used in certain situations.

Now, your program should look like the first picture.

Running the program and entering your name should produce something like the second image.

If the indentation is incorrect, you'll see a popup like the third picture.

Notice that the first letter of your name is written twice; once horizontally, and once vertically. Let’s modify the loop to change that.

Step 6: Step 5: Change the Loop Body

The best way to make the first letter of your name appear only once is to stop the loop from printing it. In this way, the name still fully appears twice.

To prevent the loop from printing the first character of your name, we can make the loop start from the second character instead of the first. To do this, we will “index” the name. This is accomplished by adding brackets to the end of “name” in the loop, and including the point we want the name to start from.

To accomplish this, we want to replace the following line:

for letter in name:

With this:

for letter in name[1:]:

Here’s why: we want “name” to start at 1 because items in this programming language are numbered from zero, and not one. So if the name is 10 characters long, it has indices 0 to 9 instead of 1 to 10. The starting index defaults to 0, but with the 1 we added, it will now start at the second letter of your name. The colon after the 1 tells the language that we want to keep the rest of the name as is. Nothing else in the file needs to be changed, so you can save and run it. You should see something like the picture after typing your name and pressing enter as before.

If you’ve followed the instructions this far, then you’ve successfully made a Python program! If you’d like to know more, there are many other tutorials for Python available on the internet.

Share

    Recommendations

    • Arduino Contest 2019

      Arduino Contest 2019
    • Tape Contest

      Tape Contest
    • Trash to Treasure

      Trash to Treasure

    3 Discussions

    0
    None
    tytower

    4 weeks ago

    Before I commented I had a try doing it .
    My Python is 2.7.12 and this does not run as expected . How do I need to change it ?

    name = input("Please enter your name: ")
    print(name)

    for letter in name:
    print(letter)

    Please enter your name: bob
    Traceback (most recent call last):
    File "Test.py", line 1, in <module>
    name = input("Please enter your name: ")
    File "<string>", line 1, in <module>
    NameError: name 'bob' is not defined


    ------------------

    1 reply
    0
    None
    tytowertytower

    Reply 4 weeks ago

    I had to enter bob as "bob" ? strange

    0
    None
    Penolopy Bulnick

    4 weeks ago

    Nice job on your first Instructable! The directions look really easy to follow along with :)