Python 3x Pandas Django

Python Datatypes & Variable

In programming, the data type is an important concept, and variables are used to store different types of data.

A variable is a symbolic name that is a reference or pointer to an object. Once an object is assigned to a variable, you can refer to the object by that name.

Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

Rules for declaring a variables:

  • Snake case (stylized as snake_case) refers to the style of writing in which each space is replaced by an underscore (_) character, and the first letter of each word written in lowercase. i.e snake_case
  • Start with lowercase or underscore
  • A variable name can only contain alpha-numeric, characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • Don't overwrite python keywords


Numericint, float, complex
Squencelist, tuple, range, daterange
Setset, frozenset
Binarybytes, bytearray, memoryview
Objectdate, time, datetime...
ClassesCustom Types
ModulesSpecialized Datatypes
Noneclass NoneType object


  strDateType        = "Hello World"	                          #str
  intDataType        = 30	                                  #int
  floatDataType      = 20.6	                                  #float
  complexDataType    = 1j	                                  #complex
  listDataType       = ["apple", "banana", "cherry"]	          #list
  tupleDataType      = ("apple", "banana", "cherry")	          #tuple
  rangeDataType      = range(6)	                                  #range
  dictDataType       = {"name" : "John", "age" : 40}	          #dict
  setDataType        = {"apple", "banana", "cherry"}	          #set
  frozesetDataType   = frozenset({"apple", "banana", "cherry"})	  #frozenset
  boolDataType       = True	                                  #bool
  bytesDataType      = b"Hello"	                                  #bytes
  bytearrayDataType  = bytearray(5)	                          #bytearray
  memoryviewDataType = memoryview(bytes(5))	                  #memoryview

Run code snippet

To verify the type of any object in Python, use the type() function:

int - Integer

Integer is a number

print(1 + 2)            #Output: 3
print(100 - 200)        #Output: -100
print(10 * 10000)       #Output: 100000
print(0)                #Output: 0

print(type(1 + 2))      #Output: < class 'int'>
print(type(100 - 200))  #Output: < class 'int'>
print(type(10 * 10000)) #Output: < class 'int'>
print(0)                #Output: < class 'int'>

float - Floating Point Interger

float is a decimal numbers

float requires more space than int for storing a values

print(1.0 + 2)          #Output: 2.0 #Python print larger data types to avoid the loss of data
print(100 / 200)        #Output: 0.5
print(10.43)            #Output: 10.43

print(type(1 / 2))      #Output: < class 'float'>
print(type(100 / 200))  #Output: < class 'float'>
print(type(10.43))      #Output: < class 'float'>

str - Strings

Strings in python are arrays of bytes representing Unicode characters

A single character is simply a string with a length of 1

x = "A"

Output: A


Output: A

Either you can create a string using single quotes or double quotes

user_name = "Michael"     #Output: Michael
print(type(user_name))    #Output: < class 'str'>

user_name = 'Michael'     #Output:Michael
print(type(user_name))    #Output: < class 'str'>

For Multiple line String use three single quotes '''

user_name = '''

String Concatenation

first_name = "Sachin"
last_name  = "Tendulkar"
full_name  = first_name + " " + last_name    #Output: Sachin Tendulkar

For more detail, please refer Python Strings

bool - boolean values - True\False

print(True)         #Output: Trie
print(type(True))   #Output: 

#Comparing two digit's
print(1 == 2)       #Output: False
print(type(1 == 2)) #Output: 


None is used to define a null value. It is not the same as an empty string, False, or a zero. It is a data type of the class NoneType object.

Assigning a value of None to a variable is one way to reset it to its original, empty state.

Python function may returns None when it does not return anything.

print(type(None))    #Output:< Class 'NoneType' >

var_name = None
print(var_name)      #Output:None

Data Type Conversion and Data Type Casting

Converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion.

Python has two types of type conversion.

  1. Implicit Type Conversion
  2. Explicit Type Conversion

Implicit Type Conversion

  • In the below example, we can see the data type of num_integer is an integer while the data type of num_float is a float.
  • Also, we can see the num_new has a float data type because python always converts smaller data types to larger data types to avoid the loss of data.
  num_integer = 123
  num_float = 1.23

  num_new = num_integer + num_float

  print("datatype of num_integer:",type(num_integer))
  print("datatype of num_float:",type(num_float))

  print("Value of num_new:",num_new)
  print("datatype of num_new:",type(num_new))

Run code snippet

Explicit Type Conversion

  • In below example, we converted num_string from string(higher) to integer(lower) type using int() function to perform the addition.
  • After converting num_string to an integer value, Python is able to add these two variables.
  • We got the num_sum value and data type to be an integer.
  num_integer = 123
  num_string = "456"

  print("Data type of num_integer:",type(num_integer))
  print("Data type of num_string before Type Casting:",type(num_string))

  num_str = int(num_string)
  print("Data type of num_str after Type Casting:",type(num_string))

  num_sum = num_integer + num_str

  print("Sum of num_integer and num_string:",num_sum)
  print("Data type of the sum:",type(num_sum))

Run code snippet

Boolean Type Conversion

# Empty string always returns False
print(bool(""))                 #Output: False

# Non-Empty strings always returns True
print(bool("some characters"))  #Output: True

# Integer values other than 0 returns True
print(bool(123))                #Output: True

# 0 returns False
print(bool(0))                  #Output: False

#from String Type 'True' to Boolean Type 'True'
print(bool('True'))             #Output: True

Other data types will see in detail in the upcoming sections

If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!