Python Programming Tutorial for Beginners – Part 2 – Variables and Data Types

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

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.

Python Programming Tutorial for Beginners - Part 2 - Variables and Data Types

Data types

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:

  • strings
  • integers
  • floats
  • Boolean

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…
5 cent…
10 cent…
dolla!”’

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

Floats are positive or negative numbers WITH decimals. So from our examples above the variables, y and Usain1_Bolt would be floats

booleans

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

Collections/Arrays

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.

Recap

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.

Python Programming Tutorial for Beginners

The following two tabs change content below.
Kelroy is an internet junkie, technology advocate and avid gamer who envisioned Geezam.com as a Caribbean based technology blog offering a more global view of technology topics. Reach out if you have a guest post or would like to write for us.