Skip to main content

Python Datatypes and Variables

 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"
     print a,b,c,d
                                           
This code gives the output - 5 32.5 hello my name is venky

Program and output

                                                   
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)
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.

                     >> Next : Python Functions and modules >>


Comments

Popular posts from this blog

Events Handling

Events in a game are the different things that happen during a game. Games are controlled by the user inputs either through mouse or keyboard. Whenever the user gives an input in some way to the game, the game has to react correspondingly according to what the user expects. Handling these events to produce some output is known as Event Handling. Suppose the user opens a game. The user has option to quit the game any time they want.There are keyboard button controls for playing the game as well as mouse clicks and motions. Every event is tracked by the pygame and it is our duty to just instruct what to do if that event occurs. There are many types of events in pygame. Some of them are : QUIT ACTIVEEVENT KEYDOWN KEYUP MOUSEMOTION MOUSEBUTTONUP MOUSEBUTTONDOWN VIDEORESIZE Let me write a code to show how the events are identified in  pygame. import pygame pygame.init() display=pygame.display.set_mode((500,500)) game_loop=False while not game_loop: ...

Moving Objects

Hi everyone ! In this post, let us see how to move objects on the game screen. In the previous post, we learnt how to draw objects and fill colors. Now we will see how to move those objects from one place to another. To move objects in a game, we need some event to occur. Let us make the object move on pressing the arrow buttons. import pygame pygame.init() white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) display=pygame.display.set_mode((500,500)) game_loop=False rect_x = 100 rect_y = 100 while not game_loop:     for event in pygame.event.get():         if event.type == pygame.QUIT:             game_loop = True         if event.type == pygame.KEYDOWN :             if event.key == pygame.K_LEFT :                 rect_x -= 10             if event.key...

Python functions and modules

Functions are a block of code that performs the task specified. They either just perform some computation and display the result or returns the result to the main function. In python, functions are defined with the keyword 'def'. The syntax for defining a function in python is : def function_name :       #Block of statements The keyword def is followed by the name of the function. It is a good practise to name the functions meaningfully representing the task done by it. The function name is followed by a colon (:). In python, Comments are represented by the symbol #. So here #Block of statements is commented and hence it will not be executed by the compiler. # is used to comment one line of code. ''' .......''' is used to comment a block of code. For example, #this is a commented line of code. ''' this is a block of commented code Everything in this code will not be looked into by the compiler This is helpful in comment...