Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

Variables in PYTHON

Variables in Python

Variables

Variables are used for storing data in the form of integer,char ,boolean etc.They act as a storage medium for us . Based on the datatype of variable  interpreter allocates the memory required by that datatype. There are 4 types of variables.

  • Integer
  • String
  • float
  • complex

Values are assigned to variables by using (=) sign..There is no need to write keyword when initializing a variable as interpreter itself asisign it according to the value given .

Integer

Integer is used for storing integer data . The keyword for integer variables is int .

a=10

The range of integer is (2\^32) and get promoted to long  when  a number greater than (2\^32)

String

Strings are used for storing character literals .When we want to save some names of person or for any character related data we use strings.

a="simer"

We can apply various operations on strings . To print letters of string we can give the index of the letter and print it .Index of a string always start from 0.

a="simer"
print a[0]

Screenshot-5

0 is the index of the 1 letter of the string ,similarly 1 idex will be of 2 letter and so on.

We can also give the range of letters which we want to print of the given string .These can be given as :

a="simer"
print a[0:]   # to print all the letter from index 0
print a[1:3]  # to print letter starting from index 1 to index 3
print a[:4]   # to print first 4 letter of string

Screenshot-6

Float

float variables are used to store floating point values . They consist of two parts mantissa and exponent.

eg 2*10^-1  ^

Here 2 is the mantissa , -1 is the exponent and 10 is the base of the exponent.

a=2.5

Complex

Complex numbers are represented by a+bj where a is the real part of number and b is the imaginary part of the number .They are used to repreent real floating point numbers.

a=5+10i
print a

Multiple Assignnmet

We can assign multiple variables at the same  time to same value. This will save time by not initializing the variable every time.

a=b=c=20

Conversions

We can convert datatype of a variable to another datatype .

Integer to float or vice versa

We can convert a floating point number to an  integer or a integer to floating point  by using int and float  keywords  with that variable.

a=123.5
print a
a=int(a)
print a

Screenshot-8

a=123
print a
a=float(a)
print a

Screenshot-7

integer to string or vice versa

We can convert integer to sting and strings to integers by using int and str keywords .Numeric strings can be converted to integers only,alphabetic strings will not be converted.

a="1234"
print a
a=int(a)
print a+2

Screenshot-5

a=1234
print a
a=str(a)
print a,a+2 #a+2 to check that is been converted to string

Screenshot-6

Complex to string or vice versa

Complex can only be converted to string datatype and only numeric string can be converted to complex datat type ,keyword used for complex is complex.

a="123"
a=complex(a)
print a

Screenshot-6

a=5+8j
a=str(a)
print a

Screenshot-5

Any queries related to this tutorial can be asked and in next tutorial we will discuss about conditional statements in python.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy