Welcome to my forth Python Programming introduction post. In this post we are going to be looking at lists and random numbers.If you have used another programming language before, then you have probably heard of an array, well, a python list is just an array, and in the same way you can have multi dimensional lists just as you can have multidimensional arrays.
So, what is a list? Well, a list is a comma delimited separation of integers, strings or variables. You can store anything you like in a list and then use it just like you would a normal variable. Lets take a look at an example.
print "Hi, George is my name" sentence = "Hi, George is my name" list = sentence.split() print list print "%s %s %s %s %s" % (list, list, list, list, list) print list, list, list, "the", list
The above code should produce the following output.
Hi, George is my name ['Hi,', 'George', 'is', 'my', 'name'] Hi, my name is George Hi, George is the name
Lets take a quick look at the code that we have not seen before, firstly, the
split() function. What this does is it takes the string it is given; in this case the string contained by the variable
sentence, and it splits the string at every space and adds each section to a list object. List are extremely simple to use and extremely useful, but there is one thing about them that can confuse the new programmer. Lists start at 0. The first object in a list called
list, so if we have 5 things in a list, we have
list, list, list, list and
That is the basics of lists. I’m not going to go into multidimensional lists because its just way too confusing for an introduction programming post. The next thing we are going to look at is the random module.
Python Random Module
The random module is very useful and adds a nice spin to any program. Instead of having a program that always does exactly the same thing and always prints out the same thing every time it is running can be quite boring. So we can use the random module to make things a bit more interesting. In the following code, we decide that we don’t like the name the user enters and we then randomly give them a new name.
import random names = ["Dave", "Rob", "Superman", "Catwoman", "Julia", "Samantha"] name = raw_input("What is your name?: ") new_name = random.choice(names) print "I don't like the name %s, from now on, I'm going to call you %s." % (name, new_name)
So, in the above code, we first import the random module with the line
import random, we then create a list of names, ask the user to enter their name, choose a different name, then we tell them that we don’t like their new name, pretty cool huh?
Before I waffle on too much, lets first understand what a module is. A module is a collection of classes, functions and variable declarations, among other things, designed, usually, to make things easier for other programmers. One thing you have to be careful of is that you don’t reuse a variable, class or function already created by python or an imported module, if you do, then that variable will overwrite the ones from the module. For instance, in the above program, don’t use a variable called
random as the variable for the user entered name.
Obviously thats quite a basic implementation of the random module but it does start to make you think of some interesting ways you could use the module.
The last thing I would like to talk about in this post is another function defined by the random module. The
randrange() function is used to randomly select a number in a range of numbers. For example, the following code randomly selects a number between 1 and 100 (includes 1 and 100).
num = "" while num != 0 or num != 101: num = random.randrange(101): print num
The above code will randomly print any number between 0 and 101 until it prints 0 or 101, but it never will, so this is an infinite loop. But lets just quickly look at that while statement, because its a bit different to the ones we have seen before.
while num != 0 or num != 101: this means, while the variable
num does not equal 0 or the variable
num does not equal 101 then loop. But if it ever equals 0 or 101 then the loop would end.
That concludes this introductory post #4. I hope you enjoyed reading it, I definitely enjoyed writing it. If you have any problems please drop a comment on the end of this post and I’ll try my best to help.
Also, the next post you see from me will be the Practical Python Programming #1. In this post we will be designing a text based game.