Latest Awards
Complete 10 python programs which focus on loops.

morgan

Score over 100 points in total

vincebanter10

Complete any 5 python programs

vincebanter10

Complete 3 python list based tasks

vincebanter10

Complete 5 python list based tasks

vincebanter10

Complete 10 python list based tasks

dman12

Score over 1000 points in total

april-logan

Complete any pygame task

april-logan

Complete any 3 pygame tasks

april-logan

You have to complete 5 pygame tasks

april-logan

Score over 500 points in total

sam-edmund

Complete any pygame task

sam-edmund

Complete any 3 pygame tasks

sam-edmund

You have to complete 5 pygame tasks

sam-edmund

Complete any pygame task

12wrigleyf

Complete any 3 pygame tasks

12wrigleyf

You have to complete 5 pygame tasks

12wrigleyf

Score over 500 points in total

jhoughton12

Complete any 10 python prograns

jhoughton12

Complete 10 python programs which focus on loops.

jhoughton12

Python Score
tasks = 0
achivements = 0
freestyle = 0
Total = 0
Top 5 Scores
114gilo 3500
2joss-nolan-2 2530
3alexdawkins 2515
4byrnebrian 2080
512wattsl 1750
Click to view all scores
Who else completed this task?
No one has completed this task yet. Be the first!

Arrays and lists

Every language contains some form of data structure which allows you to store more than one value inside a variable. These tend to be called an array or a list. In python we call them lists. What is the difference between an array and a list? What is a data structure? Lost? Ok let us start from the beginning :)

a1 = "bert"
a2 = "sally"
a3 = "fred"
print a1, "is friends with", a2
print a2, "is friends with", a3

Here we have three people who we want to say are friends with one another. If we added a 4th friends we would have to add another two lines of code. If we added 10 more friends then we would need 20 more! If we had 1000… Well you get the idea! Clearly this is a dumb way to do it. The better way is to use an array. A array is a fixed length structure which allows more than one of the same type of data to be stored. In this case many strings. Let us see it in action

names = ["bert", "sally", "fred", "sue"]
print names[0], "is friends with", names[1]
print names[1], "is friends with", names[2]
print names[2], "is friends with", names[3]

Rather than needing 4 variables we only use one. Because it contains 4 names we have to use a index (bit like accessing a letter from a string!). The numbering starts from zero (like a string!) and will always use the [] brackets.

Add “barny” to the above code to move onto the next exercise.

Output: (clear)


Leave a Reply

You must be logged in to post a comment.