Here you can find some additional learning material related to our exercises. Go a head and have a look!
Welcome to learn basics of programming with Chilicorn Code Club! Programming is giving orders to computer in a language the computer can understand. So learning programming is learning a new language. Doing our exercises you learn basic concepts of programming in Python language. Like every language also programming languages have their very own grammar. And with this we start our journey. This is not trying to be complete guide to Python but it teaches you the very basics of it. Before we start few words about our environment.
By clicking the exercises button you can choose what exercise to do. All exercises are categorized under sessions. It's good idea to do these exercises in the order they appear in the exercises menu. So when you are ready just save your work and choose next exercise from the menu.
The black section on the left of your screen in the main view is our editor. There we write our code. To actually run the code please press 'Run'-button in the right of the editor. Things written after # are comments. Comments are just plain text and for your information but the computer doesn't run code that is marked with #. You can also distinguish comments from the grey color. Our code is colored bit differently.
The white section on the right of the main view is called canvas. That's were we see our drawings. The only way to draw there is with code. You'll get the hang of it when we introduce the Turtle to you.
The smaller grey box on the bottom of the right side of main view is the console. That's were we see text output. In the very first exercises we learn to use this.
In this session you learn what these concepts mean: print, string, variable, turtle
Printing is a nice and easy way to start your journey as a programmer. Printing is basically very easy. How ever there are few things to consider. These work nice and easy:
print "Hello world!" print 1+1
With unicode characters and combining numbers and strings you have to be careful. These also work nicely:
print u"Hyvää Päivää" print "You are number " + str(1)
Quite often we want to for example print some text or otherwise use actual words somewhere in our output. How ever we have to make difference with the code and the text we want to use. This is why we have Strings. You can form a string with quotation marks. For example when you want to print word print you have to make so distinction with those two. So the first print is command for the computer and the second print is the actual content we want to print. They just happen to be the same.
Also see two examples below. What is the output in these? What is the difference?
print 1+1 print "1+1"
Variable is way to give name to some content. It helps us so that we don't always have to write everything again and again. That's not the only reason. Sometimes it's also useful to give things a name so you can use them. But let's see an example. There I have a really long sentence and I want to use it in my code.
mySentence = "This is my sentence and it's really long and long and long and I don't want to write it over and over again when I need it" print mySentence print "Example: " + mySentence "
We use Turtle Graphics-library in our exercises. It means that we draw things with our code to make it easier to see what our code actually does. You can give commands like 'forward', 'right', 'left' to your turtle and see it move on the screen.
In this session you learn these things: python syntax and about indentations, equation of line, iteration, sequence/collection.
Syntax is kind of the same thing as grammar. Languages have rules related to the arrangement of words and such. It's the same with programming languages. You need to know few basics to write the language. Natural language that isn't grammatically right can be incomprehensible. The same goes with programming languages. The Computer trying to execute the code, doesn't understand it if it's not obeying the syntax.
In natural language you separate sentences with dot and paragraphs with line break. Also in programming languages we need to somehow tell the computer how to separate code blocks that belong together. In Python we do this with indenting lines. In some other languages this is done with brackets. The absence of brackets makes python easily readable.
These print commands are equal so they can stay on the same indentation level.
print "First print" print "Second print"
Here we have a block of code that draws a square with turtle graphics. Note the indentation.
for i in range(0, 4): t.forward(100) t.right(90)
What do you think will happen in this one? Which lines are part of the loop?
for i in range(0, 4): t.forward(100) t.right(90) t.left(45) t.forward(71) t.right(90) t.forward(71)
Sometimes you might have a group of values that you want to keep together - like in sequence or collection. We call them lists. Creating a list is quite easy. You can do it much in the same way as with any variable. You only need to put all your values inside square brackets and separate them with comma. Let's say you have list of your friends names. We can write it like this:
friends = ["Anna", "Robert", "Guido", "Martha"]
If you want to get the length of a list, you can do it like this:
friends = ["Anna", "Robert", "Guido", "Martha"] len(friends)
If you want to use items of a list you can do it with the index of element. For example to get Robert you can do it like this. Note that indexes start from 0.
friends = ["Anna", "Robert", "Guido", "Martha"] friends
Now if you get new friends you can add them to your list like this.
friends = ["Anna", "Robert", "Guido", "Martha"] # One new friend friends.append("Evelyn") # List of new friends newFriends = ["Sarah", "James"] friends = friends + newFriends
Programming is giving commands to computer. Sometimes we want to give the same (or almost the same) command to computer. We could of course write the same command again and again, but for example if the amount we have to do it is huge we might not want to repeat it as such. So that's why we have loops. Here is an example of for loop. It prints the numbers from 0 to 5. Please note the range that is from 0 to 6.
for i in range(0, 6): print i
Ranges are not the only things we can loop through. We can also loop through collections.
colors = ["red", "blue", "green", "yellow"] for color in colors: print color
There is also another way to use loops. It's called while loop. It means that we keep doing something until our condition is not true anymore. This you don't have to understand right away. Here is one example for you. It prints Hello for 5 times.
x = 0 while x < 5: print "Hello" x = x + 1
In this session you learn these things: function, return statement, randomization.
You might remember already that by using loops we tried to avoid writing same thing over and over again. But it happens that loops are not always enough for this matter. We might want to repeat some - even quite complex - part of our code again but note directly after calling it the first time. Functions are a way to give name to some block of code and then use it whenever we want to.
Here is one example of function. This function is not a really clever one since we can print Hello quite easily anyways. But you can see how we define function in Python.
def printHello(): print "Hello"
If you put the above code to editor and press run you notice that nothing happens. It's because we haven't called it yet. What we did was just the definition of function called printHello. Below you can see code where we both define the function and call it.
def printHello(): print "Hello" printHello()
To make our function a bit more clever we could give it a parameter. Try to figure out what happens with this function.
def printHello(name): print "Hello " + name printHello("Sarah") printHello("Robert")
We can still modify our function a bit. Instead of printing Hello + some name we could return it. That means that our function would actually give something back when it's done. This is sometimes really usefull because you might want to do something else than just print the value you get.
def printHello(name): return "Hello " + name returnValue = printHello("Sarah") print returnValue # This will actually print Hello Sarah
With randomization we can have nice effects especially when drawing something. Python has this ready-made library for this that we can only use. So it's quite easy to get random numbers from some range or random elements of a set. Here are some examples. If you try it, please run it several times and see the random in action.
# Let's import the random library import random # This will print random integer between 1 and 10 print random.randint(1, 10) # This will print random element of list items items = [1, 10, 100, 1000] print random.choice(items)
In these sessions you learn more about these basic concepts and you learn to combine them and build your own functions and use them.