Hello everyone ! In this post, we are going to go through some of the data types and variables in python. It is easy to work with variables in python compared to java, c or c++.
Python is intelligent enough to accept any kind of data assigned to a variable.In C or C++, we need to declare a variable with a datatype of the data that the variable is going to hold. In python, there is no need of declaring the variable before defining it. We can simply assign any value to a variable.
print 5 gives the output as 5.
print 1.2 gives the output as 1.2.
Python directly understands 5 as a value of type integer and 1.2 as a value of type float.
So write the following code in the python window and save it as say 'types.py'
a = 5
b = 32.5
c="hello"
d="my name is venky"
c="hello"
d="my name is venky"
print a,b,c,d
So we see that it is very easy to work with variables in python.
Type Conversion in Python :
It is easy and convenient to convert data types of variables in python. Variable assigned with data of one type can be easily converted into a data of another type.It is easy to even view the type of data in python.To know what is the data type of a particular variable,we must use type keyword.
a=5
b=10.5
c=float(a)
d=int(b)
d=int(b)
print a,b
print c,d
Output :
5 10.5
5.0 10
To find the type of any data, just use type keyword.
For example,
a=5
b=10.8
print(type(a))
print(type(b))
This code will print the datatypes of the values stored in variables a and b. Obviously, as we know, variable a is of type integer and b is of type float.The program displays it. Here there is no use of using this keyword to find the datatype. But with complex types like list and dictionary in python, it will be useful to check the program to find what datatype does the program return.
When we run this program, the output is displayed as,
<type 'int'>
<type 'float'>
Comparing variables in python :
Python is so flexible and easy to the users.It is easy to compare the value of two variables in python.
Suppose if we need to compare two strings, in C or C++, it is a hectic job for us to assign each string to an array. Then we need to compare every element in the array to check if the both the strings are equal character wise. In python , we need not worry about assigning the string to any array. As we saw, we simply assign a string to a variable.
For example,
a="hello"
b="bye"
c="hello"
if a==b:
print "Strings a and b are equal"
else:
print "Strings a and b are not equal"
if a==c:
print "Strings a and c are equal"
else:
print "Strings a and c are not equal"
This program is to check the equality of strings.Unlike C or C++ where we compare each and every character by putting the string in an array,we just use Equality(==)operator to check two strings in python. This program shows that the variable a and c contains the same string and b contains a different string. So the output should be displayed like :
Strings a and b are not equal
Strings a and c are equal
I think this is enough as an introduction to data types and variables in python. Try working in python whatever you learn here on your desktop machine. Practice makes you familiar to the syntax of python.
Thank you.
Comments
Post a Comment