Lesson 01 - Print, Output & Variables

For this tutorial you will need the interactive Python shell IDLE which can be downloaded freely from here.

The 'print' statement in Python is the easiest to get your head around. Print is used to output to the screen. The print syntax, like most Python code, is extremely simple:

print "This is foo."

Call the print statement, and enclose the desired output in inverted commas.(Don't forget the white-space between 'print' and output!) If you weren't aware, we chose to output a string[1] in the above example. But you can essentially display any data type through the same method:

print 10

Okay, this is great but what if I want to print out two different things at the same time? Python has a very simple way of handling concatenation (sticking output together). The + sign is used to attach once piece of output to another:

print "Hello my name is " + "Oliver."

This would have the same affect as:

print "Hello my name is Oliver."

Rather simple ay? So now you must be asking yourself; 'Strings are all well and good but what would happen if I used integers[2]?'. Now here comes the smart bit. Python knows what data type you are using, it detects it internally, so it knows when your using strings, and it knows when your using numbers:

print 5 + 5

This will return the number 10 because Python detected you are using numbers, thus the + is used mathematically.

print "5" + "5"

5 points if you can predict what the output would be... "55"!!! This is because Python detected you are using strings(see footnotes for detection of strings) and used the + as a concatenation operator instead of a mathematical operator.
All of this may rather simple but it gets a little bit harder. What if you want to mix data types?:

print "My mental age is " + 2

This will return a type error; "Cannot concatenate 'str' and 'int' objects". This is because you have confused Python; You tell Python your working with strings but you're also using integers, so how is Python meant to know what to do. Are you trying to do math or concatenation? Because we all know you can't add numbers to letters can you? :) However this error is easily overcome by enclosing your integer in a str() function:

print "My mental age is " + str(2)

This converts the integer to a string so it can be attached to the other one. Also note that this works on other data types, not just integers.

Now we've done the print statement and outputting to the screen we are going to move onto arguably the most important thing about any programming language, variables. If you've ever done algebra you'll understand variables a lot quicker. Variables are place holders basically, it's a way of storing and labeling data so it can be used later on. Python allows you to name anything, anything[3]; strings, integers, lists, functions, you name, you can assign it to a variable. I'll start with a basic example:

x = 50
print x
 

In this example we assign 50 to a variable called 'x'. Note in Python variables don't have to be predefined[4]. Then we displayed the content of 'x' by printing it to the screen. If you don't see the point of this tell me which would be easier to write:

print 19865
print 19865
print 19865
print 19865
print 19865
or

a = 19865
print a
print a
print a
print a
print a 

This means we can perform basic algebra using variables to hold the numbers:

x = 10
y = 20
a = 2
print (y - a) * x
 

This is the equivalent of:

print (20 - 2) * 10

'Hey wait a moment! You said variables makes things easier, but there's less code in the second example!' True but what if we wanted to access that same data later on, we'd have to write it all out again, and you wouldn't be able to do what I'm about to show you! You can change the contents of a variable at any time and it's so simple to do:

s = "Hello world!"
print s
s = "This is foo."
print s

That would output:
Hello world!
This is foo.

Yet we only used the one variable, and that's because we rewrote it. Because you can't assign more than one thing to a variable at one given time it is over-written with the new data.

And for the grand finale we will combine everything we've learnt into one!:

x = 14
y = 7
print "The sum of " + str(x) + " and " + str(y) + " is: " + str(x+y)
x = 2

y = 21
print "The sum of " + str(x) + " and " + str(y) + " is: " + str(x+y)
 

Help for this tutorial is available as always on the forum.

If there is something you don't fully understand or would like clarification, feel free to post a reply in this thread, or alternately contact me through PM or even Email.

Footnotes:
[1]: A string is a data type that is identified by being enclosed in inverted commas or speech marks; ""
[2]: A integer is a data type that is a whole number.
[3]: There are rules to what a variable can be called. Some generic words are reserved like 'print' for obvious reasons. Also a variable name must start with a letter or underscore and may only contain letters, underscores and numbers. Variables are also case sensitive; foo is different to FOO.
[4]: In other, normally compiled languages all variables must be declared so the compiler knows what data it will be dealing with.

Image

This Python tutorial by Oliver Haddock for Dink Software and is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.0 UK: England & Wales License. Permissions beyond the scope of this license may be available at ottajay@googlemail.com