Data types, Variables and Type casting in python


Data Types

Data types are the classification of data which bifurcate the type data for processing, before giving any command to obtain the desired result from the computer, one must have to understand the type of the data you’re dealing with. Following are the common types of data,

 

Text:

String: it is being used to present the one or more collection of characters put in single quote or inverted commas. Syntax Str()

 

print(“Hello Digital Drive”)


Hello Digital Drive is a string.


print(type(“Hello Digital Drive”)) #type() function output the value of the variable


Class ‘str’


Numeric:

Integer: It represents the negative or positive whole numeric values. Syntax Int()


print(10)


10 is an integer.


print(type(10)) #type() function output the value of the variable


Class ‘int’


Float: It represents the fractional numeric values. Syntax float()

 

print(10.68)


10.68 is a float.


print(type(10.68)) #type() function output the value of the variable


Class ‘float’

 

Complex: It represents the real and imaginary numeric values. Syntax complex()

 

print(10+68j)


(10+68j) is a complex number.


print(type(10+68j)) #type() function output the value of the variable


Class ‘complex’


Sequence:

List: A data presented in one or more ordered data in square brackets. Syntax list([])


print(list[1,2,3,4,5]))


[1,2,3,4,5] is list.


print(type([1,2,3,4,5])) #type() function output the value of the variable


Class ‘list’


Tuple:  A data presented in one or more ordered data in parenthesis. Syntax tuple(())

 

print(list(1,2,3,4,5))


(1,2,3,4,5) is tuple.


print(type((1,2,3,4,5))) #type() function output the value of the variable


Class ‘tuple’

 

Range: It gives us the sequence of numbers between the start number to end number. Syntax Range()

 

print(list(range(10)))


(0,1,2,3,4,5,6,7,8,9) is range.


print(type(0,1,2,3,4,5,6,7,8,9)) #type() function output the value of the variable


Class ‘list’

 

Dictionary: It represents to mapping or defining of unordered data or collection of data. Syntax dict{}


print(dict({1:”Hello”,2:”Digital”,3:”Drive”}))


{1:’Hello’, 2:’Digital’,3:’Drive’} is dictionary.


print(type{1:’Hello’,2:’Digital’,3:’Drive’}) #type() function output the value of the variable


Class ‘dict’


Variables:

A variable is a memory slot reserved to store a value in shape of data. Data can be in the shape of numbers, texts, list, tuple, range, etc.

 

Numbers:

As already discussed in data types, numbers are bifurcate in integer, float and complex types. So the variables are also declared in their exact data type. For example.

Integer Variable:

X=10

print(x)


10 x is variable of integer 10.


Float Variable:

Y=10.68

print(Y)

print(float(10))


10.68 Y is variable of float 10.68.

10.00


Print(x+y)

 

20.68

 

The same goes on with all other data types as well.

 

Strings:

X= “Hello Digital Drive”

Print(x)

 

Hello Digital Drive

 

X= “Hello Digital Drive”

Y= “I Hope everyone is doing well”

Z= ”This is our second exercise in python”

Print(x+y+z)

 

Hello Digital Drive I Hope everyone is doing well This is our second exercise in python

 

Type Casting:

There comes a time when you want to covert the numeric values to string and strings to numeric to obtain some desired results. The process of converting numeric data into string or float or string to integer or float is called type casting. E.g.

 

Integer:

X=int(10)

Y=int(10.68)

Z=int(“10”)

 

Print (x)

Print (y)

Print (z)

 

10

10

10

 

Float:

X= float(10)

Y= float (10.68)

Z= float (“10”)

 

Print (x)

Print (y)

Print (z)

 

10.00

10.68

10.00


String:

X= str(10)

Y= str (10.68)

Z= str (“10”)

 

Print (x)

Print (y)

Print (z)

 

‘10’

‘10.68’

‘10’

Comments