Monday, May 2, 2016

2nd lesson in Codeskulptor

Okay so you've learned how to print out strings.
In this lesson we'll learn how to declare variables.
variables are just pieces of data in your program, you can name your own variables and declare them to start with some data then later you can change their data/content to something else if you like.
Let's start first by creating some variables to hold names like this.
firstname = "Joe"
middlename = "Stuart"
lastname = "Smith"
Now we have 3 variables named as firstname, middlename, and lastname.
Now we can print these variables to the screen by referring to their name(s) like.
print firstname, middlename, lastname
You can click play and see that the program will print out Joe Stuart Smith.
Next let's learn about string concatenation, to concatenate strings together is like to glue many strings together to form another string.  In Python/CodeSkulptor, we use the plus sign (+) to concatenate strings.
So let's try to declare another variable called fullname to hold our full name like below
fullname = firstname + middlename + lastname
Then let's print it using
print fullname
What you'll see on the output screen is JoeStuartSmith
fullname did exactly what we told it to do, concatenate names together but we would like to add a space in between the names so it looks nice. How do we do that? like this
fullname = firstname + " " + middlename + " " + lastname
Now print out fullname again with
print fullname
Now we get the beautiful format for fullname as Joe Stuart Smith

That's it for lesson 2.
Lesson reference:
Lesson Index