Data types in python

Nikhil Pandey
2 min readMay 13, 2023

--

⏮️previous page ⏭️next page

Data types are means to identify type of data. Python offers following build-in core data types:

(1) Numbers (2) String (3) List (4) tuple (5) Dictionary

⭐ Data Types for numbers:

Python offers following data types to store and process different types of numeric data:

(a)⭐Integer : Integer(signed*): It is the normal integer representation of whole numbers. Python3 provides single data types(int) to store any integer, whether big or small.

*It is signed representation, i.e. the integer can be positive as well as negative.

Booleans. These represents the truth values False and True . The Boolean type is a subtype of plain integers, and boolean values False and True behaves like the values 0 and 1, respectively.

(b) Floating point numbers. In python, floating point numbers represent machine-level double precision floating point numbers (15 digit precision). The range of these numbers is limited by underlying machine architecture subject to available (virtual) memory.

(c) Complex numbers. Python represents complex numbers in the form A+Bj. Complex numbers are a composite quantity made of two parts: The real part and the imaginary part, both of which are represented internally as float values (floating point numbers).

You can retrieve the two components. For a complex number z:

⭐z.real gives the real part

⭐z.imag gives the imaginary part as a float, not as a complex value.

>>>a=2+3j
>>>type(a)
>>>class 'complex'>

#this is a comment

>>>a.imag
>>>3.0
>>>a.real
>>>2.0

#code end here

Data types for strings..

All string in python3 are sequences of pure Unicode characters. Unicode is a system designed to represent every character from every language. A string can hold any type of known characters i.e. letters, numbers, and specific characters, of any known scripted language.

Following are all legal strings in python:

a='apple'
type(a)
>>> <class 'str'>

#another example

b='1234'
type(b)
>>> <class 'str'>

(3) Lists A list in python represents a group of comma-separated values of any data type between square brackets e.g. following are some lists:

l=[1,2,3,4]
type(l)
>>> <class 'list'>

#another example

p='monkey'
p=list(p)
print(p)
>>> ['m', 'o', 'n', 'k', 'e', 'y']

(4) Tuples Tuples are separated as group of comma-separated values of any data type within parentheses, e.g. following are some tuples:

#example 1
q=1,2,3
type(q)
>>> <class 'tuple'>

#example 2
w=1,
type(w)
>>> <class 'tuple'>

#example 3
a='apple'
e=tuple(a)
print(e)
>>> ('a', 'p', 'p', 'l', 'e')

(5) Set A set is a mutable data type which is created like lists, but with []. It can take values of different types but cannot mutable elements and duplicate engries e.g.

>>>set1={1,2,3}
>>>set2={1,2,3,4,3,2,1}
>>>set2
{1, 2, 3, 4}
>>>set1
{1, 2, 3}

#python terminates identical values

(6) Dictionary The dictionary is an unordered set of comma-separated …. key:value pairs within[], with the requirements that within a dictionary, no two keys can be the same (i.e. there are Unicode keys within a dictionary). For instance, following are some dictionaries :

>>>dic={'name':'Harry','age':'32'}
>>>dic
{'name': 'Harry', 'age': '32'}
>>>dic['age']
'32'

That’ll for today! Bye (please mail me if your find anything to be improved. pandeynikhilone@gmail.com) or Have any suggestion! fill form

⏮️previous page ⏭️next page

--

--

Nikhil Pandey

I am a student and a programming nerd. I learn and teach topics to help my fellow readers understand and apply their programming knowledge fluently.