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:

var=10
print(var)
10
var='string'
print(var)
string

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')

Output:

() 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:

type(0)
<class 'int'>
type('text')
<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.

myint=100

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.

myfloat=24123.324

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

sys.float_info.max
1.7976931348623157e+308

The second line is the result on my system.

Comments in python start with a #.

Examples:

#single line comment

Find the code for todays plog here.

The upcoming blogs will explore more built in data types.