(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
Updated on Jan 07, 2020
This data type supports only numerical values like
Python supports 3 different numerical types.
int- for integer values like
float- for floating-point values like
complex- for complex numbers like
Integer literals in python belong to
1 2 3
>>> i = 100 >>> i 100
Floating points are values with decimal point like.
1 2 3
>>> f = 12.3 >>> f 12.3
One point to note that when one of the operands for numeric operators is a float value then the result will be in float value.
>>> 3 * 1.5 4.5
Complex number #
As you may now complex number consists of two parts real and imaginary, and is denoted by
j.You can define complex number like this:
>>> x = 2 + 3j # where 2 is the real part and 3 is imaginary
Determining types #
type() inbuilt function which is use to determine the type of the variable.
1 2 3
>>> x = 12 >>> type(x) <class 'int'>
Python operators #
Python has the different operators which allows you to carry out required calculations in your program.
* works as expected, remaining operators require some explanation.
Float Division (
/) : The
/ operator divides and return result as floating point number means it will always return fractional part. For e.g
>>> 3/2 1.5
Integer Division (
//) : The
// perform integer division i.e it will truncate the decimal part of the answer and return only integer.
>>> 3//2 1
Exponentiation Operator (
**) : This operator helps to compute ab (a raise to the power of b). Let's take an example:
>>> 2 ** 3 # is same as 2 * 2 * 2 8
Remainder operator (%) : The
% operator also known as remainder or modulus operator. This operator return remainder after division. For e.g:
>>> 7 % 2 1
Operator Precedence #
In python every expression are evaluated using operator precedence. Let's take an example to make it clear.
>>> 3 * 4 + 1
In the above expression which operation will be evaluated first addition or multiplication? To answer such question we need to refer to operator precedence list in python. Image below list python precedence order from high to low.
As you can see in table above
* is above
* will occur first then addition. Therefore the result of the above expression will be
>>> 3 * 4 + 1 >>> 13
Let's,take one more example to illustrate one more concept.
>>> 3 + 4 - 2
In above expression which will occur first addition or subtraction. As we can see from the table
- have same precedence, then they will be evaluated from left to right, i.e addition will be applied first then subtraction.
>>> 3 + 4 - 2 >>> 5
The only exception to this rule is assignment operator (
=) which occur from right to left.
a = b = c
You can change precedence by using parentheses
(), For e.g:
>> 3 * (4 + 1) >> 15
As you can see from the precedence table
() has highest priority so in expression
3 * (4 + 1),
(4 + 1) is evaluated first then multiplication. Hence you can use
() to alter order of precedence.
Augmented Assignment Operator #
These operator allows you write shortcut assignment statements. For e.g:
1 2 3 4
>>> count = 1 >>> count = count + 1 >>> count 2
by using Augmented Assignment Operator we can write it as:
1 2 3 4
>>> count = 1 >>> count += 1 >>> count 2
Similarly you can use
** with assignment operator to form augmented assignment operator.
||Integer division assignment||
In the next post we will learn about python strings.
Other Tutorials (Sponsors)
This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!