Ticker

6/recent/ticker-posts

Python tutorials part 2 - Variables and Datatypes

 



    Python , a high level programming language has always been popular for its simplicity and readability . We have just familiarized with python features in from our previous post . Here let us see about the variables and data types in python.

A variable is simply a memory location where you store a value , which may or may not be changed later. We have mentioned earlier that in python you do not need to mention the datatype while declaration , instead simply assign values to it . Variables in python are usually case sensitive .Upper and lower case variable differ and get different memory locations. For eg "name"  is not the same as "Name" . Adding to this there is no separate variable declaration and initialization . You just assign a value to it.

Speaking about datatypes , there are prominently 6 datatypes in python , which are numbers , strings , list , tuples , set , and dictionary . A type() function return the datatype of a variable .

So inside numbers you have integers , float , complex and boolean . 

eg x = 5 - int datatype , x = 3.57 - float , x = 2j - complex , x = 0 or 1 - bool . The bool value return if a statement is True(1) or False(0) .

You may note that python involves automatic type conversions( coercion ). eg - when you add an integer to float , the result is a floating number .

Check the below python statement for printing a line  :

x = 5

print( " The value of x is : ", x )

output  -  The value of x is : 5  


Let a = " Python" , it is of string datatype ,  

 The len() function returns you the length of the string , in this case , len (a)  = 6 . You can iterate through a string . Each letter of a string is given index . Indexing begin from 0 to len(string) - 1 . So , a[0] returns you P , and similarly , a[1] ,a[2] ,a[3], a[4] ,a[5] should give you Y ,T ,H, O ,N respectively . Also note that strings are immutable  , ie you cannot assign a different value for any index value in a string .

Another operation that can be performed on string is slicing ,you can extract a portion of string specifying indexes ,  for eg -   a[3: ]  return "HON" ,   a[3:5]  returns "HO"  ,   a[0: ] -gives "PYTHON" .    You can also access the values from the end.  For eg -  a[ -1] gives "N"

There are some interesting function that allows you to changes cases and also check them .

- string.upper() converts the string to all uppercase and string.lower() converts them to lowercase .

- string.isupper() checks if the string elements are all uppercase and string.islower() for lowercase .


Another important datatype is list . There are similar to strings , but contains a list of strings , numbers, or any other datatypes  in which each item is given a index ranging from 0 .  Lists are mutable . That is you can update values or items in a string . 

Along with which you can also add items to a list (using list_name . append('item')  function at the end and to anywhere using list_name . insert(index_number) function ) and reverse it using Iist_name.reverse(function) .

list = [ 'a' , 2 , 3 , 'name' ]

print(list[2]) 

list.append(5) 

print(list)  

list.insert( 3 , 'b' )     # index and value

print(list)  


Output

>> 3

>> ['a' , 2, 3, 'name' , 5]

>> ['a' , 2, 3, 'b' ,'name' , 5]


Then comes our next datatype named dictionaries . They are collection similar to list . They contain items as key : value pairs enclosed by curly brackets . You can access these using indexes . These are also mutable and you can do this using key names . 

data = {   name : xyz , age : 10 , number : 70 , category : 'a'   }

data [name] = 'abc'

print(data) 

data [class] = 'five'

print(data)  


Output

>>  {name :'abc' , age : 10 , number : 70 , category : 'a'   }

>> {name :'abc' , age : 10 , number : 70 , category : 'a'  , class : 'five' }


Next we have tuples . The are usually ordered and immutable .Duplicate values may be present . Using count function can give you the number of times a particular values is repeated in a tuple. The items in a tuple are enclosed within round brackets .

For eg . let fruits = ( 'apple ' , 'mango' , 5 , 10 , 5 )

fruits[4] = 5

fruits[2] = 5


Finally we are left with sets . Sets are meant for collection similar to tuples and dictionaries , but unlike tuples they do not maintain order . Duplicate values are not allowed and they are not indexed . 

Let  , set = ( 1 , 2 , 'ab ' , 4 , 'cd' )

set[2]  gives you an error .


So now you may have got understanding on variable declaration and datatypes in python . We will be back with lot of other concepts in python later .

Happy Reading !!


Post a Comment

0 Comments