How to use Data Type in Python

Posted on
How to use Data Type in Python

Python Tutorial

How to use Data Type in Python – A Data Type is a data classification that tells the interpreter how the programmer will use the data. Data types in Python are as follows:

DATA TYPES FUNCTIONNOTEEXAMPLE
intIntegerDeclare an integer10 or 1024
floatFloatDeclare a number that has a comma2.11 or 56.90
strStringDeclare characters or sentences enclosed by ” or ‘ signs“Hello Python, let’s learn Python programming”
‘Hello Python, let’s learn Python programming’
boolBooleanDeclare True if it is true and the value is 1
Declare False if it is false and has a value of 0
True or False
hexHexadecimalExpresses a hexadecimal number (base 16)8f3 or a1
complexComplexExpressing pairs of real and imaginary numbers3 + 3x
listListDeclare various data types in an array and their contents can be changed[‘john’, 18, 71.2]
tupleTuple Declare various data types in an array and their contents cannot be changed(‘john’, 18, 71.2)
dictDictionaryDeclare various data types in an array that have labels along with their values and their contents can be changed{‘name’:’john’, ‘age’:18, ‘height’:71.2}

How to view Python data types:
If you don’t know what a variable stores data for, to find out you can simply use the type(variable name) command. Then the data type of the variable will appear.

1. How to use Integer

To be able to use Integers in Python, the command form is like this:

x = 10
print(x)
type(x)

The result is:

10
int

2. How to use Float

This is the same as integers. The command is as follows:

x = 10.2
print(x)
type(x)

The result is:

10.2
float

3. How to use String

To use strings in python, characters must be enclosed in either two quotation marks (” “) or one quotation mark (‘ ‘).

For example:

x = 'Hello Python'
y = "Hello Python"

print(x)
print(y)

The result is:

 Hello Python
 Hello Python

In addition to writing strings, python can also perform string manipulation. We created a separate page to provide a good reading experience, see here.

4. How to Use Boolean

Boolean will return only True or False expression of a condition.

Example:

print('Python' == 'python')
print('Python' == 'Python')
print('Python' == "Python")

print(20 == 20)
print(10 > 20)
print(10 < 20)

The result is:

False
True
True

True
False
True

5. How to use Hexadecimal

To use hexadecimal, the basic function used is hex(). That will return the value to be hexadecimal. But it’s different if the input is integer, float or ascii.

a. Integer using hex()

Example:

x = 20
print(hex(x))

The result is:

0x14

b. Float using float.hex()

If you want to see the hexadecimal data of a float, then the function is float.hex()

Example:

x = 20.2
print(float.hex(x))

The result is:

0x1.4333333333333p+4

c. ASCII using hex(ord(‘ascii val’))

If you want to see the hexadecimal value of an ASCII value, the function used is hex(ord('ascii val'))

Example:

x = 'a'
print(hex(ord(x)))

y = 'b'
print(hex(ord(y)))

The result is:

0x61
0x62

Hopefully this article is useful.