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
>>> 5 - 3
>>> 4 * 5
>>> 70 / 10
>>> INT (70 / 10)
>>> 4**3
>>> 10%3

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')

\n represents next line

To use \n in a statement, r can be prefixed as in the following example
>>> print("C:\Some\name")

To change it
>>> print(r"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'

2 or more strings written side by side are automatically concatenated
>>> 'Py' 'thon'

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'

Indexing Strings
>>> word = "PythonString"
>>> word[2]
>>> word[10]

Reverse Indexing
>>> word[-2]
>>> word[-6]

String Slicing
>>> word[2:6]
>>> word[-4:-8]

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:]
>>> word[:-4]

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))

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.
>>> squares = [1,4,9,16,25,36]
>>> squares
>>> 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)

>>>cubes = squares + [1,8,27,64,125]
[1,4,9,16,25,36, 1,8,27,64,125]
>>>cubes[6] = 10
>>> print(len(cubes))
>>>cubes[:] = []
>>> cubes

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]
>>> nestedlist[3]

Read our previous posts: Python Introduction 1 and 2

Download Python here