## Data types in Python

When using Python, the best feature it provides over other languages is the versatility using its data types. There are Numbers, Strings, Lists as major data types in Python. While we consider other data types like Tuples, Dictionary, and other types too while working.

## Number data type – Using Python as a Calculator

One of the basic use of Python is to calculate and derive mathematical solutions to real-world problems. The biggest challenge in the current world is numbers. The number of devices increases and the number of users exponentially are increasing every day. So the data is the most important factor that drives today’s eWorld.

Python interpreter understands numbers as the basic data type. Hence, python is used as a calculator to calculate basic mathematical equations.

The **addition, subtraction, multiplication, division, power, modulus** are used normally as we use in a calculator.

The following equations show how the python interpreter calculates the given expression.

`>>> 3 + 7`

10

>>> 5 - 3

2

>>> 4 * 5

20

>>> 70 / 10

7.0

>>> INT (70 / 10)

7

>>> 4**3

64

>>> 10%3

1

These were some common usages of Python interpreter as a Calculator.

In addition to Numbers (Integers and Floating Point), Python also supports other data types such as **fractions** and **complex numbers**.

## String data type in Python

Python treats strings in a double quote or single quote, the same way.

Which means “Python” and ‘Python’ seem the same to python interpreter.

Escaping single quote required a backslash to print it.

`>>> print( 'Don\'t')`

Don't

\n represents next line

`>>>print("Hi.\nHello")`

Hi

Hello

To use \n in a statement, r can be prefixed as in the following example

`>>> print("C:\Some\name")`

C:\Some

ame

To change it

`>>> print(r"C:\Some\name")`

C:\Some\name

When string literals are of multiple lines, using a “””. . .””” or ”’. . .”’ can be very helpful.

`>>> print("""\`

This is line one.

This is line two.

This is line three

""")

This is line one.

This is line two.

This is line three

Strings can be manipulated using + for concatenation and * for multiplication

2 times ‘co’ and added with ‘nut’

`>>> 2*'co'+'nut'`

'coconut'

2 or more strings written side by side are automatically concatenated

`>>> 'Py' 'thon'`

'Python'

**Note**: This works only with literals and not with any variables or expressions.

When concatenating a variable and literal using + symbol is helpful

`>>> name = 'Py'`

>>> name + 'thon'

'Python'

Indexing Strings

`>>> word = "PythonString"`

>>> word[2]

't'

>>> word[10]

'n'

Reverse Indexing

`>>> word[-2]`

'n'

>>> word[-6]

'S'

String Slicing

`>>> word[2:6]`

'thonS'

>>> word[-4:-8]

'onStr'

When one of the ends isn’t given in the string slicing statement, it takes the complete length of the string

For example,

`>>> word[5:]`

'nString'

>>> word[:-4]

'PythonSt'

**Note**: Python strings cannot be changed or modified. They are immutable. Hence assigning a value using indexing raises an exception and is an error.

len() – returns the length of the given string.

`>>> s = "This is a string."`

>>> print(len(s))

17

## Lists data type – Arrays in Python

Lists are special data type which can hold variables of different types separated by a comma and bounded by square braces.

Example:

`>>> squares = [1,4,9,16,25,36]`

>>> squares

[1,4,9,16,25,36]

>>> type(squares)

<type 'list'>

**Note**: type() is a function used to determine the type of the given variable.

All the operations used on strings can be applied to the Lists.

They can be indexed, sliced, concatenated, doubled (using * operator) but the major difference is that lists are mutable which means the values in a list can be modified or deleted.

len() gives the length of the list (the number of elements in the list)

`>>>squares[0]`

1

>>>squares[1:3]

[4,9,16]

>>>squares[:-3]

[1,4,9,16]

>>>cubes = squares + [1,8,27,64,125]

>>>cubes

[1,4,9,16,25,36, 1,8,27,64,125]

>>>cubes[6] = 10

>>>cubes

[1,4,9,16,25,36,10,8,27,64,125]

>>>cubes.append(6**3)

>>>cubes

[1,4,9,16,25,36,10,8,27,64,125,216]

>>> print(len(cubes))

12

>>>cubes[:] = []

>>> cubes

[]

>>>print(len(cubes))

0

Nested Lists

Lists within lists are known as nested lists.

For example,

`>>> nestedLists = [1,2,3,[4,5,6],[7,8,9],10]`

>>> nestedList[2]

3

>>> nestedlist[3]

[4,5,6]

Read our previous posts: Python Introduction 1 and 2

Download Python here