# Learn Practical Programming: Step 1

Variables, operations, input, and output. Photo by Clément H on Unsplash

Now that everything works properly it’s time to start coding a little. However, as I mentioned in previous posts, we’ll start coding on paper. This means that before writing in python, we’ll write our algorithm, then test it, and if it works we’ll go and code it in python. This is because it is always easier to find algorithmic errors (the ones which correspond to our logic, not to the programming language) in the paper. So, without further delay, let’s start with some concepts.

When we write code, there will be times when we’ll want to leave notes so we can remember what we were doing or for some other purpose. There are many ways to leave comments in our code depending on the language, yet for pseudocode, we’ll use // before what we want to comment (for each line). Like this

`// this is a commentthis isn't a commentThis isn't a comment // this is a comment`

Comments will be quite useful for us, as it is how I’ll leave notes to help you understand what I’m doing in my examples. You should use them too to explain what is happening in some lines when those lines aren’t easy to understand by reading the code itself (trust me, it happens).

# VARIABLES

Think of variables as if they were little boxes in which you could store information. What kind of information? You can choose from numbers to letters, words, images, and even complex objects. Here are some of the most common variable types:

• Whole numbers (known as integers)
• Decimal numbers (known as floats or doubles)
• Letters
• Texts (known as strings)
• True or false values (known as booleans)

Now let’s see how you could use them in pseudocode (this is when we “code” in the paper).

`myFirstVariable = 6    // this is a number variableanotherVariable = False    // this is a boolean variablevar1 = True    // this is also a boolean variablevar2 = 2.3    // this is a float or double variablevar3 = "Hello world!"    // this is a string (text) variable`

Notice how the variable name “myFirstVariable” has no spaces between words, this is because computers can’t know that two (or more) separate words can be the variable name, it can only identify one “word” as a name. So as a custom, when programming, if you want to give your variable a 2+ word name it is recommended to start each word (except the first one) with an upper case. This is known as CamelCase.

# OPERATIONS

Now that we have some ways to store information we’ll want to do something with it. Some common operations we can use with variables are the following:

`* SUM: adds 2 (or more) variables' values    Example:       a = 5       b = 2       c = a + b // c = 7* SUBTRACTION: subtracts 2 (or more) variable's values    Example:       a = 5       b = 2       c = a - b // c = 3* MULTIPLICATION: multiplies 2 (or more) variable's values    Example:       a = 5       b = 2       c = a * b // c = 10* DIVISION: divides 2 (or more) variable's values    Example:       a = 5       b = 2       c = a / b // c = 2.5* CONCATENATION: joins 2 (or more) letters or strings    Example:       a = "hello"       b = " world"       c = a + b // c = "hello world"`

# INPUT & OUTPUT

The last concept in this lesson is how we let the computer know that we want the variable “a” must have a value of 5. This is achieved with something called input.

`a = input()// Notice how the word input ends with parenthesis. This means that//    it is a function. We'll learn about functions later. For the//    moment just remember that it helps us let the computer know//    that we want the variable a to receive a value we specified.`

And there is also a need for the computer to let us know the result/value of some variables. We’ll need the computer to print something on the screen.

`a = 5print(a)// This prints the value of a (5) on the screen. Notice that print() is also a function and must have the variable we want to print inside its parenthesis.`

# EXAMPLES & TESTS

Do you remember what I mentioned about Test-Driven Learning? It’s time to work with that. I’ll give you some examples and then you’ll have to do some yourself. Please keep in mind that for the moment we’ll work with pseudocode (pen/pencil & paper).

## Example: a program which gives us the sum of 2 numbers

`a = input("Give me variable one")b = input("Give me variable two")// Note that I added a message for the input. This will result in//   the program first printing my message and then waiting for the//   user to enter a number.c = a + bresult = "The sum is: " + cprint(result)`

## Example: a program which multiples a number by a second number, and then subtract it the third number

`a = input("Give me variable one")b = input("Give me variable two")c = input("Give me variable three")d = a * bd = d + c// Notice how I use the value variable d has and then add it the //   value of variable c.result = "The result is: " + dprint(result)`

## Test: write a program in pseudocode to divide a number by a second number, multiply it by a third number, and then divide by the sum of all initial three numbers (the numbers given by the user).

Here are some tests your code must fulfill to consider it successful.

`TEST 1:number 1 is 5number 2 is 2number 3 is 4result must be 1.25TEST 1:number 1 is 6number 2 is 3number 3 is 11result must be 1.1`

I strongly suggest you try this yourself before checking my solution.

Link to my solution.

I hope you enjoyed this step of the course and found it useful. Until the next step!

--

-- ## SSAM

I’m a computer science student and I write about programming, roleplaying games, sci-fi, movies, books, finances, and anything that comes into my mind.