• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2024
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Python Numeric Data Types | Detail Guide with Examples

Aniruddha Chaudhari/38436/4
CodePython

There are four main Python Numeric Data Types:

  • int (Integer)
  • long
  • float
  • complex
  • boolean (Subset of integer)

You might be thinking why do I mention 5 data types in the above list. It has a reason and I will clear your doubt in this article.

I keep talking Python is far different from all other programming languages.

If you are taught for any of the programming languages, you might be aware of these data types. But there are some interesting facts about Python which is different from other programming languages.

In C programming, we explicitly mention the data type of the variable.

for example,

int count = 10;
float perc = 80.45;

In Python programming, we don’t specify the data type of the variable explicitly.

for example,

count = 17;
perc = 77.05;

Dynamic data types in Python:

The data type of the variable is identified at the run-time based on the type of value you are assigning to the variable.

Anytime in the program, you can change the data type of any variable by passing different value.

This is called dynamic data types in Python.

Now, let’s see in detail about numeric data types in Python.

1. int

Implementation of int in Python is similar to the long in C.

It has a precision limit of 32 bits. So one can set any integer value up to 2^32-1.

Example:

num = 20
type(num)
#<type 'int'>

Here are some examples you can try on integer data type:

  • Write a program to swap two int value without using any temp variable. (It is just one line of code in Python.)

How to find the maximum and minimum value accepted by the integer?

Finding max value of integer

import sys
sys.maxint
#2147483647

Finding min value of integer

You can calculate its minimum value by a simple code of line.

|import sys
-sys.maxint - 1
#-2147483648

So then what will happen if we provide the value to the int variable out of its range?

As we know we don’t specify the data type of value while using a numeric variable in python. When you assign the value out of int size limit, the variable is converted to long int.

2. long

There is no precision limit to the long integer.

Yes, it’s right!

So you can enter any number without limit. When you assign value to the value greater than int max limit (i.e. 2147483647), it is treated at long int.

for example,

#World Total Population
population =7514815137
type(population)
#<type,'long'>

In fact, this limit depends on the size of memory (RAM). The size of the value in a variable cannot be greater than the size of actual memory space you are using for running python program.

But how can you test it out the limit range of long int?

To test this, I can’t suggest you to keep typing the integers. It may hurt your finger typing 😛

The smart way is to generate this number as an output of the program.

One of the clever examples is to find the factorial of 6000.

There are so many Python interviews; the interviewer asks to find out the factorial of a big number.

If you are coming from other programming languages, you might consider it as a big challenge. In many of the programming languages, finding the factorial of big number crashes your program if the factorial of the number goes out of the integer range limit.

But this is not the case for Python. You can find the factorial of any number as long as your memory is not exhausted. It is near to impossible to exhaust the memory limit unless you are using 128KB RAM 😀

3. float

Implementation of float in Python is similar to the double in C and C++.

Unlike to the long data type, float has max and min value limit.

How to get Max and Min possible value of the float?

You can use sys.float_info to get max and min possible value for float numeric type in Python.

import sys
sys.float_info

Output:

sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

 

4. complex

A complex number is a bundle of the real and imaginary number. Here real and imaginary values are floating type numeric values.

You can pass both integers as well as float value to the real and imaginary part of the complex number.

comp = 10+7j
type(comp)
#<type, 'complex'>

comp = 12.44+7.56j
type(comp)
#<type, 'complex'>

You will not find the use of this variable in general arithmetic operation. It is mostly used in programming for data science and for other scientific purposes.

5. boolean

Above four are the main data type of the numeric variable. There is also one numeric type called boolean.

It accepts the value 0 and 1. If you pass the value 0 or 1, the system can not identify if the variable is an integer or boolean type. It is simply treated as an int, so I will not consider it as the main data type.

int var = 1
type(var) 
#<type, 'int'>

It is nothing but the subtype of an integer.

Now let’s see, some of the frequently asked questions on Numeric Data types…

How does system identify the Python Numeric Data Types of the Variable?

It identifies based on the value and range of the value, you are providing to the variable.

  • Using value passing to the variable

If you are providing integer value without decimal point it is considered as an int value. All the numerical values with a decimal point is a float in Python.

  • Using a range of the value assigned to a variable

Integer and float value has a limit for the range of values assigned to it.

When the range of integer value goes beyond the limit the variable is converted into a long data type. This we will see in detail in long data type.

How to know the Datatype of a Numeric Variable?

You can simply get the type of a variable by using inbuilt type function as below.

count = 17;
type(count)
#<type 'int'>

perc = 17.45;
type(perc)
#<type 'float'>

In your program, you may want to know the data type of the variable. And based on the type of variable, you may want to manipulate and perform the operation. You can refer Declaring and checking type of variable.

How to take the value for Python Numeric Data Types as an input from the user?

In above all the example, we have assigned a value to the variable. We can also ask the user to enter the numeric value and then store in a variable.

Steps followed for reading user input:

  • Taking user input. The input taken from the user is always considered as a string.
  • For taking the numeric value as a user input, we have to convert it into the numeric value.

Here is a complete tutorial about reading user input and saving it as a numeric value.

Wrapping up…

This simple guide to address all the basic questions regarding Python numeric data types. If you have anything to ask, write in a comment. I will reply you right back.

Python Interview Questions eBook

Python
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Comments

  • Reply
    Melinda Jaz
    March 18, 2019 at 8:35 am

    I’m a teacher and always I have an interest in programming. I started learning Python. Thanks for sharing all these tutorials. I am going through them one by one. Anirudh, any tips for new beginners?

    • Reply
      Aniruddha Chaudhari
      March 18, 2019 at 8:39 am

      I am glad to see your interest. Programming is all about practice. The more you practice, the better you become.

  • Reply
    Mayank
    January 9, 2020 at 7:58 pm

    thanks for your tip bro

    • Reply
      Aniruddha Chaudhari
      January 11, 2020 at 9:00 am

      You’re welcome!

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard