Python Data Types

Published May 11, 2020, 9:42 a.m.

In python there are many types of variables. The primary types are: None, bool, str, int and float. One thing that sets python apart from other programming languages is that variable types do not have to be declared.  That is you do not have to have a statement to initialize a variable as a certain type.  When you want to declare a variable, you just write it!  Python figures it out for you.  You can attempt to force python to adjust a variable type as we will see later.

The first type to talk about is the None type.  If you google: what is the point of the None type variable, you find:

The None keyword is used to define a null variable or an object. In Python, None keyword is an object, and it is a data type of the class NoneType . We can assign None to any variable, but you can not create other NoneType objects. Note: All variables that are assigned None point to the same object.

And that is all I have to say about the None type variable for now.  Surely it will come up later.  I don't think it is a variable that you will heavily use on purpose.  The other variable types are pretty straight forward. 

The bool type can have two values: True or False

>>> a = True
>>> b= False

'bool' stands for Boolean after George Boole for whom this type of logic structure was named.  Bool-types will surface when constructing the logic of a program.  Python's 'if' statements will rely on bool parameters.  Note that in python a true or false statement renders a bool variable type.  For instance:

>>> 1 == 1
True

In python the double equal sign means "compare and test equality".  Notice also that python evaluates the statement and renders a result.  Since I defined a as True and b as False if I type them into the interactive session and hit enter:

>>> a
True
>>> b
False

python returns their values.  In that same way, when I typed '1==1' python return the bool value for that statement.  What happens if I type 1==2?

>>> 1==2
False

Because, 1 does not equal 2, python returns the value False.

Now, let us take a look at strings.  The str type in python stands for string and can be a string of characters.  As I mentioned earlier, variables do not need to be declared ahead of time and can even be overwritten with new types.  For instance.  If I type:

>>> a = "True"
>>> a
'True'

Now this might be confusing, but I want to illustrate a point.  In python "With great power comes great irresponsibility!".  It is nice to be able to reset variables on the fly.  However, programs only do what you tell them to do.  No more or less.  If you add those quotations to the word "True" then python interprets you to mean that you want the variable 'a' to be a string.  Notice again that hitting enter after typing 'a' returned "'True'" in quotes.

Now if I check some logic:

>>> a == True
False

Does a == True?  No!  Because True is a bool type and 'True' is a str type.  One way to check the type of the variable is to write 'type(var)' as such:

>>> type(a)
<class 'str'>
>>> type(b)
<class 'bool'>

See? the a variable is a string (as we defined it) and the b variable is still a bool type.

A str variable can be declared between either single quotes or double quotes.  This is helpful if you want to have either single or double quotes within your string!  For instance:

>>> String_1 = "'Hello' said the boy to the machine!"
>>> String_1
"'Hello' said the boy to the machine!"

or:

>>> String_2 = '"HELLO." said the machine to the boy'
>>> String_2
'"HELLO." said the machine to the boy'

Typing the variable name in interactive mode returns the variable to the screen;  However, to return the variable to the screen in an actual program, you must use the 'print' function.

>>> print(String_1)
'Hello' said the boy to the machine!
>>> print(String_2)
"HELLO." said the machine to the boy
>>>

The final two types to talk about are int and float. Both are number types;  Ints are positive, negative and zero counting numbers.  Floats are decimal type numbers.  Either type can convert to the other.  However, when converting from float to int, there is generally a loss of precision.

>>> a = 101
>>> b = 100.0
>>> type(a)
<class 'int'>
>>> type(b)
<class 'float'>

Python 3 handles conversion if you try to operate (do math) on your integers.

>>> a/b
1.01
>>> type(a/b)
<class 'float'>

And by default, even if you operate on two integers, python reasonable thinks that you want a float for the resulting number.  However, if you tell python to give you an int, python rounds down by default.

>>> c= 4
>>> a/c
25.25
>>> int(a/c)
25
>>> int(1.999)
1

Well I hope that you feel pretty well introduced to python data types.  In our next tutorial we will delve into python containers.  See you next time!

skip_nextPython Containers
  • Download Python!

  • Python Interactive Session

  • Python Data Types
    (currently viewing)
  • Python Containers

  • Loops and Logic