Thursday, 4 April 2024

Python Data Types

 

Data Types


Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

There are various data types in Python. Start with Numbers


Python Numbers

Integers, floating point numbers and complex numbers fall under Python numbers category. They are defined as int, float and complex classes in Python.

We can use the type() function to know which class a variable or a value belongs to. Similarly, the isinstance() function is used to check if an object belongs to a particular class.


Program:

a = 5

print(a, "is of type", type(a))

a = 2.0

print(a, "is of type", type(a))


a = 1+2j

print(a, "is complex number?", isinstance(1+2j,complex))


# Scripts Ends 

Output:

5 is of type <class 'int'>

2.0 is of type <class 'float'>

(1+2j) is complex number? True


integers can be of any length, it is only limited by the memory available.

A floating-point number is accurate up to 15 decimal places. Integer and floating points are separated by decimal points. 1 is an integer, 1.0 is a floating-point number.

Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part. Here are some examples.


Program:

a = 1234567890123456789

b = 0.1234567890123456789

c = 1+2j

print(a)

print(b)

print(c)

Output:

1234567890123456789

0.12345678901234568

(1+2j)


No comments:

Post a Comment

Interactive Report: Introduction to the Internet of Things (IoT) ...

Popular Posts