Python Datatypes
In any programming language, a variable can hold different types of data. As Python is a dynamically typed language, we do not need to define the type of variable. The interpreter implicitly binds the value provided with its type.
Contents
What are Python standard data types?
Python has five standard data types:-
- Numbers
- String
- List
- Tuple
- Set
- Dictionary
In this tutorial, we will have a brief look at each of the data types.
Python Numbers
Number data types store numeric values. Number objects are created when you assign a value to them. Python supports four different numerical types −
- int (signed integers)
- long (long integers, they can also be represented in octal and hexadecimal)
- float (floating point real values)
- complex (complex numbers)
Type | Format | Description |
int | a = 10 | Signed Integer |
long | a = 345L | Long integers, they can also be represented in octal and hexadecimal |
float | a = 45.67 | Floating point real values |
complex | a = 3.14J | Number in the form of a+bj |
Example of Python Numbers:-
a = 10 #Signed Integer b = 345L #Long integers c = 45.67 #Floating point real values d = 3.14J #Complex number print(a) print(b) print(c) print(d)
Python Strings
A string is a character of sequence in any programming language. Python strings are declared using a single quote or double quote or triple quote.
# Single Line Strings name = "John Doe" # Double Quote friend = 'Ricky Ponting' # Single Quote # Multiline String separated by a black slash at the end of each line hello = "Hello\ world" # Using triple quotation marks welcome = """welcome to Programming World of Python"""
We can use a slice operator [] and [:] to get a substring from a string in Python. The index of string starts from 0 and -1 represents the end of the string. The concatenation operation can be done using plus (+) sign and asterisk(*) serves as the repetition operator.
Let us explore some examples:-
str = 'Hello World!' print str # Prints complete string print str[0] # Prints first character of the string print str[2:5] # Prints characters starting from 3rd to 5th print str[2:] # Prints string starting from 3rd character print str * 2 # Prints string two times print str + "TEST" # Prints concatenated string
Python Lists
Python list is ordered data type i.e. with heterogeneous data. List are enclosed in brackets []. The list is the most widely used data type in Python. We can use a slice operator [] and [:] to get items in the list. The concatenation operation can be done using plus (+) sign and asterisk(*) serves as the repetition operator.
Below is an example of list with simple operations:-
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tinylist = [123, 'john'] print(list) # Prints complete list print(list[0]) # Prints first element of the list print(list[1:3]) # Prints elements starting from 2nd till 3rd print(list[2:]) # Prints elements starting from 3rd element print(tinylist * 2) # Prints list two times print(list + tinylist) # Prints concatenated lists
Output:-
abcd
[786, 2.23]
[2.23, ‘john’, 70.2]
[123, ‘john’, 123, ‘john’]
[‘abcd’, 786, 2.23, ‘john’, 70.2, 123, ‘john’]
Python Tuples
Tuples are an immutable sequence of items meaning it cannot be modified once the variable is created. They can be viewed as read-only lists. The data can be heterogeneous as the list. Tuples are enclosed in small brackets (). We can use a slice operator [] and [:] to get items in the list. The concatenation operation can be done using plus (+) sign and asterisk(*) serves as the repetition operator.
Example of Tuple:-
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tuple[2] = 1000 # Invalid syntax with tuple list[2] = 1000 # Valid syntax with list
Python Sets
Sets are unordered collection in Python containg unique items. They are created using braces {} and its items being separated by a comma. The items inside sets are not ordered as in lists and tuples. Sets allows operations like union, intersection on two different sets. The main advantage of set is that it eliminates the duplocates in the items. Sets cannot be accessed using slicing operator as there is nothing called index in a set.
a = {5,2,3,1,4} fruits = {'apple', 'banana', 'mango'} print(a) print(fruits)
Output:-
set([‘mango’, ‘apple’, ‘banana’])
Python Dictionary
In Python, dictionary is one of the useful data type that can contain key-value pairs. This is used when we have different kinds of data in a larger volume. They are optimized for retrieving data and are used by many developers as they provide ease of use. In Python, braces {} are used to define dictionaries where each item is a pair in the form of key: value. Here, key and value can be of any type.
Let us use an example below to be more famlier with Dictinary data type in Python.
student = {'name': 'John', 'surname': 'Doe'} student['name'] = 'Alex' # set the value print (student['name']) # print the value. student['roll'] = 34 # Add a new key 'roll' with the associated value print (student.keys()) # print out a list of keys in the dictionary print ('roll' in student) # test to see if 'roll' is in the dictionary. This returns true.
Output:
[‘surname’, ‘name’, ‘roll’]
True
How to find type of python variable?
We can use type() function in python to know under which data type the variable belongs to.
a = 200 print(a, "is of type",type(a)) x = 3 + 5j print(x, "is of type",type(x))
Output:-
(3+5j) is of type <class ‘complex’>