• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Python Numbers – Integers, Floating-Point, and Complex Numbers

July 21, 2020 by javainterviewpoint Leave a Comment

Python Numbers can be classified into three types Integers, Floating-point, and Complex numbers, and there is no explicit type declaration needed in Python. The numbers which we type in will be automatically interpreted as a number.

Python Numbers

  1. Integer Numbers
  2. Floating-Point Numbers
  3. Complex Numbers

Python Numbers

1. Integer Numbers

Like any other programming language, Python Integer Numbers are whole numbers that can be positive, negative, or zero (..-2, -1, 0, 1, 2,…). In Python Integer is also known as int

A simple declaration of an integer looks like this

>>> a = 10
>>> b = -15
>>> type(a)
<class 'int'>
>>> type(b)
<class 'int'>

We can see the data type of a variable using the type() function, since we are storing an integer [Without decimal points] into the variable ‘a’ and ‘b’, the type of ‘a’ and ‘b’ is assumed by the Python interpreter as ‘int’

Note: In Python, any sequence of digits without a functional part can be assumed as Integers. For example, we can enter an integer literal like below

>>> 7
7

We can also simply print a plain zero(0)

>>> 0
0

But what will happen if we are using zero in front of an integer?

>>> 07
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers

We will be getting “SyntaxError: leading zeros in decimal integer literals are not permitted”. Though in mathematics world ’07’ is the same as ‘7’, in Python it is not permitted for an integer literal.

In Python, we can also refer int type with different bases,

  • Binary numbers (Base 2) have only 0 and 1 numbers.
  • Octal numbers (Base 8) uses the numbers from 0 to 7.
  • Decimal numbers (Base 10) will have numbers from 0 to 9. This the standard number system which we use.
  • Hexadecimal numbers (Base 16) contains numbers from 0 to 9 and letters A through F.

To inform Python interpreter to use a different base other than base 10, we need to add a zero and a special character to the number. For Example, 0x11 tells the Python to treat the number as a hexadecimal number.

Below are the characters which we need to use

  • ‘0b’or ‘0B’ for Binary Numbers (Base 2)
  • ‘0o’ or ‘0O’ for Octal Numbers (Base 8)
  • ‘0x’ or ‘0X’ for Hexadecimal Numbers (Base 16)

We can also convert the numbers from one base to another base using bin(), oct(), and hex() functions. Let’s put all together and try converting into different bases.

Let’s convert the number 10 to different bases, by calling the bin(), oct(), and hex() functions

>>> bin(10)
'0b1010'
>>> oct(10)
'0o12'
>>> hex(10)
'0xa'

2. Floating-Point Numbers

Floating-Point Numbers can be easily identified by their decimal points or they have an exponential (e) to define the number, they can be positive, negative, or zero (..-2.0, -1.0, 0.0, 1.0, 2.0,…). In Python Floating-Point numbers are also known as float

For Example, 3.0 and -4.1 are examples of floating-point numbers. 5E3 (5 times 10 to the power of 3) is also an example of a floating-point number in Python.

>>> c = 10.0
>>> d = -15.0
>>> e = 0.0
>>> type(c)
<class 'float'>
>>> type(d)
<class 'float'>
>>> type(e)
<class 'float'>

3. Complex Numbers

A Complex number is a number which consists of real and imaginary part paired together. Complex numbers are typically using in Engineering like hydrodynamics, alternating voltages, etc.. Python is one of the programming languages which supports complex numbers by default.

For Example, 5 + 2j is a complex number, where 5 is the real part and 4 is the imaginary part. The imaginary part always contains a j in it.

We can simply assign a complex number to a variable in Python directly, like below

>>> num = 5 + 2j

We can get the real of the complex number, by using the num.real [variable.real]. Likewise, if we want to get the imaginary part, we need to use num.imag[variable.image]

>>> num.real
5.0
>>> num.imag
2.0

Type Conversion

Whenever we are adding ‘int’ and ‘int’, the result will be an ‘int’, whereas if we are adding ‘int’ and ‘float’, then the result will be in ‘float’, because of Python automatically coverts ‘int’ into ‘float’ before addition.

>>> a = 10
>>> b = 20
>>> c = a + b
>>> c
30
>>> type(c)
<class 'int'>
>>> d = 10
>>> e = 20.1
>>> f = d + e
>>> f
30.1
>>> type(f)
<class 'float'>

As we saw, Python automatically converts the data type (int to float) whenever needed,  we can also convert the datatype manually by ourself.

We can explicitly convert int to float by using the int() function and convert float to int by using the float() function.

>>> int(10.1)
10
>>> int(-10.7)
-10
>>> int (-10.2)
-10
>>> float (10)
10.0

Note: The int() function truncates the decimal points and does not perform round off

Basic Arithmetic operations

Operator Operation
+ Addition
– Subtraction
* Multiplication
/ Float Division
// Integer Division
% Modulus
** Exponentiation

Let’s perform some basic arithmetic operations on python numbers.

>>> 3.1 + 2.9
6.0
>>> 4 * 2
8
>>> 3 / 2
1.5
>>> 3 // 2
1

Did you notice it?

3 // 2 gives us the result of 1 and NOT 1.5

The ‘/’ operator performs the floating-point division even though both the numerator and denominator are int, but the ‘//’ operator is a tricky little operator when the result is positive it truncates the decimal part.

But, when the result is negative, it performs the round off functionality

>>> -7 // 2
-4

When we divided -7 by 2 [-7 // 2], instead of giving -3.5 as a result, it gave -4 as it has performed round off on the result.

What if we want the remainder of a division? then we can use the Modulo operator %

>>> 4 % 2
0
>>> 5 % 2
1
>>> -10 % 3
2

The last operator which we will be looking is ** operator [exponentiation or power operator]

>>> 4 ** 2
16
>>> 2 ** 3
8
>>> -3 ** 2
-9

This operator performs the “raised to the power of” operation, so 4 ** 2 will perform 42

Happy Learning!!

Filed Under: Python Tagged With: Complex, float, Floating Point, int, Integers, Python Numbers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·