Python Variables and Types
When we create a program, we often like to store values so
that it can be used later. We use objects to capture data, which then can be
manipulated by computer to provide information. By now we know that object/
variable is a name which refers to a value.
Based on the data type of a variable, the interpreter
allocates memory and decides what can be stored in the reserved memory.
Therefore, by assigning different data types to variables, you can store integers,
decimals, or characters in these variables.
Every object has:
A. An Identity, - can be known using
id (object)
B. A type – can be checked using type
(object) and
C. A value
Let us study all these in detail
A. Identity of the object: It is the object's address in
memory and does not change once it has been created.
(We would be referring to objects as variable for now)
B. Type (i.e data type): It is a set of values, and the
allowable operations on those values. It can be one of the following:
1. Number
Number data type stores Numerical Values. This data type is
immutable i.e. value of its object cannot be changed (we will talk about this
aspect later). These are of three different types:
a) Integer & Long
b) Float/floating point
c) Complex
Range of an integer in Python can be from -2147483648 to 2147483647,
and long integer has unlimited range subject to available memory.
|
1.1 Integers are the whole numbers consisting of + or – sign
with decimal digits like 100000, -99, 0, 17. While writing a large integer
value, don't use commas to separate digits. Also integers should not have
leading zeros. When we are working with integers, we need not to worry about
the size of integer as a very big integer value is automatically handled by
Python. When we want a value to be treated as very long integer value append L
to the value. Such values are treated as long integers by python.
>>> a = 10
>>> b = 5192L
#example of supplying a very long value to a variable
>>> c= 4298114
>>> type(c) # type (
) is used to check data type of value
<type 'int'>
>>> c = c * 5669
>>> type(c)
<type 'long'>
|
1.2 Floating Point: Numbers with fractions or decimal point
are called floating point numbers.
A floating point number will consist of sign (+,-) sequence
of decimals digits and a dot such as 0.0, -21.9, 0.98333328, 15.2963. These
numbers can also be used to represent a number in engineering/ scientific
notation.
1.3 Complex: Complex number in python is made up of two
floating point values, one each for real and imaginary part. For accessing
different parts of variable (object) x; we will use x.real and x.image.
Imaginary part of the number is represented by 'j' instead of 'i', so 1+0j
denotes zero imaginary part.
Example
>>> x = 1+0j
>>> print(x.real,x.imag)
1.0 0.0
|
2. None
This is special data type with single value. It is used to
signify the absence of value/false in a situation. It is represented by None.
3. Sequence
A sequence is an ordered collection of items, indexed by
positive integers. It is combination of mutable and non-mutable data types.
Three types of sequence data type available in Python are Strings, Lists &
Tuples.
3.1 String: is an ordered sequence of letters/characters.
They are enclosed in single quotes (' ') or double (" "). The quotes are not
part of string. They only tell the computer where the string constant begins
and ends. They can have any character or sign, including space in them. These
are immutable data types. We will learn about immutable data types while
dealing with third aspect of object i.e. value of object.
Example
>>> a = 'Ram’
|
It is possible to change one type of value/ variable to
another type. It is known as type conversion or type casting. The conversion
can be done explicitly (programmer specifies the conversions) or implicitly
(Interpreter automatically converts the data type).
For explicit type casting, we use functions (constructors):
int ()
float ()
str ()
bool ()
Example
>>> a= 12.34
>>> b= int(a)
>>> print b
12
|
3.2 Lists: List is also a sequence of values of any type.
Values in the list are called elements / items. These are mutable and indexed/ordered.
List is enclosed in square brackets.
Example
color =
["Red","Green","Yellow","Blue"]
3.3 Tuples: Tuples are a sequence of values of any type, and
are indexed by integers. They are immutable. Tuples are enclosed in ().
4. Sets
Set is an unordered collection of values, of any type, with
no duplicate entry. Sets are immutable.
Example
s = set ([1,2,34])
5. Mapping
This data type is unordered and mutable. Dictionaries fall
under Mappings
5.1 Dictionaries: Can store any number of python objects.
What they store is a key – value pairs, which are accessed using key.
Dictionary is enclosed in curly brackets.
Example
d = {1:'a',2:'b',3:'c'}
C. Value of Object (variable) – to bind value to a variable, we use assignment operator (=). This is also known as building of a variable.
Example
>>> p = 3.145
|
Here, value on RHS of "=" is assigned to newly created "pi"
variable.
Mutable and Immutable Variables
A mutable variable is one whose value may change in place,
whereas in an immutable variable change of value will not happen in place.
Modifying an immutable variable will rebuild the same variable.
Example
>>>x=5
Will create a value 5 referenced by x
x→ 5
>>>y=x
This statement will make y refer to 5 of x
>>> x=x+y
As x being integer (immutable type) has been rebuild.
In the statement, expression on RHS will result into value
10 and when this is assigned to LHS (x), x will rebuild to 10. So now
x → 10
y → 5
x → 10
y → 5
After learning about what a variable can incorporate, let’s
move on with naming them.
Programmers choose the names of the variable that are
meaningful. A variable name:
1. Can be of any size
2. Have allowed characters, which are a-z, A-Z, 0-9 and
underscore ( _ )
3. should begin with an alphabet or underscore
4. should not be a keyword
It is a good practice to follow these identifier naming
conventions:
1. Variable name should be meaningful and short
2. Generally, they are written in lower case letters
Assigning Values to variable
Python variables do not need explicit declaration to reserve
memory space. The declaration happens automatically when you assign a value to
a variable. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the
variable and the operand to the right of the = operator is the value stored in
the variable. For example:
train_no = 'T#1234' #String type variable
balance = 2345.12 #float
type variable
rollNo = 105 #integer type variable
|
Multiple Assignment
Python allows you to assign a single value to several
variables simultaneously. For example:
a = b = c = 10 #assigning same value to multiple
variables
Here, an integer object
is created with the value 1, and all three variables are assigned to the same
memory location. You can also assign multiple objects to multiple variables.
For example:
x, y, z = 10, 20, 30 #assigning
multiple values to multiple variables
Here, two integer objects with values 1 and 2 are
assigned to variables a and b respectively, and one string object with the
value "john" is assigned to the variable c.
While assigning values through multiple assignments,
please remember that Python first evaluates the RHS (right hand side)
expression(s) and then assigns them to LHS. For example:
a, b, c =
6, 7, 8 #statement
1
b, c, a =
a+2, b+1, c+2 #statement 2
print(a,b,c)
Statement1 assigns 6, 7 and 8 to a, b and c respectively
6+2,
7+1, 8+2 = 8, 8, 10
Then it will make the statement as:
b, c,
a = 8, 8, 10
Thus, b = 8, c = 8, a = 10
The third statement print(a,b,c) will print
10 8
8
Important: In Python, a variable is created when you first assign a
value to it. Using an undefined variable in an expression/statement causes an
error called Name Error.
|
No comments: