Python for Beginners (Lesson 2)

From EventScripts Community Encyclopedia


Tutorial: Python for Beginners (Lesson 2)

Requires: ES (Not needed)
Difficulty: Easy
Author: Saul Rennison
Contributors: ~
Length: 10 mins

Tutorial Overview

Table of Contents

Contents

Description

Previous Lesson: Lesson 1
Next Lesson: Lesson 3

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.

Tutorial Content

Editing in Notepad

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

# A simple program.
print "Mary had a little lamb,"
print "it's fleece was white as snow;"
print "and everywhere that Mary went",
print "her lamb was sure to go."

Keep this exactly the same, down to where the commas are placed. Save the file as mary.py.

Using the IDLE Environment

Now, open up the Python IDLE program again. Goto the Open file dialog (File->Open), find mary.py and open it. A new window will open, showing the program you just wrote. Run the module (same as program, just in Python terms): to do this, click Run and then 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,
it's fleece was white as snow;
and everywhere that Mary went her lamb was sure to go.

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 example is just a demonstration to tell you that a .py file is just a simple text file, which anyone can see.

There are a couple of things to notice here:

  • First of all, the comment wasn't shown. That is good, because remember - comments aren't compiled. (Try compiling it after removing the # -- it comes out errory [is that a word?])
  • Second, is that the 3rd and 4th line got joined. This is because there is a comma just outside the inverted commas that surround the text in the print command.
  • You can also run the program from your command line program (e.g. MS-DOS) - Open the prompt up, type cd path\to\your\file then type python mary.py. Your program will now execute in the command line.

Variables

Now lets 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, 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):

# Variables demonstrated
print "This program is a demo of variables"
v = 1
print "The value of v is now", v
v = v + 1
print "v now equals itself plus one, making it worth", v
v = 51
print "v can store any numerical value, to be used elsewhere."
print "for example, in a sentence. v is now worth", v
print "v times 5 equals", v*5
print "but v still only remains", v
print "to make v five times bigger, you would have to type v = v * 5"
v = v * 5
print "there you go, now v equals", v, "and not", v / 5

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:

# Giving variables text, and adding text.
word1 = "Good"
word2 = "Morning"
word3 = "to you too!"
print word1, word2
sentence = word1 + " " + word2 + " " +word3
print sentence

The output will be:

Good Morning
Good Morning to you too!

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. However, it doesn't add spaces in between the words - hence me putting in the " " things (there is one space between those).

There are three ways to hold strings.

'This is a string with single quotes'. 
"This is a string with Double Quotes".
"""This is an example of a really really long string
that can sometimes span over
many
lines but still work in the same way"""

Lists

Lists are important aspects to Python coding. Python lists are built into the language. They can do all the basic list operations (searching for something, adding, subtracting, etc.) and in addition have the ability to index the elements inside the list. By indexing I mean that we can refer to a list element by its sequence number (assuming the first element starts at zero!). The Syntax for a List is:

>>> aList = [Thing1,Thing2,Thing3...]

An Important thing to remember about indexing is that the first item in the list is 0.

>>> aList = ['one','two','three','four','five']
>>> print aList[2]
three

It Printed 'three' because [2] refered to the 3rd piece of information Now for some other List commands

>>> aList = [1,2,3,4,5]
>>> aList[2] = 7     #Changes the 3rd piece of info to "7"
>>> print aList
[1,2,7,4,5]
 
>>> aList.append(6) # Adds the value in parenthesis to the END of the list
>>> print aList
[1,2,7,4,5,6]
 
>>> another = [7,8,9,10]
>>> aList.append(another)  # adds the list 'another' to 'aList'
>>> print aList
[1,2,7,4,5,6,[7,8,9,10]]   # notice how python still seperates the List
 
>>> print aList[6][2]      # This cmd finds the value in the sublist in position 6 and then the info in position 2 of that list
9 
 
>>> print [1,3,5,7].index(5)
2                              #This returns '2' because '5' is in position 2
 
>>> print aList.index(6)
5
>>> print aList.index(7)       # Note how this only finds the 1st instance of a 7
2
 
>>> print len(aList)     # The List is 7 Positions Long (Counts Sublists as 1 position)
7

Conclusion

Well done! We now understand longer programs, and know the use of variables. In Lesson 3, we investiage at Loops and Conditionals. Take a look at the tutorial description to go onto the next lesson.

Thanks for reading

blog comments powered by Disqus