Different types of Data types | Data types in python

Python Data Types

data types in python




Python data types are different in some aspects from other programming languages. It is simple to understand and easy to use. Because Python is interpreted programming language and a Python interpreter can determine which type of data is storing, so no need to define the data type of memory location. 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. String
  3. List
  4. Tuple
  5. Dictionary


Numbers

Integers, floating-point numbers, and complex numbers fall under the Python numbers category. They are defined as int, float, and complex classes in Python.

Number stores numeric values. Python creates Number objects when a number is assigned to a variable. Complex numbers are written with a "j" as the imaginary part. To verify the type of an object in Python, use the type() function.

Example:

a = 30 #Integer type
print(a)
print(type(a))

a = 10.32 #Float type
print(a)
print(type(a))

a = 20+0j #Complex type
print(a)
print(type(a))

Output:

30
<class 'int'>
10.32
<class 'float'>
(20+0j)
<class 'complex'>


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. Subsets of strings can be taken using the slice operator ([ ] and [:]) with indexes starting at 0 at the beginning of the string and working their way from -I at the end. The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator.

Example:

str"Hello Python"

print (str)
print (str[0])
print (str[1:4])
print ( str[4:])

print (str3)
print (str + "PYTHON")

Output:

Hello Python
H
ell
o Python
Hello PythonHello PythonHello Python
Hello PythonPYTHON


List

Lists are similar to arrays in C. However; the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets []. 

We can use slice [:] operators to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings.

Example:

list = [ 'abc'123'hi'10.1210]
print (list)
print (list[0])
print (list[2:4])
print (list[2:])
print (list *2)
print (list + list)

Output:

['abc'123'hi'10.1210]
abc
['hi'10.12]
['hi'10.1210]
['abc'123'hi'10.1210'abc'123'hi'10.1210]
['abc'123'hi'10.1210'abc'123'hi'10.1210]


Tuple

A tuple is similar to the list in many ways. Like lists, tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses (). A tuple is a read-only data structure as we cant modify the size and value of the items of a tuple.

Example:

tuple = ('abc'123'hi'10.1210)

print (tuple[0])
print (tuple[2:4])
print (tuple[3:])
print (tuple * 2)
print (tuple + tuple)
print (tuple)

Output:

abc
('hi'10.12)
(10.1210)
('abc'123'hi'10.1210'abc'123'hi'10.1210)
('abc'123'hi'10.1210'abc'123'hi'10.1210)
('abc'123'hi'10.1210)


Python Dictionary

Dictionary is an ordered set of a key-value pair of items. They work like associative arrays or hashes and consist of key-value pairs. A dictionary key can be almost any Python type but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces (|).

Example:

d = {1:'abc'2:'xyz'3:'pqr'}

print (d[1])
print (d[3])
print (d)
print (d.keys())
print (d.values())

Output:

abc
pqr
{1'abc'2'xyz'3'pqr'}
dict_keys([123])
dict_values(['abc''xyz''pqr'])
Next Post Previous Post
No Comment
Add Comment
comment url