Python Data Types

The Python programming language provides a variety of built-in data types. The following are the most commonly used built-in data types in Python.

  • None
  • Numeric
  • String
  • List
  • Tuple
  • Set
  • Range
  • Dictionary

Let's look at each built-in data type in Python.

'None' data type in Python

In many programming languages, we use the value null to represent nothing. 'None' is Python's equivalent of NULL value in other programming languages. In Python, 'None' is the object which represents nothing. When we want a value to hold nothing, we do assign it with value 'None'.


 

 

'Numeric' data type in Python

The Python programming language provides four numeric data types. They are as follows.

  • int - All the numbers without fraction part (Example - 10). For int, there is no upper limit.
  • float - All the numbers with a fraction part (Example - 10.5). It’s accurate up to 15 decimal places
  • complex - All the numbers with real and imaginary parts (Example - 5 + 10j).
  • bool - boolean values True and False.

 

 

 

 

'String' data type in Python

A string is a sequence of character data which are confined in either a single quote or double quote. The string data type in Python is called as 'str'. In Python, a string can contain as many characters as you wish. The only limit is the system's memory. In Python, the string may also be an empty string.

 

 

'List' data type in Python

The list is the most commonly used data type available in Python. In Python, the list data type values are enclosed in square brackets ([ ]) and each value is separated with comma symbol. In Python, the list data type may contain different data type values.

 

 

 

'Tuple' data type in Python

The tuple is similar to list in Python. In Python, the tuple data type is immutable. That means the tuples cannot be modified, unlike lists. In Python, the tuples may contain different data type values. The tuples are enclosed in parentheses. 


 

 

'Set' data type in Python

In Python, a set is a collection of an unordered and unindexed data elements of different data types. In Python, the set data type elements are immutable (duplicates are not allowed). However, set itself is mutable. Set is created with curly brackets.

 

 

'Range' data type in Python

In Python, range( ) is a built-in function used to create a list of integers with a specified range of values.

Syntax

variable_name = range(start, end, difference)

Example

 

num = range(10,20,1)
print(type(num))
print(list(num))

In the above example Python code the variable num is stored with a list [10,11,12,13,14,15,16,17,18,19]. The range( ) function takes three arguments. The first argument indicates the starting value, the second argument indicates ending value, and the third argument indicates the difference between each element in the list.  

 

Note:

  • In Python, the default starting value of range( ) function is 0.
  • In Python, the default difference between the values of range( ) function is 1.
  • In Python, the ending value is not included in the list. That means, the range( ) function returns a list starting with starting value and ending with End-1 but end value is not included.

 

'Dictionary' data type in Python

In Python, a dictionary is similar to list, but dictionary stores each element as a pair of Key and Value. In list, every element is accessed using index value, whereas in the dictionary we use, a unique key to access a value. In a dictionary, every key and value are separated with a colon (:) symbol and each pair separated with comma (,) symbol. In Python, the dictionary data type is called 'dict'.

 


 

 

No comments:

Post a Comment