Python Data Types

Updated: Aug 29, 2020




Python Data Types


Hello, readers, first of all, congratulations on being able to install python on your systems and now you must be all set to get tour hands dirty with Python.


Before discussing data types, I want to spend a couple of minutes discussing keywords in Python.


Keywords


Keywords are the words reserved by Python for its own use, they cannot be used as variable names. Below is the table of Python Keywords. As compared to other programming languages like C, Java, Python have very less number of keywords.




REPL: Read Evaluate Print Loop


Just for the testing purpose to make sure if your code is working fine, you really don't need to have a very big arrangement. Go to the Unix shell and type python, it will give you a python command line to work on. Note that it only executes one line at a time and is there just for handy python programming.


To exit the REPL, press Ctrl + D or type exit()



Comments In Python


Comments in python starts with #

There is a misconception that python supports multi-line comments with ''' or """



''' 
This is a multi-line comment
Welcome to LinuxAdvise
'''


In reality, it's a multi-line string that is going to take space in memory.



Data types In Python




Data types define the type of the variable, whether it is an integer variable, string variable, tuple, dictionary, list, etc. In this article, we will learn about the data types and their usage in Python.



1. Numeric Data Type in Python


IntegerIn Python 3, there is no upper bound on the integer number which means we can have the value as large as our system memory allows.




# Integer number
a=100
print(a)
print("Data Type of a variable is", type(a))


Output:


100 
Data Type of a variable is <class 'int'>



FloatValues with decimal points are the float values, there is no need to specify the data type in Python. It is automatically inferred based on the value we are assigning to a variable. For example, here fval is a float data type.



# Float number
fval=87.97
print(fval)
print("Data Type of a variable is", type(fval))

Output:


87.97 
Data Type of a variable is <class 'float'>



Complex Number Numbers with real and imaginary parts are known as complex numbers. Unlike other programming languages such as Java, Python is able to identify these complex numbers with the values. In the following example when we print the type of the variable cval, it prints as a complex number.


# Complex number
cval=5+4j
print(cval)
print("Data Type of a variable is", type(cval))

Output:


(5+4j) 
Data Type of a variable is <class 'complex'>



Binary, Octal, and Hexadecimal numbers


In Python, we can print decimal equivalent of binary, octal, and hexadecimal numbers using the prefixes.

0b(zero + 'b') and 0B(zero + 'B') – Binary Number 0o(zero + 'o') and 0O(zero + 'O') – Octal Number 0x(zero + 'x') and 0X(zero + 'X') – Hexadecimal Number



# Integer equivalent of binary number 1010
bval=0b1010
print(bval)

# Integer equivalent of octal number 323
cval=0o323
print(cval)

# Integer equivalent of hexadecimal number FFA
hval=0xFFA
print(hval)

Output:


10
211
4090



2. Python Data Type – String


String is a sequence of characters in Python. The data type of String in Python is called "str".

Strings in Python are either enclosed with single quotes or double-quotes. In the following example, we have demonstrated two strings one with the double quotes and other string str2 with the single quotes.


# Python program to print strings and type
str1="This is a string with double quotes"
str2='this is a string with single quotes'

# Displaying string str1 and its type
print(str1)
print(type(str1))

# Displaying string str2 and its type
print(str2)
print(type(str2))

Output:


This is a string with double quotes
<class 'str'>
this is a string with single quotes
<class 'str'>