Posted by
farhan khwaja
on
Tuesday, December 13, 2011
Read our
previous post

This Post is about
numbers and mathematical operations. In this tutorial we shall be
covering data types, operators and type conversion. To represent
'

**Numbers**' in python, we have**int, float, complex datatypes**. For conditional statements, we have '**Bool**' datatype
Type

**ipython**on the Terminal ( Ctrl + Alt + T ) to start the interpreter :**$ ipython**

## Numbers and their Data types :

The speciality of Python is that any number irrespective of its size can be represented. It doesn't have any size constraints like any other programming language.

Before we move on let me first tell you about two functions :-

**print()**: The

*print()*function prints the value inside the brackets onto the screen i.e. helps us to see the output.

**type() :**The

*type()*function is the one which we will be using in this tutorial to determine the

**Data Type**of that particular variable.

**Note :**

To know about any function and its usage, type the function name followed by a ?.

Example :

**type ?**

**Ex.**

type the following on the interpreter:

1. x = 13

2. print(x) // This will print the value of variable x.

3. type(x) // This will data type to which x belongs.

As I stated above that we Python can store value of any size so to check that again perform the above 3 steps with giving value as '9999999999' to x. i.e. x = 9999999999.

When you use the

**type()**function you will get to know the data type of the variable.

**NOTE :**Along with the traditional data types such as INT, FLOAT etc Python has an extra Data Type known as

**COMPLEX**. The Below image shows that.

**Normally complex number have the format '**,

*x+iy*' but in python the format is '*x+yj*'. After you declare a variable i.e p = 4+5j**then you can check the real and imaginary part of the varaible by the following**

**For REAL**:

**p.real****For Imaginary :**

*p.imag (*As shown above in the image)

*You can also use the*

**abs()**which will give the absolute value of the variable 'p'

**Boolean :**

Its a primitive datatype having one of the two values :

*or*

**True****.**

*False***Note :**

Python is a Case Sensitive Language, so True with 'T' is a boolean type but true with 't' would be a variable.

We can do all kinds of binary operations i.e 'or' 'and' with these boolean variables.

ln [0] : t = True

ln [1] : f = False

ln [2] : t or f

Out [0] : True

## Arithmetic Operations :

Python takes care of all the mathematical operations with all data types.

**Addition**:

*print x + y -*Prints the addition of x and y.

**Multiplication**:

*print x * y -*Prints the multiplication of x and y.

**Division**:

*print x / y -*Prints the division of x and y.

**Raised to Power**:

*print x ** y -*Prints the x raised to power of y.

**The Below image shows all of the above Mathematical Operations with int, float and complex data types**.

## Operators :

Like every other programming language Python also has operators, which help us to increment, decrement or find the modulo of the number. So here are few examples on the same:

**Modulo**:

Type the following in the terminal with the interpreter on :

178232 % 2

0 ( Answer Returned by the Interpreter )

**Increment**:

ln [1] : a = 192823

ln [2] : a += 2

ln [3] : print a

**192825**( Answer returned )

**Decrement**:

ln [1] : a = 192823

ln [2] : a -= 2

ln [3] : print a

**192821**( Answer returned )

**Multiplication**:

ln [1] : a = 113

ln [2] : a *= 2

ln [3] : print a

**226**( Answer returned )

**Division**:

ln [1] : a =20

ln [2] : a /= 2

ln [3] : print a

**10**( Answer returned )

**:**

Relational and Logical Operators

Relational and Logical Operators

Python also has support for relational and logical operators. Lets try some examples:

We start with initializing three variables by typing

p, z, n = 1, 0, -1

**To check equivalency of two variables use '=='**

p == z

checks if 1 is equal to 0 which is False

p >= n

checks if 1 is greater than or equal to -1 which is True

**We can also check for multiple logical operations in one statement itself.**

n < z < p

gives True. This statement checks if 'z' is smaller than 'p' and greater than 'n'.

For inequality we use (

**!**)## Type Conversion :

Python also supports type casting and type conversion like any other programming language.

**Ex. :**

In the above figure you can see the type casting done.