Welcome back to another guide in learning to code! Or, Nice to meet you, if you’re starting here!
In this post, we’ll be learning variables and the print()
statement from the previous guide.
print()
In the two lines of code we wrote, the first line was print()
.
What this does is that it simply prints stuff to the console.
Now, I say stuff because you can use letters and words and you can use numbers.
Like so:
print(1)
That makes the console “say”,
1
You can test it yourself! Try replacing print('Hello World!')
with print(1)
and run the program by opening the file with Python. If you’re using VSC, you can just press Ctrl
and F5
Now, you can also use print('1')
to make the console output
1
But, what’s the catch? What makes those so different? One print uses '1'
while the other just has 1
. Why is that?
Well, that, my friend, brings us to the topic of…
Variables
Lovely variables, the backbone of Artificial Intelligence.
Just like in math, variables can represent anything. To “make” a variable, you have to declare it first.
There’s a few ways to declare them, but since we’re using python, we’ll need to learn the different variable types.
And, for reference, here’s some examples of variables,
howmanytimeseverettehasbeenbanned = 9999
CarbonAtomicMass = 12.0107
my_password = 'plz no hack me!11!!!!11!1'
Player1 = my_password
Yes, you can set a variable to a variable.
So, let’s see, the difference between '1'
and 1
are the variable types.
Just like there are different types of chairs, there are different types of variables. Let’s learn two basic ones.
Strings are letters, otherwise called Characters. They’re known as characters because you can put numbers into a string.
Here’s three examples of strings:
'My computer is on fire.'
'I have written only 2 guides for coding and 1 for setting up Git'
"I can totally think of a third example."
If you notice, I’ve been putting everything inside of apostrophes ( ’ ). That just lets the code know that it’s a string. Since the 2 and 1 in the second example are within two apostrophes, they are part of a string.
If you were paying close attention, you have probably realized that the third example has it’s string inside quotation marks ( " " ). Python lets use utilize both apostrophes and quotation marks when making strings.
One thing I had to learn on my own was that if you tried to do this,
'Well, I'm not sure this will work.'
The apostrophe used for the I'm
cuts off the string early, which is why the letters after the '
in I'm
are white. That’s why you would do either of these,
'Well, I\'m not sure this will work.'
"well, I'm not sure this will work."
The backslash ( \ ) will let you use apostrophes in a string using apostrophes ''
.
Now, let’s talk about Integers.
Integers are simple. They’re just numbers.
1
2
10
1003
These are integers. Simple.
Now, let’s see what each variable type these four variable are.
howmanytimeseverettehasbeenbanned = 9999
CarbonAtomicMass = 12.0107
my_password = 'plz no hack me!11!!!!11!1'
Player1 = my_password
Can you guess?
Here’s the answer:
howmanytimeseverettehasbeenbanned = 9999 # This is a Integer
CarbonAtomicMass = 12.0107 # This is a Float
my_password = 'plz no hack me!11!!!!11!1' # This is a String
Player1 = my_password # This is a String
Oh, and for reference, the #
signifies that the rest of a line is a comment. Comments are ignored by the program, so you can write notes in your code. It’s good practice to use comments so others know what the heck is going on in the code.
But, as you can see, there’s another variable type in there that I didn’t talk about. It’s a float.
It’s like an Integer but it has numbers after a decimal. (I think that’s what it’s called.)
Floats just let you use decimals and have precise numbers.
One last basic variable type are Booleans. This is just true and false.
isPlayerDead = True
doYouUnderstand = False
You get the point. Booleans are used for comparing things using if
statements. We’ll talk more about them when we actually need to use them.
Here’s some more variables. Look at them and guess what their types are, guess them right and you get an antag token the Science Award directly from our RD, Everret Garrison.
nameOne = 'Kerbin Fiber'
name2 = "Dindu Nuffin"
justANumber = 9
AnotherNumber = '23098'
copiedVar = nameOne
lastNumber = 304
seventeen = False
unamedVariable = justANumber
why = copiedVar
iwaskidding_heresanotherone = 19.8234
And then the answers:
nameOne = 'Kerbin Fiber' #String
name2 = "Dindu Nuffin" #String
justANumber = 9 #Integer
AnotherNumber = '23098' #String
copiedVar = nameOne #String
lastNumber = 304 #Integer
seventeen = False #Boolean
unamedVariable = justANumber #Integer
why = copiedVar #String
iwaskidding_heresanotherone = 19.8234 #Float