Datatypes
The common datatypes in python
Inside python you have different datatypes which you can use or will encounter. The basic/most often datatypes are:
- String
- Integer
- Float
- List
- Dictionary
But before we will see what different options each datatype has you first need to know some things:
- First, what are comments?. A Comment is just some kind of text which will not be run by python but will make youre life easier if you learn from the start to use it. You can use comments to explain what a line or piece of code does inside the program. Every Comment inside python starts with an # But beware, if you read further i will show you some examples where a peace of code looks like an comment but really isn’t.
- Second, what are variables?. A variable is an piece of memory which you can reserve to save data on the fly. In my examples i always start my variables with an underscore _. Its not needed but this way i always know which codes are variables. To save some kind of info to an variable you first need to give the variable a name and follow that name with an =
For example:
So the name of the variable is _hello and with the = i said that everything that comes next will be put inside _hello, be aware that at this point the “datatype” of the variable is still nothing. That will change depending on the data you will use after the =
So lets start first with the datatypes:
String
Simple said an String is not an piece of underware but a piece of text inside python. If you want to create an string you simple use quotes around some text. So lets say i want to create an string with the words Hello World inside of it i can simple say:
or
Python will see both examples as an String. So if i want to save the string “Hello World” to the variable _hello you can simply use:
In this example i saved the string “Hello World” to the variable _hello and on the second line i wrote that i want to print the variable to the console to check if the text is really saved to the variable.
Integer
An int/integer is the definition of a whole number. The number can be positive or negative but there are no decimals. The most simple way to set a integer to an variable is:
So in this example i saved the number 1 to x, 2 to y and 123 to z. After that i printed the value of x and printed on the next line the type of the variable which will say Integer
You can do all sort of things with an integer. You can count inside a loop or make an calculation with different numbers like:
So here you can see that inside x = 3 because i did 1 + 2. the same goes for y. and with z i summed up x + y which will resolve into 8: z = 3 + 5
Float
Floats looks simmilar to an integer except it will allow you to use one or more decimals.
So this will automatically result in floats because of the decimals.
List
A list is used to store multiple items inside a variable, there are different kind of lists inside python and a little bit further i will show the use of an dictionary But a normal list can be created using the brackets.
So in this example i created an list called _list and inside are 3 value’s. a Dog, Cat and a Fish. Did you see that i didn’t use the “ for fish. It maybe can bee sloppy but i will not give you an error. Like i said at the topic Strings you can define them by using “ or ‘
So we have an list of animals now, each animal has in “index” inside this list. A computer always starts counting with a 0 so in python we start with that to. for example, if you want to print just dog you can do:
Because dog is the first animal inside the list. You can print the second item “Cat” with _list[1]
I only used Strings for this list, but you can use all sort of datatypes to put inside an list like numbers or boolean types (true/false)
Here are some examples about what you can do with a list:
Dictionary
A dictionary is simmilar to an list but within an dictionary you store datavalues in key:value pairs. The dictionary object at default will be unordered, can be changed but will not allow duplicates. To create an dictionary you use the curly brackets and make use of an key and value pair:
So here i created an dictionary called _dict and added different pairs to it, device:laptop, model:macbook pro, brand:apple, year:2014 So to use these values you can just use:
So instead of using the index number 0,1,2,3 you can use the key name to get the value. So lets use the list and dictionary together:
So here i added an “list” to the key options with different strings inside of it. But you can also turn this around to save multiple dictionary’s inside an list:
So here i added 2 dictionary’s to a list.
S.Schins BASICS
Python Datatypes Basics Examples