|
• What is Python?
• Why Python?
• Native Data types
• Container Data Types
• Everything is an Object
• Control Structures
• Functions and Procedures
• Classes and Instances
• Modules and packages
• Exceptions
• File Objects
• Tips and Tricks
What is Python?
• Python is an interpreted and high-level programming language
• A Dynamically typed OO language
• Design philosophy emphasizes code readability
• No compilation = Fast Edit–Test–Debug cycle
• Extensible in C or C++
Why Python?
• Clear and elegant syntax - easy to learn and use
• Dynamic interpreted Language
• Rapid Application development
• Access to system calls
• Efficient built-in data structures
• Vast standard libraries
• Flexible – OO or functional
• Platform independent
• Free, Open Source
• Heavily used in Quartz …. J
How to start Python
• Install python from python.org/download
• Launch python interpreter:
• Type python in system console
• Or In windows : start menu > run > python
• This will bring up the python interpreter. Now type:
• print 'Hello'
• type: exit() to exit from python interpreter
Data Types
• Bool True/False
• Numbers pi = 3.14159, age = 25
• Character string 'sample' , ''Double can be used''
• List [1,2,3]
• Dictionaries { 'name' : 'biswa', 'city:'HYD'}
• Objects my_instance = MyClass('foo',10)
• Modules import myfile
Numbers
• Integers:
–my_int = 4
–print my_int/3
• Long:
–2**10 1267650600228229401496703205376L
• Floating point:
–my_float = 2.0
–20/my_float 10.0
String
• str = 'hello friends! Welcome to python'
• 'hello' + 'world' 'helloworld' # concatenation
• 'hello' * 3 'hellohellohello' # repetition
• len('hello') 5 # size
• 'll' in 'hello' True # Test if 'll' is a substring
• Convert other types to string : str()
pi = 3.14159
print 'Hi! i am pi, my value is :' + str(pi)
>> Hi! i am pi, my value is : 3.14159
• print 'I can\'t escape!' I can't escape!
• print '''triple quotes
can contain
Multi line strings'''
– triple quotes
can contain
Multi line strings
• raw strings :
print r'I can escape /n' I can escape /n
String slicing
• string[start:end] Elements of the string starting from start and extending up to but not including end element.
• s = 'Hello'
• s[0] 'H'
• s[-1] 'o'
• s[1:4] 'ell'
• s[1:] 'ello' # from 1 till the end
• s[:4] 'Hell' # from begining till 3 [but not 4]
• s[:] 'Hello' #from beginning to end
• s[:-3] 'He' # from beginning going up to but not # including 3rd last element
• s[-3:] 'llo' #from 3rd last element to end
String methods
– 'aabbcc'.find('b') 2
– 'aabb'.find('cd') -1
– 'aba'.replace('a','c') 'cbc’
– 'aabaab'.count(‘a') 4
– 'name,age,sex'.split(',') ['name' , 'age' , 'sex' ]
– ';'.join( ['a' , 'b’ , ‘c' , 'd' ] 'a;b;c;d’
List
• Sequence of elements - mutable
• Can contain heterogeneous data
• E.g-
li = [1, 'two',3, 'four', 5]
li[3] 'four'
len(li) 5
li.append('a')
li [1, 'two',3, 'four', 5 , 'a' ]
li[1:3] ['two’,3]
List methods
• Adding Elements to a List
– list.append(elem)
– list.insert(index, elem)
– list.extend(list2)
– list*n / n *list
li = [2,4,5,9]
li.append(10) #now li = [2,4,5,9,10]
li.insert(2,'new')
li [2, 4, 'new', 5, 9, 10]
li.extend([1,3,5])
li [2, 4, 'new', 5, 9, 10, 1, 3, 5]
[2,4,6] + [1,3,5] [2,4,6,1,3,5]
L = [1,2] * 5 [1,2,1,2,1,2,1,2,1,2]
L += [3]*5 [1,2,1,2,1,2,1,2,1,2,3,3,3,3,3]
|
|
|
|