Python Data Types
A variable can hold different types of values. For example, a
person's name must be stored as a string whereas its id must
be stored as an integer.
Python provides various standard data types that define the
storage method on each of them. The data types defined in
Python are given below.
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Numbers
Number stores numeric values. The integer, float, and complex
values belong to a Python Numbers data-type. Python provides
the type() function to know the data-type of the variable.
Similarly, the isinstance() function is used to check an object
belongs to a particular class.
Python creates Number objects when a number is assigned to a
variable. For example;
1. a=5
2. print("The type of a", type(a))
3.
4. b = 40.5
5. print("The type of b", type(b))
6.
7. c = 1+3j
8. print("The type of c", type(c))
9. print(" c is a complex number", isinstance(1+3j,complex))
Output:
The type of a <class 'int'>
The type of b <class 'float'>
The type of c <class 'complex'>
c is complex number: True
Python supports three types of numeric data.
1. Int - Integer value can be any length such as integers 10,
2, 29, -20, -150 etc. Python has no restriction on the length
of an integer. Its value belongs to int
2. Float - Float is used to store floating-point numbers like
1.9, 9.902, 15.2, etc. It is accurate upto 15 decimal points.
3. complex - A complex number contains an ordered pair,
i.e., x + iy where x and y denote the real and imaginary
parts, respectively. The complex numbers like 2.14j, 2.0 +
2.3j, etc.
Sequence Type
String
The string can be defined as the sequence of characters
represented in the quotation marks. In Python, we can use
single, double, or triple quotes to define a string.
String handling in Python is a straightforward task since Python
provides built-in functions and operators to perform operations
in the string.
In the case of string handling, the operator + is used to
concatenate two strings as the operation "hello"+"
python" returns "hello python".
The operator * is known as a repetition operator as the
operation "Python" *2 returns 'Python Python'.
Example - 1
1. str1 = 'hello javatpoint' #string str1
2. str2 = ' how are you' #string str2
3. print (str1[0:2]) #printing first two character using slice operator
4. print (str1[4]) #printing 4th character of the string
5. print (str1*2) #printing the string twice
6. print (str1 + str2) #printing the concatenation of str1 and str2
Output:
he
o
hello javatpointhello javatpoint
hello javatpoint how are you
List
Lists are used to store multiple items in a single
variable.
Lists are one of 4 built-in data types in Python used to
store collections of data, the other 3 are Tuple, Set,
and Dictionary, all with different qualities and usage.
Lists are created using square brackets:
Example
Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
Output
['apple', 'banana', 'cherry']
Python Tuples
mytuple = ("apple", "banana", "cherry")
Tuple
Tuples are used to store multiple items in a single
variable.
Tuple is one of 4 built-in data types in Python used to
store collections of data, the other 3 are List, Set,
and Dictionary, all with different qualities and usage.
A tuple is a collection which is ordered
and unchangeable.
Tuples are written with round brackets.
Example
Create a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
Output
{'banana', 'cherry', 'apple'}
Python Sets
Set
Sets are used to store multiple items in a single
avariable.
Set is one of 4 built-in data types in Python used to
store collections of data, the other 3 are List, Tuple,
and Dictionary, all with different qualities and usage.
A set is a collection which
is unordered, unchangeable*, and unindexed.
* Note: Set items are unchangeable, but you can
remove items and add new items.
Sets are written with curly brackets.
Example
Create a Set:
thisset = {"apple", "banana", "cherry"}
print(thisset)
Dictionary
Dictionaries are used to store data values in key: value
pairs.
A dictionary is a collection which is ordered*,
changeable and do not allow duplicates.
As of Python version 3.7, dictionaries are ordered. In
Python 3.6 and earlier, dictionaries are unordered.
Dictionaries are written with curly brackets, and have
keys and values:
Example
Create and print a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Output
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
Python Booleans
Booleans represent one of two values: True or False.
Boolean Values
In programming you often need to know if an
expression is True or False.
You can evaluate any expression in Python, and get
one of two answers, True or False.
When you compare two values, the expression is
evaluated and Python returns the Boolean answer:
Example
print(10 > 9)
print(10 == 9)
print(10 < 9)
Output
True
False
False