The 'print' statement in Python is the easiest to get your head around. Print is used to output to the screen. The print sytax, like most Python code, is extremly simple:
- Code: Select all
print "This is foo."
If you wern't aware we chose to ouput a string[1] in the above example. But you can esencially display any data type through the same method:
- Code: Select all
print 10
- Code: Select all
print "Hello my name is " + "Oliver."
- Code: Select all
print "Hello my name is Oliver."
- Code: Select all
print 5 + 5
- Code: Select all
print "5" + "5"
All of this may rather simple but it gets a little bit harder. What if you want to mix data types?:
- Code: Select all
print "My mental age is " + 2
- Code: Select all
print "My mental age is " + str(2)
Now we've done the print satement and outptting to the screen we are going to move onto argubly the most important thing about any programming language, variables. If you've ever done algebra you'll understand variables alot 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:
- Code: Select all
x = 50
print x
- Code: Select all
print 19865
print 19865
print 19865
print 19865
print 19865
- Code: Select all
a = 19865
print a
print a
print a
print a
print a
- Code: Select all
x = 10
y = 20
a = 2
print (y - a) * x
- Code: Select all
print (20 - 2) * 10
You can change the contents of a variable at any time. And it's so simple to do:
- Code: Select all
s = "Hello world!"
print s
s = "This is foo."
print s
Hello world!
This is foo.
Yet we only used the one varible, and that's because we rewrote it. Because you can't assign more than one thing to a varible 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!:
- Code: Select all
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)
Footnotes:
[1]: A string is a data type that is identifyed by being enclosed in inverted commas or speach 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 cases sensitive; foo is different to FOO.
[4]: In other, normally compiled languages all varibles must be decleared so the compiler knows what data it will be dealing with.
-------------------------------------------------------------------------------------------------------------

Python tutorial: 'print' and variables by Oliver Haddock 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.
