Data Type, Numbers, and Casting in Python

Data Type, Numbers, and Casting in Python.

Posted on

At this Point We all know the basic of python, what is python indentation, what is a comment in Python, and at previous we know what is variable in python, if you don’t reach then there is a link for all python articles https://smgplaza.com/category/python/.In This Article we are Studying about data type in python, numbers in python, type conversion and also typecasting these all topics are covered in this Articles.

Python Data Type

Built-in data type

The data type is the most important. variables can store data of different types and values, In python Data type built-in by default.

Below, There are some python In-built data types

Text Typesstr
Numerical Typesint, complex, float
Sequence Typeslist, tuple, range
Mapping Typesdict(dictnory)
Set Typesset, frozenset
Boolean Typesbool
Binary Typesbytes, byte array

How To get the data type of Declared Variable?

if you want to get the data type of any variable simply just use type() function like below.

x = "SMGPLAZA"
print(type(x))

You got Output like below…

<class 'str'>

How to declare or assign a value to a variable ?

for this we are doing one program

S = 76                                     #int (numerial type)
print(type(x))

S = 16.4                                   #float(numerial type)

S = 2m                                     #complex(numerial type)

M = "SMGplaza"                             #str (text type)

G = ["SMGPLAZA","JAY","MANAV"]             #list(sequence type) 

G = ("SMGPLAZA","JAY","MANAV")             #tuple(sequence type)

G = range(100)                             #range(sequence type)

P = {"name" : "XYZ", "class" : "abc"}      #dict(mapping type)

L = {"SMGPLAZA","JAY","MANAV"}             #set(set type)

L = frozenset({"SMGPLAZA","JAY","MANAV"})  #frozenset(set type)

A = true                                   #bool(boolean type)

z = J"SMGPLAZA"                            #bytes(binary type) 

A = bytearray(100)                         #bytearray(binary type)

If you want to give a specific type then you can do like this

J = str("smgplaza")

Python Numbers …

there are three type of numbers in python programming int, float, complex example are given below

x = 100
y = 10.4
z = 1j
a = -1
b = -30.6
c = -6+3j
print(type(x))
print(type(y))
print(type(z)) 
print(type(a))
print(type(b))
print(type(c)) 

Output Of Above Code For know the Type Of Variable

<class 'int'>                                                                                                                                 
<class 'float'>                                                                                                                               
<class 'complex'>                                                                                                                             
<class 'int'>                                                                                                                                 
<class 'float'>                                                                                                                               
<class 'complex'> 

Type Conversion

Convert one type to another type use of int() , float() , cmplex(), Example Are Given Below.

x = 100
y = 10.4
z = 4m
#convert int to float
M = float(x)

#convert float into int  
N = int(y)    

#convert int to complex  
O = complex(x)     
  
print(M)
print(N)
print(O)

Output Of Above Code

100.0                                                                                                                                         
10                                                                                                                                            
(100+0j)

Random Number

python doesn’t have any random() function for make a random number but it has a in bulit module for a random umber it’s called random.

For this first import random and then assign value for it

import random
print(random.randrange(1,100))

Every Time Output Id Different That Produced Random No Between 1 to 100, Output Like Below.

69

Python Casting

There are Many times where you have To declare a data type of specific variable This Can Be Possible With Python Casting, Like Below.

x = int(10)   # x will be 10
y = int(2.80) # y will be 2
z = int("3") # z will be 3

print(x)
print(y)
print(z)

here we are convert or declare a variable as a int output of above code is like below.

10                                                                                                                                            
2                                                                                                                                             
3 

Here we are declaring a variable as a string and float code is below

x = float(1)     # x will be 1.0
w = float("4.2") # w will be 4.2
z = str(3.0)  # z will be '3.0'

print(x)
print(w)
print(z)

Output OF above Code Is

1.0                                                                                                                                           
4.2                                                                                                                                           
3.0

Also visit this articles :-

For All python article, you have to follow this link, https://smgplaza.com/category/python/

If you have any doubts any suggestions or any query DM us one below Social media handles.

Our Social Media Handles:-

Instagram Handle :- https://www.instagram.com/smgplaza/

Twitter Handle :- https://twitter.com/smgplaza

We the team of smgplaza are glad to receive your all valuable support. For more details and updates you can follow us on Twitter, Gmail Facebook, and Instagram. We are trying to serve you the best of us. If you face any doubts you can comment it in the given below comment box

Leave a Reply

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