Computers store all of the data in them as 1’s and 0’s. So how can we differentiate between different types of data we may want to manipulate in a python program? And how can we store them temporarily so that we have easy access to that data, but don’t necessarily want just anyone to see it?
The forms of data that python can store are ints and floats, strings and bools. It stores these pieces of data in variables. Lets go into a little more detail about what this data is and how its stored.
Ints vs floats
Python stores numbers in one of two ways, can you guess what those two ways are? An int is simply an integer or whole number. A float, or floating point number, is a number with a decimal point. Simple right? Here are some examples:
3 is an int
3.0 is a float
Sometimes strings can be a bit hard to explain. A string is a sequence of characters and any characters on a keyboard can be stored in strings. This includes the spacebar. So I could effectively store this entire post in a string. To create a string, just type anything between a pair of quotes. Python can actually tell the difference between “” and ‘’, so either will work when creating strings. That being said I would recommend you stick to “”. Here’s an example to explain why.
Imagine I wanted to put that last sentence into a string. By doing this:
‘Here’s an example to explain why.’
Python would only recognize ‘Here’ as a string, and then crash when it tries to read the rest of the sentence. But if I do this:
“Here’s an example to explain why.”
I wont have any problems.
Strings can also contain numbers, for example “3” and “3.03” are both totally valid strings.
A bool, or boolean, can be either True or False. Notice how I capitalize True and False, this will be important later. Booleans help to write functions that need some kind of logic. We will go over bools in more detail in a post that is completely about bools. For now, just know that a bool can have the value True or the value False.
Ok, so we know how python differentiates between these different forms of data, lets talk about how we store them.
All of these forms of data can be stored in variables. Imagine a variable as a box with a label on it. In this box you can put any data you want.
Lets say we have two variables, x and y, and we want to put an int, 3, in x and a float, 3.3, in y. How would we go about doing this. I think its about time to get out idle.
To create a variable, all you have to do is state its name, then type an =, then put in what ever you want to store. For example I have gone ahead and stored the int and float values into x and y like this:
x = 3
y = 3.3
We can also store strings in variables. For example my name is Trey, so I can create a variable, name, and store the string, “Trey”, by doing this:
name = “Trey”
As for boolean values, there are several ways to go about this. We can just create a variable with either True or False stored inside of it. For example:
t = True
f = False
But we can also pass a mathematical expression into the variable, like this:
t = 3 > 2
f = 4 > 5
Like I said before we are going to go deeper into booleans another time, but just for now remember that something like this won’t work:
t = 3 = 3
Now that we know how to store data into variables, lets touch briefly on how to do things with that data. As far as numbers are concerned, all of the normal mathematical expressions will work in python. For example if x = 2 + 2, then python will know that x = 4. There are some interesting things you can do with numbers however. For example there is an expression known as modulo that allows you to find the reminder of x when divided by y. To do this, type in x % y. for example:
10 % 7 = 3
Then there is integer division. This is simply division with out using decimal places to show remainders. To do this use // instead of / when dividing. For example:
5 / 2 = 2.5
5 // 2 = 2
For strings we can do something called string concatenation. This is putting two or more strings together. Here’s an example of a code that takes someone’s first name and last name and outputs their full name:
full = “Kobe” + “ “ + “Bryant”
That “ “ in the middle is actually a space between the two strings, you could just put it after Kobe or before Bryant.
There are many other ways to manipulate data in python, but this pretty much covers the basics.
Thank you so much for reading, I’m extremely new to this so please, please, please leave comments to let me know what you think. I do read all of them.
Also, I’ve decided that if I will regularly update you guys on anything having to do with blog by posting a weekly update post. These are just to let you guys know what’s going on behind the scenes and also lets you guys give me easy, weekly feed back on the lessons or the site or anything else.
These will also be a great way to track our progress and let you guys know what’s coming up in terms of the lessons.
So once again, thank you so much for reading. I really appreciate you trusting me with some of your time. I’ll see you guys in the next lesson!