November 5, 2016

Data types in Python 3

Python is a dynamically typed programming language, so you don’t need to explicitly specify the data type of a variable before usage. A variable can be rebound to objects of different data types at runtime and the Python interpreter will not complain.

An example:


The following are the data types available in Python:

  • Boolean - True and False
  • int and float
  • list
  • tuple
  • range
  • string
  • set
  • dictionary

This is not an exhaustive list, just the list of types I am guessing are most relevant to me for Machine learning and building web apps. For the complete list refer this link.

The following values are considered Falsey:

  • False
  • None
  • 0
  • {},“,(),[]

All other values can be considered truthy.

What is the difference between true and false vs truthy and falsey?

True and False are boolean values. Truthy and Falsey values are terms to describe how objects will be evaluated as True and False values in the context of if conditions. A better explanation can be found on Stackoverflow.

To quickly check the truthiness or falsiness of objects, use bool(data).

if not bool(()):
	print('() is falsey')
if not bool([]):
	print('[] is falsey')
if not bool({}):
	print('{} is falsey')
if not bool(''):
	print('\'\' is falsey')
if not bool(None):
	print('None is falsey')


() is falsey
[] is falsey
{} is falsey
'' is falsey
None is falsey

All other values can be considered truthy.

To check the datatype of an object, use type(value). Example:

<class 'int'>
<class 'str'>

Moving on to integers and floats.

Integers are just whole numbers. So you can have positive and negative values but not fractions. Examples: 10,1414,524123,-1414.


The cool thing about integers in Python is that the maximum value is only limited by the available system memory on the device running the code. Coming from Java, it’s a welcome change, especially when dealing with large numbers (I encountered this problem in programming challenges online).

The standard operators can be used on integers such as *,/,+,-,**.

(I will blog about operators later).

Floating point numbers can handle precision after the decimal point. Examples 6.2, 24.3.


Float is implemented using double in C. To check the maximum float value your system supports, type in the following:


The second line is the result on my system.

Comments in python start with a #.


#single line comment

Find the code for todays plog here.

The upcoming blogs will explore more built in data types.


© Plogging Dev - Powered by Hugo Theme by Kiss