top of page
Writer's pictureRajesh Singh

Data Types in Python with examples

Updated: Feb 16, 2022


The type of the variable is known as Python Data type like integer variable, string variable, tuple, dictionary, list etc. There are two categories of Python data types, one is mutable data types and second is immutable data types.


Immutable Data types in Python

  1. Numeric

  2. String

  3. Tuple

Mutable Data types in Python

  1. List

  2. Dictionary

  3. Set

Immutable Data types in Python

1. Numeric Data Type in Python

Integer – In 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.


Exam:

num = 10

print(num)



Float –The float values are those values which have decimal points, there is no need to identify the float data type in Python. It is define by assigning to a variable like fnum is a float data type.


Exam:

fnum = 11.54

print(fnum)

Output:

11.54

Complex Number – Complex number is the combination of real and imaginary parts. Python is capable to identify these complex numbers with the values like cnum is a complex number data type.

Exam:

cnum = 3 + 4j

print(cnum)

Output:

3+4j



2. String Data Type in Python

A sequence of characters is known as String in Python. The representation of string data type in Python is “str”. Strings are enclosed with single quotes or double quotes in Python. For example we can take two string one with the double quotes and other string s2 with the single quotes.

Example:

s1= "This is a String"

s2 = 'This is also a String'

print(s1)

Output: This is a String

print(s2)

Output: This a also a String



3. Tuple Data Type in Python

Tuple data type cannot be changed in Python. It is an ordered collection of elements enclosed in round brackets and separated by commas.

# tuple of integers

t1 = (5, 6, 7, 8, 9)

print(t1)

Output:

(5, 6, 7, 8, 9)


# tuple of strings

t2 = ("hey", "hello", "by")

# loop through tuple elements

for s in t2:

print (s)

Output:

hey

hello

by


# tuple of mixed type elements

t3 = (5, "Mom", 40, "Dad")

print(t3[1])

Output: Mom



Mutable Data types in Python

1. List Data Type in Python

List is an ordered collection of elements like tuple but it can be changed. A list is enclosed with square brackets and elements are separated by commas.

# list of integers

lis1 = (5, 6, 7, 8, 9)

print(lis1)

Output: (5, 6, 7, 8, 9)

# list of strings

lis2 = ("Pear", "Mango", "Banana")

for x in lis2:

print (x)

Output:

Pear

Mango

Banana

# List of mixed type elements

lis3 = (10, "Rajesh", 20, "Singh")

print(lis3[3])

Output:

Singh


2. Dictionary Data Type in Python

A collection of key and value pairs is known as Dictionary. It doesn’t allow repeated keys but the values can be duplicate. It is an ordered, indexed and mutable collection of elements.

Exam:

dict = {1:"Rajesh","lastname":"Singh", "age":38}

print(dict[1])

Output: Rajesh

print(dict["lastname"])

Output: Singh

print(dict["age"])

Output: 38


3. Set Data Type in Python

An unordered and unindexed collection of items is known as a set. It means when we print the elements of a set they will appear in the random order and we cannot access the elements of set based on indexes because it is unindexed. Elements of set are enclosed in curly braces and separated by commas

# Set Example

myset = {"hey", 5, "by", "Hello Ram"}

for a in myset:

print(a)

Output:

by

5

hey

Hello Ram



26 views0 comments

Recent Posts

See All

For Loop MCQs in Python

Q1. What is the output of the following Python code? x={1:"X",2:"Y",3:"Z"} for i,j in x.items():     print(i,j,end=" ") A.    1 X 2 Y 3 Z...

Comments


bottom of page