Python variables and types

Variables

In real world programs have to deal with real time data and in order to work with data it is required to have variables that can help in storing values in memory locations. As in case of any other programming language, python too cannot do without variables.

Variables are names given to memory locations. Values stored in the variables can be altered within the program. Since variables store values in memory locations, each variable is associated with a physical address. Variables are used to in carrying forward transactions.

Variables are assigned a meaningful name in order to keep programming simple as it can be very difficult to code with physical addresses.

Variables by reference

In Python there is no need to define a data type but it is important to assign a value to it for e.g. var1 = 20 defines a variable by the name var1 having a value of 20. If we now create a new variable by the name var2 as follows:
var2 = var1;
Using the above statement, both var2 and var1 will refer to the same object in memory and no new object will be created for it.

The type of variable will change if you change the type of value assigned to it. The following figure explains how.

# type 
var1 = "123"
print type(var1)
var1 = 123
print type(var1)

Download the code Run the code
In the above example , first a string value “123” is assigned to var1. Hence, var1 becomes a string variable and when you check it with type(var1), it displays . However, if you now assign an integer value 123 to var1, and again check the type of var1, it will display .

Data Types in Python

Python has five standard types:

Numbers

Python supports 4 numerical type:
int     (e.g. 10, -10, 56, -89 )
long  (e.g. 1456L, 0134L), you can use a smaller case L with long numbers but it is advisable to use upper case to avoid confusion.
float  (e..g. 1.01, 5.6, 8.76)
complex (e.g. 8.6e-5.6j, 2e+5.7j)

String

A string contains characters in single or double quotes. Triple quotes are used to store string values with multiple lines.

List

Lists allow you to hold several objects in an order. All values inside the list are written in square brackets and is separated by a comma.

For example:

lst1=[1,2,3,4]
bird_list=['sparrow', 'crow', 'duck','dodo']

Tuple

Tuples are somewhat similar to Lists but tuples cannot be changed and instead of square brackets we use parentheses (), to define a tuple.

tup1=[1,2,3,4]
bird_tup1=['sparrow', 'crow', 'duck','dodo']

Dictionary

Dictionary has objects defined in curly brackets { }. Each Key is separated by a colon ‘:’ and each object is separated by a comma as shown:

dict1 = {'Bird': 'Crow', 'Color': 'Black' , 'size': 'Medium'}

Conversion between Data Types:

You can perform conversion between the built-in data types. Here are some of the functions that can perform conversion

Conversion of string to integer:

str1 = “123”
int (str1)

Conversion of integer to long or float

long (str1)

float (str1) converts to float

complex (a,b) creates a complex number

repr (x) converts to expression string

eval (str): evaluate a string and returns an object

#Conversion of integer to float
print('Conversion of an integer to Long')
int1= 10
print('value of integer is: '+str(int1))
print('now convert using long() function')
flt1 = float(int1)
print('the converted value is now : '+str(flt1))

Download the code Run the code

#Conversion of string to integer
str1 = '123'
print('type of str1 is : ')
print(type(str1))
x=int(str1)
print('type of str1 after conversion is:')
print (type(x))

Download the code Run the code

<<< Python TutorialPython Arithmetic Operators >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .