Python Data Types Part One

In this tutorial you will learn:

  1. Numbers
  2. Booleans
  3. NoneType
  4. Type conversion

Introduction:

Python data-type is a classification that specifies the type of value a variable has. Since everything in python is an object, every type in python have his own specific methods and attributes that can be used on this type. (we will discuss more about Ojbects later and see how to create our own data type).

This tutorial will be devided in two part the first part which is the current tutorial will be about the numbers data-types and the second part will be about some more complex data-types like list and tuple.

1 Numbers and type():

Python support different type of numbers like int (Integers), float (floating point) and complex (complex number).

  • Integer is a number that is not a fraction, it can be negative or positive number: (23, 65, -5)
  • Floating point is a number that contain floating decimal like: (25.6, 75.0, 55.33, -33.45)
  • Complex number is a number that can be expressed in the form a + bi, in python the i is replaced by j : (23 + 4i). You may not use this type unless you have some math problem that uses complex numbers. Actually i have never used this type in my code but it is good to know about it in case you need it. You can read more about complex number here

Now lets some examples:

0#int type
1x = 24
2
3#float type
4y = 65.25
5
6#complex type
7z = 24 + 3j

When you run this code each variable type will be defined based on its own value.
Python automaticaly knows what type the variable is based on the value assigned to it.

In python we can use a built-in function called “‘type()”’ to see the type of our variables, lets see an example of that:

 0#int type
 1x = 24
 2print(type(x))
 3
 4#float type
 5y = 65.25
 6print(type(y))
 7
 8#complex type
 9z = 24 + 3j
10print(type(z))

OUTPUT: <class ‘int’>

OUTPUT: <class ‘float’>

OUTPUT: <class ‘complex’>

As you see the “‘type”’ function tells us the type of each variable.

The numbers that we have been using and what we always use and see around us are decimal (base 10) number system, but there is some other number systems that can be used by programmers like: binary (base 2), octal (base 8) and hexadecimal (base 16).
In python we can represent these numbers by adding a special prefix.

Number System Prefix
Binary ‘0b’ Or ‘0B’
Octal ‘0o’ Or ‘0O’
Hexadecimal ‘0x’ Or ‘0X’

Lets see this in code:

 0#binary
 1x = 0b01101
 2
 3#octal
 4y = 0o15
 5
 6#hexadecimal
 7z = 0xD
 8
 9print(x, y, z, sep=', ')

OUTPUT: 13, 13, 13

As you see we have printed the number 13 in a three different number systems.
You can read more about the different type of number systems here.

2 Booleans:

Boolean type is a True or False type, this type is used in condition statement to decide if a specific part of the code should run or not.

0x = True
1
2y = False
3
4print(x, type(x))
5print(y, type(y))

OUTPUT: True <class ‘bool’>

OUTPUT: False <class ‘bool’>

3 NoneType:

In python we can create a variable that doesn’t have any value by using the None keyword, which tells the python interpreter that this variable doesn’t contain any value.

0x = None
1
2print(x)
3print(type(x))

OUTPUT:

OUTPUT: <class ‘NoneType’>

When you run this code you will see that the first print function will print nothing, this because the “‘x”’ variable doesn’t contain any value.
The second print function prints the type of the “‘x”’ variable which is NoneType.

4 Type conversion:

In python we can convert one type of number to another type. Lets see an example :

 0#int
 1x = 55
 2print(type(x))
 3
 4#float 
 5y = 33.45
 6print(type(y))
 7
 8#convert x from int to float
 9x = float(x)
10print("converted x", type(x))
11
12#convert y from float to int
13y = int(y)
14print("converted y", type(y))
15
16#convert string to complex
17z = complex('3 + 4j')
18print(type(z))

OUTPUT: <class ‘int’>

OUTPUT: <class ‘float’>

OUTPUT: converted x <class ‘float’>

OUTPUT: converted y <class ‘int’>

OUTPUT: <class ‘complex’>

As you see we have converted int type to float and float to int, using the “‘int()”’, “‘float”’ and “‘complex”’ built-in functions.

Now lets try to convert an int,float and None to bool.

 0#convert int and float to bool
 1b = bool(5)
 2b2 = bool(-3.5)
 3b3 = bool(0)
 4b4 = bool(None)
 5
 6print(b, type(b))
 7print(b2, type(b2))
 8print(b3, type(b3))
 9print(b4, type(b4))

OUTPUT: True <class ‘complex’>

OUTPUT: True

OUTPUT: False

OUTPUT: False

As you see the “‘bool()”’ built-in function convert any positive or negative number (int or float) to True, but only zero and None return False.

In part two of this tutorial you will learn more about some other important types like list, tuple and string.

Adnan

Read more posts by this author.