Udit Vashisht
Author: Udit Vashisht


Chapter 7- Numbers, Conversions and Operators

Chapter 7- Numbers, Conversions and Operators

  • 3 minutes read
  • 78 Views
Chapter 7- Numbers, Conversions and Operators

    Table of Contents

In the previous chapter, we learned about various datatypes. From this chapter onward, we will learn in detail about each data type.

Numbers in Python

This datatype stores the numeric values and is immutable. For the beginners, it is sufficient to understand that immutable objects are those which can not be changed once created (Though this subject needs a deeper and detailed post). It can be further divided into three types:-

  1. int - Often called integers, these are positive or negative whole numbers with no decimal point. Prior to Python 3, Python 2 had int and long int types but the long int has been discontinued in Python 3 and now the int can be of unlimited size. e.g. 1, 2, -3, 99 etc.
  2. float - They have decimal point separating the integer from the fraction e.g. 22.0 , 5.0, -22.0 etc.
  3. complex - They are of the form a + bj. ‘a’ is the real part, ‘b’ is the imaginary part and j is the square root of -1. Also, both ‘a’ and ‘b’ are floats.

Python has a built-in function type() which tells you the data-type.

>>> type(2)
<class 'int'>
>>> type(2.0)
<class 'float'>
>>> type(3+4j)
<class 'complex'>

Coercion in Python

In Python, one type of number can be easily converted into another type and it is called ‘coercion’.

Any mathematical operation will coerce an integer to a float if one of the operands is a float. Also, the result of the division (/) will always be a float irrespective of whether the operands were integers or floats.

>>> 1 + 2.0
3.0
>>> 1*5.0
5.0
>>> 5/1
5.0
>>> a = 10
>>> b = 2
>>> c = a/b
>>> type(a)
<class 'int'>
>>> type(b)
<class 'int'>
>>> type(c)
<class 'float'>
>>> 

Alternatively, you can convert the numbers as under:-

  1. int(x) will convert x to a plain integer. Also, it will convert the float into the lower integer. i.e int (2.9) will give 2.
  2. float(x) will convert x to a floating-point number.
  3. complex(x) will convert x to a complex number with imaginary part ‘0’.
  4. complex(x,y) will result into x + yj.
>>> int(1.0)
1
>>> int (2.9)
2
>>> float (25)
25.0
>>> complex(2)
(2+0j)
>>> complex(2,3)
(2+3j)

Operators in Python

Before getting into operators, we must note that in all the below mentioned operators except the division (/) operator, the result will be float only if one or both of the operands are floats. If both of them are integers, then the result will be an integer.

  1. Addition (+) - e.g. 2 + 1 = 3.
  2. Subtraction(-)- e.g. 3 - 1 = 2.
  3. Multiplication(*) - e.g. 5 * 2 = 10.
  4. Division(/) - e.g. 4/2. In this case the result will be a float i.e. 2.0 even when both the operands were integers.
  5. Truncation/Floor Division(//). It will truncate the fractional part if any and result will be an integer. e.g. 5//2 = 2. In mathematical terms, it will give you the quotient.
  6. Modulo (%)- It will give the remainder as an answer. e.g. 5 % 3 = 2.
  7. Exponentiation - e.g. 5 ** 2 = 25.
>>> 2 + 1
3
>>> 2.0 + 1
3.0
>>> 3 - 1
2
>>> 3 - 1.0
2.0
>>> 5 * 2 
10
>>> 5.0 * 2
10.0
>>> 4/2
2.0
>>> 5//2
2
>>> 5//2.0
2.0
>>> 5%3
2
>>> 5.0%3
2.0
>>> 5**2
25

Table of Contents

Chapter 6 - Data Types & Variables



Related Posts

Matplotlib Stack Plots/Bars | Matplotlib Tutorial in Python | Chapter 4
By Udit Vashisht

Matplotlib Tutorial in Python

*Matplotlib Stack Plots/Bars | Chapter 4

In this chapter we will learn to create Matplotlib/Python Stack Plots/Bars.Stack Plots are also called area graphs/plots in general.

Matplotlib Tutorials in Python - Creating a Simple Stack Plot in Matplotlib

A good example of using a stack ...

Read More
Matplotlib Tutorial in Python | Chapter 2 | Extracting Data from CSVs and plotting Bar Charts.
By Udit Vashisht

Matplotlib Tutorial in Python

Chapter 2 | Extracting Data from CSVs and plotting Bar Charts

In the last chapter, we learned to draw simple plots in Matplotlib and further customizing it.In this chapter we will be learning to extract data from external sources like CSV and creating ...

Read More
Introduction to Python Language - Chapter 1
By Udit Vashisht

Introduction to Python

Curiosity and zeal to learn something new have probably brought you here. It doesn’t matter whether you are new to programming or switching from some other redundant or less useful language, you must be having a few very important questions in your mind. We will be answering ...

Read More
Search
Tags
tech tutorials automate python beautifulsoup web scrapping webscrapping bs4 Strip Python3 programming Pythonanywhere free Online Hosting hindi til github today i learned Windows Installations Installation Learn Python in Hindi Python Tutorials Beginners macos installation guide linux SaralGyaan Saral Gyaan json in python JSON to CSV Convert json to csv python in hindi convert json csv in python remove background python mini projects background removal remove.bg tweepy Django Django tutorials Django for beginners Django Free tutorials Proxy Models User Models AbstractUser UserModel convert json to csv python json to csv python Variables Python cheats Quick tips == and is f string in python f-strings pep-498 formatting in python python f string smtplib python send email with attachment python send email automated emails python python send email gmail automated email sending passwords secrets environment variables if name == main Matplotlib tutorial Matplotlib lists pandas Scatter Plot Time Series Data Live plots Matplotlib Subplots Matplotlib Candlesticks plots Tutorial Logging unittest testing python test Object Oriented Programming Python OOP Database Database Migration Python 3.8 Walrus Operator Data Analysis Pandas Dataframe Pandas Series Dataframe index pandas index python pandas tutorial python pandas python pandas dataframe python f-strings padding how to flatten a nested json nested json to csv json to csv python pandas Pandas Tutorial insert rows pandas pandas append list line charts line plots in python Django proxy user model django custom user model django user model matplotlib marker size pytplot legends scatter plot python pandas python virtual environment virtualenv venv python python venv virtual environment in python python decorators