Hello and welcome to another post in this python programming for beginners series. In this post, we will be going over variables, data types and the type() function. Now let’s get into things by opening the Thonny IDE and let’s start.
Variables are containers for storing data values – think of them as a bucket or a drawer. So if we have a variable named x and we store the value 3, think of it as a bucket holding the number three. We can have other variables such as y holding the value 2.2 or z with the value “Cuba”. Variables don’t have to just have short one character names they can be almost anything as long as they don’t start with a number, don’t have any spaces or have any special characters except underscore. Variable names are also case sensitive so “fancyname” is not the same as “Fancyname”. Personally when naming variables I avoid underscores, try to be simple and descriptive and use mixedCase where the first word is common but subsequent words are capitalized or CamelCase where every word is capitalized. Let’s look at some good and bad examples of variable names.
Data types are the form that the data takes in its variable bucket. For this beginners series, we will be focusing on 4 main datatypes with a 5th type which will be further broken down later on. The 1st four are:
The 5th type we’ll talk about later are called collections or arrays and just remember we’ll explain those later.
strings or str
Strings are text in double or single quotes or triple quotes for multi-line strings. So from our examples above the variable z would be a string, so would tRiNiDaD.
z = “Cuba”
tRiNiDaD = ‘soca’
a multiline string would be:
socaSong = ”’cent…
Notice the triple quotes which can be single quotes or doubles quotes as long as you remain consistent.
integers or int
Integers are positive or negative numbers without decimals. So from our examples above the variables, x and man would be integers.
Floats are positive or negative numbers WITH decimals. So from our examples above the variables, y and Usain1_Bolt would be floats
Booleans are a simple but powerful data type that can only be one of two things True or False. Make sure the T and the F are capitalized. So from our examples above the variables, i and dirT would be booleans
I won’t give any examples yet but they can be a List, Tuple, Set or Dictionary and sometimes are a combination of one or more data types. More on them later
Finding out variable type()
To find out what data type a variable is we can use the type() function which is built into python. Let’s use the type function, print stuff to the screen and comment within our code in the next post in the series.
Variables are containers for storing data. We have been calling them buckets. Data types are the form this data takes in our buckets and can be text, different kinds of numbers, true or false or a collection of multiple data types. Also to find out what data type a variable is we can use the type() function.
Feel free to experiment some more and when you are ready let’s jump into the next video in the series.
More on Geezam.com:
Latest posts by Kelroy (see all)
- Python Programming Tutorial for Beginners – Part 5 – String concatenation - March 4, 2021
- Python Programming Tutorial for Beginners – Part 4 – Basic Operators - March 2, 2021
- Python Programming Tutorial for Beginners – Part 3 – Printing and commenting - February 27, 2021
- Python Programming Tutorial for Beginners – Part 2 – Variables and Data Types - February 23, 2021
- Python Programming Tutorial for Beginners – Part 1 – Hello and Introduction - February 19, 2021