Skip to content

Introduction to Python

ZekeLabs Technologies Private Limited edited this page Dec 1, 2016 · 2 revisions

##Important Aspects of Python

  • Dynamically Typed Language - The type of the variable is identified based on the value assigned to it.

  • Automatic Memory Management - No need of programmer to worry about memory allocation & free

##The ‘is’ operator

  • This will return true if both the variables are referring to same memory

  • For numbers less than 255 will exhibit true

>>> a = 5 >>> b = 5 >>> a is b True >>> a = 1000 >>> b = 1000 >>> a is b False

  • Smaller length string will show this behaviour

###Garbage Collector in Python

  • Python’s GC finds out all the objects in the memory which have zero reference count & deletes it.

  • a = 5, a piece of memory is alloccated for 5 & a refers to 5. The object memory which holds 5 contains - ref-count, data-type & data.

###Math Library >>> import math

>>> math.cos(10) -0.8390715290764524 >>> math.cos(90) -0.4480736161291701

>>> math.floor(3.7) 3.0 >>> help(math)

>>> math.factorial(5)

>>> s.count(‘ ’) 2 >>> s = ‘This is good odd’

>>> s.count(‘od’) 2

>>> s.find(‘od’) 10 >>> s.find(‘is’) 2 >>> s = ‘this is a nice is ssd idid sisi’ >>> s.count(‘is’) 4 >>> s.find(‘is’) 2 >>> s.find(‘is’,3) 5 >>> s.find(‘is’,6) 15 >>> s.find(‘is’,16) 29

>>> s.find(‘iskk’,16) -1 >>> t = ‘goog god great’ >>> s + t ‘this is a nice is ssd idid sisigoog god great’

‘this is a nice is ssd idid sisi’ >>> url = ‘www.google.com

www.google.com

>>> url.endswith(‘com’) True >>> url.endswith(‘net’) False >>> url.startswith(‘net’) False >>> url.startswith(‘www’) True >>> s = ‘2121212’ >>> s.isdigit() True >>> s = ‘2121212j’

>>> s.isdigit() False >>> s.isalnum() True >>> s.isalpha() False

>>> s = ‘this is good. isnt it?’ >>> s.replace(‘is’,‘this’) ‘ththis this good. thisnt it?’

‘this is good. isnt it?’

>>> s.find(‘is’) 2 >>> s.rfind(‘is’) 14 >>> s.rindex(‘is’) 14 >>> s = ‘[email protected]’ >>> s.split(‘@’)

‘awantikdas’, ‘gmail.com’

>>> s.partition(‘@’) (‘awantikdas’, ‘@’, ‘gmail.com’) >>> s = ‘awantikdas@gmail@com’

>>> s.split(‘@’)

‘awantikdas’, ‘gmail’, ‘com’

>>> s.partition(‘@’) (‘awantikdas’, ‘@’, ‘gmail@com’)

>>> s = ‘22.34’ >>> s.partiition(‘.’) Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: ‘str’ object has no attribute ‘partiition’ >>> s.partition(‘.’) (‘22’, ‘.’, ‘34’) >>> f,p,m =s.partition(‘.’) >>> f.isdigit() and m.isdigit() True >>> s = ‘22.34.8’ >>> f,p,m =s.partition(‘.’) >>> f.isdigit() and m.isdigit() False

‘34.8’ >>> s = ‘ ddsds s sdsd n’ >>> s.strip() ‘ddsds s sdsd’ >>> s.replace(‘ ’,”) ‘ddsdsssdsdn’

‘ ddsds s sdsd n’ >>> s = s.replace(‘ ’,”)

‘ddsdsssdsdn’

>>> s = ‘{name} is great {}% hike’ >>> s = ‘{name} is great {percent}% hike’

>>> s.format(percent=20,name=‘awantik’) ‘awantik is great 20% hike’ >>> s.format(percent=30,name=‘Great’) ‘Great is great 30% hike’ >>> help(format)

##Strings

  1. Immutable - String don’t change

  2. Iterable - It can be travelled

>> s1 = ‘Great Python’ s1 points to a piece of memory holding ‘Great Python’. You can change what s1 points to but cannot change the contents of what s1 points to. >> s1 = ‘great’ // This is valid >> s1 = ‘h’ //This is not valid

###String multiplication >>> s1 * 2 ‘greatgreat’

###String concatination >>> s1 + ‘ ’ + s1 ‘great great’

###Reverse String >>> s ‘dlrow olleh’

None of string operation will mutate the actual string but return new string

##Inbuilt String functions To see do >>> help(str)

###endswith & startswith

>>> s.endswith(‘world’) True >>> s = ‘www.zekelabs.com’ >>> s.ends(‘.com’) Traceback (most recent call last):

File "<stdin>", line 1, in <module>

AttributeError: ‘str’ object has no attribute ‘ends’ >>> s.endswith(‘.com’) True >>> s.endswith(‘.in’) False >>> s.startswith(‘www’) True

###Difference between find & index

  • find - Pattern may or may not be present

>>> s.find(‘.’,4) 12 >>> s.find(‘hhg’,4) -1 >>> s.index(‘hhg’,4) Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ValueError: substring not found

>>> s.find(‘hhg’) -1 >>> s.find(‘.’) 3 >>> s.rfind(‘.’) 12 >>> s.rfind(‘.’)

###format (({>>> s = “Hi, Mr {name}. Congrats you got {percent}% hike”})) (({>>> s})) (({‘Hi, Mr {name}. Congrats you got {percent}% hike’})) (({>>> s.format(name=“Khan”, percent=200)})) (({‘Hi, Mr Khan. Congrats you got 200% hike’})) (({>>> })) (({>>> s.format(name=“Das”, percent=20)})) (({‘Hi, Mr Das. Congrats you got 20% hike’})) (({>>> s.format(percent=20,name=“Das”)})) (({‘Hi, Mr Das. Congrats you got 20% hike’}))

Clone this wiki locally