Perhaps you read my blog post You Already Know How to Code and you need more convincing. Well, OK then, here are more ways in which you already know how to code.
You Already Know Variables
“Yeah, in the sense that it’s ‘things that change’. Like where I last put my keys…”
And you’d be right. Variables are things that can change. They are a placeholder for values. You could declare ‘where I last put my keys’ as a variable, and every day that variable could have a different value assigned. Day one: the kitchen table. Day two: the floor. Day three: down the sofa, and so on.
What stays constant is the variable name, ‘where I last put my keys’. If you were more like a computer, and less like a forgetful human, whenever you put down your keys, you would make a mental note under the variable name ‘where I last put my keys’ of the current location of the keys. So, say for instance you come in the door and put your keys on the key hook (I know, the last place you’d actually put them). You would set a mental note of ‘where I last put my keys’ equal to the value ‘the key hook’. You would then go about your business and later in the day, when you want to go out again, you will start wondering, ‘where did I put my keys?’ But that is not a problem, because all you have to do is look up that mental note (variable) you stored earlier, under the name ‘where I last put my keys’. You find that the value of that variable is ‘the key hook’. You find your keys and are able to go out straight away.
The process of you making a mental note about something is similar to what a computer does when a variable is set. The variable name is stored, and along with it is a reference to a place in the computer’s memory where the variable value is stored. When you refer to the variable by its name, the computer looks up that place in memory and retrieves the value.
Let’s try this in code, using python as our programming language:
last_key_location = 'key hook' # Some time passes, some other code is run print(last_key_location)
Here we declared a variable called last_key_location
(variable names can’t have spaces) and set it to the value ‘key hook’. The comment below the declaration represents other code we could execute in-between. We then retrieve the value of the variable last_key_location
and print it. The output is:
key hook
Just like the non-forgetful version of you, the code makes a ‘mental note’ of the ‘last key location’, stored in a variable called last_key_location
. When it needs to ‘remember’ the value of last_key_location
, it just references that variable name, which pulls up the value.
You Already Know Data Types
“The number of times I’ve had to tell you, it’s not true, I don’t know this stuff, you’re just stringing me along now…”
A data type is a way of classifying a piece of information. There are three basic (primitive) data types that a computer uses. These are: string, number and boolean.
String
Did you ever make one of those personalised necklaces, where you took beads with the letters of your name on them and threaded them onto a cord? If you did, you were making a visual representation of the ‘string’ data type, which is simply a set of characters (such as letters, like A, B or C) strung together in a sequence. The word ‘string’ is a string. The sentence ‘This is a string’ is a string. Strings can contain any characters that you can type on a keyboard, and also those that you may not have on your keyboard, such as characters from other languages such as German or Chinese. ‘bde5’@;(*&^dvG£#29’ is a string. ‘Öäöüé’ is a string. ‘读写汉字 – 学中文’ is a string.
So a string, as far as a computer program is concerned, is basically text. It generally contains data like names, addresses, passwords, descriptions etc.
In python code, declaring a string looks like this:
name = 'Liz'
To denote that the data is a string, we put quotes around the text. These can be single or double quotes for python.
Number
This is exactly what it says on the tin. A number is any kind of numeric data. 123 is a number. -123 is a number. 5.445 is a number. Numbers can represent ages, prices, population counts… anything that can be represented numerically. If you can put it into your calculator, and perform calculations with it, it’s a number. And that’s how the number data type is used by computer programs. Numbers can be used in calculations. You can multiply them, square them, subtract them etc. In contrast, you can’t do that with strings. Strings can’t be multiplied together, or subtracted or squared.
In python code, declaring a number looks like this:
hours_in_day = 24
Note that we do not use quotes this time to declare a number. If we were to use quotes, the number 24 would become the string ’24’ and it would be interpreted by the program as a string. The string ’24’ is not a number as far as the computer program is concerned, and it can’t perform calculations with it.
Boolean
In the previous post, You Already Know How to Code, I explained how we use boolean logic in our everyday lives. Following on from that, a boolean data type is where a program would store a simple ‘True’ or ‘False’ value. It’s a data type that represents ‘Yes’ or ‘No’, ‘On’ or ‘Off’. It is the simplest data type, as it can only have one of two values.
We declare a boolean data type in python code like so:
is_it_sunny = True take_umbrella = False
To declare a boolean, we don’t use quotes, because otherwise that would denote a string. For python, we must use a capital ‘T’ or ‘F’ for ‘True’ and ‘False’, but other languages may use the lowercase forms ‘true’ and ‘false’, or the uppercase forms ‘TRUE’ and ‘FALSE’.
You Already Know Operators
“I’m pretty sure I know less than you think I do!”
If you’ve ever done any maths at all, you’ve used operators. They’re the symbols you put between the numbers, to denote what calculation is being made. For example, in the expression ‘2 + 2’, the ‘+’ (or ‘plus’) sign is the operator. It represents an addition operation. In that expression, we are adding the number 2 to another number 2 to get 4.
Arithmetic Operators
In addition to ‘+’ (excuse the pun), other operators you will be familiar with are ‘–’ (minus), ‘x’ (times), and ‘/’ (divide). These are all examples of arithmetic operators. When you use arithmetic operators on a set of numbers, you result you get is another number.
Let’s see this in python code:
years = 2 years = years + 2 days_in_year = 365 days = years * days_in_year days = days - 30 print(days) years = days / days_in_year print(years)
The output for this code is:
1430 3.9178082191780823
You’ll notice that we are not only performing arithmetic operations, but setting variables too, to store the result of one calculation so we can use it in the next calculation. So, on the first line, years
is set to ‘2’, but on the next line, it is set to the value it currently is (2) plus ‘2’. Therefore years
is set to ‘4’ on the second line.
You’ll also notice that there is a ‘*’ operator being used. This is the multiplication operator. We can’t use ‘x’, because that character represents the letter ‘x’. Therefore, in python, and many other languages, the multiplication operator is ‘*’ instead.
Comparison Operators
You’ve almost certainly come across these. These operators compare one value to another, and examples are ‘>’ (greater than), ‘<‘ (less than), ‘>=’ (greater than or equal to), ‘<=’ (less than or equal to) and ‘==’ (equal to). When using a comparison operator, the result you get is a boolean (a true or false value). Either something is equal to something else, or it is not. Either something is greater than something else, or it is not.
Here are some python code examples:
years = 5 new = years < 2 newish = years <= 2 established = years == 5 mature = years > 5 aged = years >= 8 print(f'new: {new}') print(f'newish: {newish}') print(f'established: {established}') print(f'mature: {mature}') print(f'aged: {aged}')
In this code, we compare the variable years
to various other values, with various comparison operators. The result in each case will be a boolean. If we run this code, we get:
new: False newish: False established: True mature: False aged: False
By making several comparisons, we have determined that the years
variable value belongs in the ‘established’ category.
Logical Operators
In my previous post, You Already Know How to Code, we looked at the ways in which you already understand boolean logic. And in the last section, we explored how you already know the boolean data type. Logical operators are simply those used in boolean logic, and they operate on expressions that evaluate to boolean values. The result of the operation is another boolean value.
For example, the expression ‘Is it raining?’ is a boolean expression with a boolean answer, true or false. The expression ‘Is it cold outside?’ is another boolean expression, also with a boolean answer. Logical operators are used to combine expressions like these into larger expressions with another boolean answer.
For example, we could have the compound expression: “‘Is it raining?’ OR ‘Is it cold outside?'”. The ‘OR’ is the logical operator here. It means, check the answer from both expressions, and if either one is ‘true’, the overall answer is ‘true’. So in this case, if it is raining, or it is cold outside, the OR expression would have the answer ‘true’.
In another example, we could have the compound expression: “‘Is it raining?’ AND ‘Is it cold outside?'”. The ‘AND’ is the logical operator. It means, check the answer from both expressions, and if both are ‘true’, the overall answer is ‘true’. If either of them are ‘false’, then the overall answer is ‘false’. So in this case, if it is raining, but it is not cold outside, the overall answer would be ‘false’. Only if it is both raining and cold will the overall answer be ‘true’.
Another common logical operator is ‘NOT’. If you precede an expression with NOT, it flips the answer it would have given from ‘false’ to ‘true’ and vice-versa. So, for example, in expression “NOT ‘Is it raining?'” will have the answer ‘true’ if it is not raining, and ‘false’ if it is raining.
Let’s see these operators in python code:
is_raining = True is_cold = False need_warm_waterproof_coat = is_raining and is_cold need_waterproof_coat = is_raining need_light_jacket = not is_cold need_coat = is_cold or is_raining print(f'Need warm waterproof coat: {need_warm_waterproof_coat}') print(f'Need waterproof coat: {need_waterproof_coat}') print(f'Need light jacket: {need_light_jacket}') print(f'Need coat: {need_coat}')
When this is run, it outputs:
Need warm waterproof coat: False Need waterproof coat: True Need light jacket: True Need coat: True
The code has determined that we need a light, waterproof coat after checking the boolean values is_raining
and is_cold
in combination with each other.
Conclusion
Are you convinced yet, that you already know how to code? Read on in You Already Know How to Code: Part 3, or if you want to learn more, I recommend trying the free courses at CodeCademy or SoloLearn. See you next time!
2 Responses
[…] You Already Know How to Code: Part 2 August 10, 2021 […]
[…] You Already Know How to Code: Part 2 August 10, 2021 […]