Search This Blog

Thursday, March 5, 2015

Variables and Data Types


             Reference
Name ----------------> Deepti
Variable                     Object


  • "Name", which is a variable acts as a reference to "Deepti" which is an object
  • It is the object that has the data type associated with it,


>>> id(name)
42209536
>>> hex(id(name))
'0x2841100'
>>> name.__repr__
<method-wrapper '__repr__' of str object at 0x02841100>
>>> 


>>> name = "Deepti"
>>> name = 'deepti'
>>> name = "Deepti\nvaidyula"
>>> name
'Deepti\nvaidyula'



  • # The 'r' here tells that it is a raw string and don't interpret the \n

>>> name = r"Deepti\nvaidyula" 
>>> name
'Deepti\\nvaidyula'

>>> print name
Deepti\nvaidyula
>>> name = "Deepti\nvaidyula"
>>> print name
Deepti
vaidyula


  • # Python also supports unicode

>>> name = u"Deepti"
>>> name

u'Deepti'

# Convert unicode to String - use str
>>> str(name)
'Deepti'
>>> name
u'Deepti'


  • Use 'unicode' to convert to unicode




  • We cannot directly change string objects in memory as they are immutable

>>> name = "Daniel"
>>> name[0]
'D'
>>> name[0] = 'a'

Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    name[0] = 'a'

TypeError: 'str' object does not support item assignment
>>> name = "Radcliffe"
>>> name

'Radcliffe'


>>> a="Daniel"
>>> name = a
>>> name
'Daniel'
>>> a

'Daniel'



>>> a="Radcliffe"
>>> a
'Radcliffe'
>>> name
'Daniel'

String Concatenation

fn = "deepti"
ln = "Vaidyula"

full_name = fn + ln
print full_name
full_name = fn + ' ' + ln
print full_name

>>> 
deeptiVaidyula
deepti Vaidyula

  • Python like most high languages has garbage collection. When any object is no longer referenced by any variable, it is automatically cleaned up.
  • Repeated sequence in string
>>> rhyme = "twinkle"
>>> print rhyme*2 + " little star"
twinkletwinkle little star

String Slicing


  • string[start:end:steps]


>>> num="123456789"
>>> print num[5]
6
>>> print num[5:8]
678
>>> print num[5:8:2]
68
>>> print num[5:8:2]
KeyboardInterrupt
>>> print num[5:8:1]
678
>>> print num[5:9:1]
6789
>>> print num[5:9:2]
68
>>>


  • num.find returns the index

>>> num.find("5")
4

>>> num.find("abc")
-1
>>>
"abc" is not present in num

>>> name = "Daniel Radcliffe"
>>> name.split()
['Daniel', 'Radcliffe']
>>> 

>>> name = "Daniel:Radcliffe"
>>> name.split(":")
['Daniel', 'Radcliffe']

>>> name.replace("Daniel", "Ginny")

'Ginny:Radcliffe'

String Formatting

>>> ip="192.168.12.38"
>>> print "Hack this ip : %s" %ip
Hack this ip : 192.168.12.38

Operations and Lists



>>> 2 + 2
4
>>> list=[1,2,3,4]
>>> list[0]
1
>>> list[-1]
4
>>> len(list)

4


  • Lists are heterogenous. So, we can mix numbers and alphabet


>>> mylist=[1,2,3,4,["a","b","c"]]
>>> mylist
[1, 2, 3, 4, ['a', 'b', 'c']]
>>> len(mylist)

5
>>> mylist.append(5)
>>> mylist
[1, 2, 3, 4, ['a', 'b', 'c'], 5]
>>> mylist.reverse()
>>> print mylist.reverse()
None
>>> mylist.pop()
5
>>> mylist
[1, 2, 3, 4, ['a', 'b', 'c']]
>>> mylist.insert(4,"inserted")
>>> mylist

[1, 2, 3, 4, 'inserted', ['a', 'b', 'c']]
>>> mylist

[1, 2, 4, 'inserted', ['a', 'b', 'c']]













No comments:

Post a Comment