Python for Beginners

From EventScripts Community Encyclopedia

Contents

Part 1: Programs in a file, and variables

Introduction

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, 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' - 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 (if you don't have it, download Python 2.5 here.). Click 'File > Open' and find mary.py and open it. if you cant 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,
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 thing 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 messy)
  • 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, this stops the program from starting a new line on the screen when showing text.
  • You can also run the program from your command line program (e.g. MSDOS) - 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).

Part 1: Conclusion

Well done! We now understand longer programs, and know the use of variables. In Part 2, we look at loops and conditions, about them and how to use them.

Part 2: Loops, Loops, Loops, Loops...

Just imagine you needed a program to do something 20 times. What would you do? You could copy and paste the code 20 times, and have a virtually unreadable program, not to mention slow and pointless. Or, you could tell the computer to repeat a bit of code between point A and point B, until the time comes that you need it to stop. Such a thing is called a loop.

The 'while' loop

The following are examples of a type of loop, called the 'while' loop:

a = 0
while a < 10:
    a = a + 1
    print a

How does this program work? Lets go through it in English:

'a' now equals 0
As long as 'a' is less than 10, do the following:
    Make 'a' one larger than what it already is.
    print on-screen what 'a' is now worth.

What does this do? Lets go through what the computer would be 'thinking' when it is in the 'while' loop:

#JUST GLANCE OVER THIS QUICKLY
#(It looks fancy, but is really simple)
Is 'a' less than 10? YES (its 0)
Make 'a' one larger (now 1)
print on-screen what 'a' is (1)

Is 'a' less than 10? YES (its 1)
Make 'a' one larger (now 2)
print on-screen what 'a' is (2)

Is 'a' less than 10? YES (its 2)
Make 'a' one larger (now 3)
print on-screen what 'a' is (3)

Is 'a' less than 10? YES (its 3)
Make 'a' one larger (now 4)
print on-screen what 'a' is (4)

Is 'a' less than 10? YES (its 4)
Make 'a' one larger (now 5)
print on-screen what 'a' is (5)

Is 'a' less than 10? YES (its 5)
Make 'a' one larger (now 6)
print on-screen what 'a' is (6)

Is 'a' less than 10? YES (its 6)
Make 'a' one larger (now 7)
print on-screen what 'a' is (7)

Is 'a' less than 10? YES (are you still here?)
Make 'a' one larger (now 8)
print on-screen what 'a' is (8)

Is 'a' less than 10? YES (its 8)
Make 'a' one larger (now 9)
print on-screen what 'a' is (9)

Is 'a' less than 10? YES (its 9)
Make 'a' one larger (now 10)
print on-screen what 'a' is (10)

Is 'a' less than 10? NO (its 10, therefore isn't less than 10)
Don't do the loop
There's no code left to do, so the program ends

So in short, try to think of it that way when you write 'while' loops. This is how you write them, by the way (and a couple of examples):

while {condition that the loop continues}:
    {what to do in the loop}
    {have it indented, usually four spaces}
{the code here is not looped}
{because it isn't indented}
 
# EXAMPLE
# Type this in, see what it does
x = 10
while x != 0:
    print x
    x = x - 1
    print "wow, we've counted x down, and now it equals", x
print "And now the loop has ended." 

Remember, to make a program, you open IDLE, click File > New Window, type your program in the new window, then press F5 to run.

Boolean Expressions

What do you type in the area marked {conditions that the loop continues}? The answer is a boolean expression.

What? A forgotten concept for the non-math people here. Never mind, boolean expression just means a question that can be answered with a TRUE or FALSE response. For example, if you wanted to say your age is the same as the person next to you, you would type:

My age == the age of the person next to me

And the statement would be TRUE. If you were younger than the person opposite, you'd say:

My age < the age of the person opposite me

And the statement would be TRUE. If, however, you were to say the following, and the person opposite of you was younger than you:

My age < the age of the person opposite me

The statement would be FALSE - the truth is that it is the other way around. This is how a loop thinks - if the expression is true, keep looping. If it is false, don't loop. With this in mind, lets have a look at the operators (symbols that represent an action) that are involved in boolean expressions:

Exp. 	Function
< 	less than
<= 	less that or equal to
> 	greater than
>= 	greater than or equal to
!= 	not equal to
<> 	not equal to (alternate)
== 	equal to

Conclusion

Hopefully you will now understand the basics of Python, how to make a simple program, knowledge of classes, all the different types of variables and why and how they are used. Also, please take a look at the next tutorial: ES_Python_for_Beginners.

blog comments powered by Disqus