Variables in PYTHON

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 .

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.

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.

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 :

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.

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.

 

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.

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.

 

Screenshot-8

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.

Screenshot-5

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.

Screenshot-6

Screenshot-5

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

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *