data types in Python | Python Data types

By | December 25, 2019

There are several data types in Python like int, float, string, Boolean which are common to other programming languages like C, C++, and Java.

There are other data types in Python which another programming language does not have like lists, dictionaries, etc.

data types in Python

Variables in Python

other programming languages like C++, Java, etc variables are declared by specifying it’s data type first then the variable name.

In python the case is different here, You do not need to specify the data type in Python.

How to declare variable in Python

In order to declare a variable in Python. You have to write only the variable name and python will pick the data type according to values that you want to store in it.

If you specified value inside single or double quotations Python will understand that you defined variable is a string. If you only stored digit in it then Python will understand that you have a variable integer.

Integer data type in Python

In Python integer can be declared and initialized by just assigning values to the variable names.

For example:

value1 = 5
value2 = 6

Sum of two Values

sum = value1 + value2

print(sum)

Plus (+) sign in Python

plus behave in python differently according to the situation that is used in your program.

for example: if a plus sign is used between two integers in Python then plus sign will help in addition of two numbers;

3+5 the Answer will be 8

Plus(+) as a Concatenation

if a plus sign is used between two strings then plus behaves as a concatenation. String Will help in a concatenation of two strings.

e.g “a” + “b” The answer will be ab

Declaring the string variables in Python

value of String variables can either be in double-quotes or in single quotes.

name = “Jony”

or name1 = ‘Jony’

boolean in Python

Boolean can be declared like other variables but In Python boolean variable can be either true or false.

Example:

choice = True

Note:

The Capitalization is important in True False

Float in Python

Float variable in Python declaration in Python is the same as other variables but in a float during initialization, the value is different which is in decimal format.

For example:

variableName = 3.99

Printing Type of any variable in Python

type(a) This function in Python is used to find (print) the data type of variable that you have used in your program.

Conversion of Variables into any type

Below are the functions that help to convert your string and int to any type in python

str()
float()
int()
double()

Leave a Reply

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