- Variables & Datatypes
- Basic program
Variables & Datatypes
Variables are basic building blocks of a programming language and it applies to python programs also. Variables hold data in memory. Python variables differ by a slight than the other programming languages. In python, variables can be declared without specifying the data type and the data type will be determined by the values assigned to the variable. for example in c++, declaring a variable “a” with the value assigned to it will be like the code below.
int a = 10;
But in python it is declared like
a = 10 #Assign the values 10 to variable a
Note : in python “;” is not need to end the statement
Let see more examples
x = 5 # assign variable x the value 5 y = x + 10 # assign variable y the value of x plus 10 z = y # assign variable z the value of y
As with other programming languages, variables in python is case-sensitive and can include letter, number, ( _ ) char but the variable name cannot start with a number.
Let us see how python assigns the datatype based on the values by an example program.
x = 1 print(type(x)) # outputs: <class 'int'> x = 1.0 print(type(x)) # outputs: <class 'float'>
if we see the first line
we can see that it is a integer value and the output is displayed as
if we see the second variable declaration and ask it to print its type
we can see that it is a decimal number and its type is float
This applies to other data types too because python is a dynamically typed programming language and it assigns a data type to its variables based on the type of values assigned to it.
Let us see a basic program about declaring variables and datatypes.
x=10 y=x+12 print(y) print("Data type of variable x and y" + str(type(x)) + str(type(y))) d=1.2 print(d) print("Data type of variable d is " + str(type(d))) s = 'Arvin Education' print(s) print("Data type of variable s is " + str(type(s)))
The output is as follows
22 Data type of variable x and y<class 'int'><class 'int'> 1.2 Data type of variable d is <class 'float'> Arvin Education Data type of variable s is <class 'str'>
That is all for this post. In the next post we will see control statements in python.
Please see https://machinelearningtamil.blogspot.com/ for தமிழ் Version of this post.