Embed presentation
Download as PDF, PPTX






![Command Line Arguments• Sys module comes to the rescue.• Import sys• Print sys.argv[0]• For i in sys.argv[]: print i• The type of all the command line arguments is str.• Checking for input data types and type conversion should always be done.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-7-2048.jpg&f=jpg&w=240)
![Python Interpreter• Executing interactively: Open DOS prompt and just type python.• C:>python ActivePython 2.5.1.1 (ActiveState Software Inc.) based on Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>• >>> print "Welcome to Technobeans!" Welcome to Technobeans!• To come out of the interactive shell, press Ctrl+Z and Enter key.• When Python interpreter loads, modules/packages will be available for importing <Python installation>libsite-packages.• sys.path.append("C:My_Scripts") – for importing user defined modules.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-8-2048.jpg&f=jpg&w=240)






![None, Empty!!• Def foo(): pass print foo() - None• list = [] for i in list: print i - Empty• a = raw_input(“enter a:”) press “enter key” - Empty/ Sometimes referred as Nothing• None is commonly used for exception handling.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-15-2048.jpg&f=jpg&w=240)

![Operators• Addition: +• Subtraction: -• Multiplication: *• Exponentiation: * *• Division: / and / / (floor or [x])• Modulus: %](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-17-2048.jpg&f=jpg&w=240)


![Looping Execution - while / for• while condition : Statements• for var_name in Sequence/function which outputs a sequence: statements Range() - iterate over a sequence of numbers. range(5) = [0,1,2,3,4] range(5,10) = [5,6,7,8,9] range(0,10,3) = [0,3,6,9]• Do-while: Not available. While True: if condition: break](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-20-2048.jpg&f=jpg&w=240)

![Sequence data type• A Sequence type in python is a mini built-in data structure that contains elements in an orderly manner which can be fetched using indices. There are three types of sequences: Strings >>> a_string = “Chetan Giridhar" Lists >>> a_list = [“Chetan",”Giridhar"] Tuples >>> a_tuple = (“Chetan",”Giridhar")• Strings and tuples are immutable in Python, but lists are mutable. (An immutable object can not modified-in-place. What it means is that when a function/expression tries to modify its contents right there its original memory location, it either fails or creates an altogether new object.). >>> a_string [0] = "t“ - Invalid >>> a_list[0] = "My Example“ - Valid >>> a_tuple[0] = "My Example“ - Invalid](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-22-2048.jpg&f=jpg&w=240)

![Working with lists• Defining lists numList = [2003,2005,2008,2010] strList = [“IIA”, “Chetan”, “Python”]• Accessing a list For x in numList: print x print strList[0] or strList[-1]• Slicing a list firstHalf = numList[:2] lastHalf = numList[2:3]• Adding and removing items list.append(“2009”) – end of the list list.extend(list1) – end of the list list.insert(index, item) – at a specified index](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-24-2048.jpg&f=jpg&w=240)
![Pop(index) – removes the element at the index remove(item) – removes the first occurrence of item.• Sorting a list list.sort() list.reverse() list = ["iia","IIA", "chetan", "python"] list.sort(key = str.lower) for i in list: print I• Converting tuple to list List(tuple)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-25-2048.jpg&f=jpg&w=240)
![String functions• test = ‘This is a simple string’• len(test) = 23• test.count(‘r’) = 1• test.find(‘r’) = 18• test = test.replace(‘simple’, ‘short’) ‘This is short string’• test.index(‘simple’) = 10• test.split(‘is’) = [‘This ‘, ‘a short string’]• ‘some’. join(test.split(‘is’))• test.upper() and test.lower() and test.lower.capatalize()• test.lstrip(‘ ‘) and test.rstrip(‘t’)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-26-2048.jpg&f=jpg&w=240)
![eval vs exec• Exec function will execute Python Code that is contained in str string and return the result.• Eval works similar to the exec function except that it only evaluates the string as Python expression and returns the result.• def foo(): print "foo“ eval("foo" + "()")• cards = ['king', 'queen', 'jack'] codeStr = "for i in cards: print i" exec(codeStr)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-27-2048.jpg&f=jpg&w=240)

![Working with dicts…>>> aDict = {"a" : 1, "b": 3}>>> type(aDict) type 'dict‘>>> aDict.keys() and aDict.values()>>> aDict.items() - [('a', 1), ('b', 2)]>>> for key in aDict.keys(): print "Key: " + str(key) + " Value: " +str(aDict[key]) Key: a Value: 1 Key: b Value: 2>>> aList = [1,2,3] aDict = {"a" : 1, "b": 3} aNestedDict = {'key1' : aList, 'key2': aDict}>>> aNestedDict['key2']['a'] - 1>>> for key,value in aDict.iteritems() swapDict[value] = key](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-29-2048.jpg&f=jpg&w=240)






![• >>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]• >>> print filter(lambda x: x % 3 == 0, foo)• [18, 9, 24, 12, 27]• >>> print map(lambda x: x * 2 + 10, foo)• [14, 46, 28, 54, 44, 58, 26, 34, 64]• >>> print reduce(lambda x, y: x + y, foo)• 139](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-36-2048.jpg&f=jpg&w=240)






![• Regular expressions use the backslash character ('') to indicate special forms or to allow special characters to be used without invoking their special meaning.• What if we want to use ‘’ in file path?• The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'.• f = open(r"c:Windowsnotepad.exe", "r")• Special characters: ‘.’, ‘^’, ‘$’,’*’,’+’,’?’,{m}, {m,n},’’, ‘|’, ‘(…)’, ‘d’, ‘D’, ‘s’, ‘S’, ‘w’, ‘W’• re.split('[a-f]+', '0a3B9’) - ['0', '3', '9']](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-43-2048.jpg&f=jpg&w=240)













![Garbage Collection• Python's memory allocation and de-allocation method is automatic.• Python uses two strategies for memory allocation reference counting and garbage collection.• Reference counting works by counting the number of times an object is referenced by other objects in the system. When references to an object are removed, the reference count for an object is decremented. When the reference count becomes zero the object is de-allocated.• Caveat is that it cannot handle reference cycles.• def make_cycle(): l=[] l.append(l)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-57-2048.jpg&f=jpg&w=240)






This tutorial provides an introduction to the Python programming language. It will cover Python's core features like syntax, data types, operators, conditional and loop execution, functions, modules and packages to enable writing basic programs. The tutorial is intended for learners to learn Python together through questions, discussions and pointing out mistakes.






![Command Line Arguments• Sys module comes to the rescue.• Import sys• Print sys.argv[0]• For i in sys.argv[]: print i• The type of all the command line arguments is str.• Checking for input data types and type conversion should always be done.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-7-2048.jpg&f=jpg&w=240)
![Python Interpreter• Executing interactively: Open DOS prompt and just type python.• C:>python ActivePython 2.5.1.1 (ActiveState Software Inc.) based on Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>• >>> print "Welcome to Technobeans!" Welcome to Technobeans!• To come out of the interactive shell, press Ctrl+Z and Enter key.• When Python interpreter loads, modules/packages will be available for importing <Python installation>libsite-packages.• sys.path.append("C:My_Scripts") – for importing user defined modules.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-8-2048.jpg&f=jpg&w=240)






![None, Empty!!• Def foo(): pass print foo() - None• list = [] for i in list: print i - Empty• a = raw_input(“enter a:”) press “enter key” - Empty/ Sometimes referred as Nothing• None is commonly used for exception handling.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-15-2048.jpg&f=jpg&w=240)

![Operators• Addition: +• Subtraction: -• Multiplication: *• Exponentiation: * *• Division: / and / / (floor or [x])• Modulus: %](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-17-2048.jpg&f=jpg&w=240)


![Looping Execution - while / for• while condition : Statements• for var_name in Sequence/function which outputs a sequence: statements Range() - iterate over a sequence of numbers. range(5) = [0,1,2,3,4] range(5,10) = [5,6,7,8,9] range(0,10,3) = [0,3,6,9]• Do-while: Not available. While True: if condition: break](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-20-2048.jpg&f=jpg&w=240)

![Sequence data type• A Sequence type in python is a mini built-in data structure that contains elements in an orderly manner which can be fetched using indices. There are three types of sequences: Strings >>> a_string = “Chetan Giridhar" Lists >>> a_list = [“Chetan",”Giridhar"] Tuples >>> a_tuple = (“Chetan",”Giridhar")• Strings and tuples are immutable in Python, but lists are mutable. (An immutable object can not modified-in-place. What it means is that when a function/expression tries to modify its contents right there its original memory location, it either fails or creates an altogether new object.). >>> a_string [0] = "t“ - Invalid >>> a_list[0] = "My Example“ - Valid >>> a_tuple[0] = "My Example“ - Invalid](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-22-2048.jpg&f=jpg&w=240)

![Working with lists• Defining lists numList = [2003,2005,2008,2010] strList = [“IIA”, “Chetan”, “Python”]• Accessing a list For x in numList: print x print strList[0] or strList[-1]• Slicing a list firstHalf = numList[:2] lastHalf = numList[2:3]• Adding and removing items list.append(“2009”) – end of the list list.extend(list1) – end of the list list.insert(index, item) – at a specified index](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-24-2048.jpg&f=jpg&w=240)
![Pop(index) – removes the element at the index remove(item) – removes the first occurrence of item.• Sorting a list list.sort() list.reverse() list = ["iia","IIA", "chetan", "python"] list.sort(key = str.lower) for i in list: print I• Converting tuple to list List(tuple)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-25-2048.jpg&f=jpg&w=240)
![String functions• test = ‘This is a simple string’• len(test) = 23• test.count(‘r’) = 1• test.find(‘r’) = 18• test = test.replace(‘simple’, ‘short’) ‘This is short string’• test.index(‘simple’) = 10• test.split(‘is’) = [‘This ‘, ‘a short string’]• ‘some’. join(test.split(‘is’))• test.upper() and test.lower() and test.lower.capatalize()• test.lstrip(‘ ‘) and test.rstrip(‘t’)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-26-2048.jpg&f=jpg&w=240)
![eval vs exec• Exec function will execute Python Code that is contained in str string and return the result.• Eval works similar to the exec function except that it only evaluates the string as Python expression and returns the result.• def foo(): print "foo“ eval("foo" + "()")• cards = ['king', 'queen', 'jack'] codeStr = "for i in cards: print i" exec(codeStr)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-27-2048.jpg&f=jpg&w=240)

![Working with dicts…>>> aDict = {"a" : 1, "b": 3}>>> type(aDict) type 'dict‘>>> aDict.keys() and aDict.values()>>> aDict.items() - [('a', 1), ('b', 2)]>>> for key in aDict.keys(): print "Key: " + str(key) + " Value: " +str(aDict[key]) Key: a Value: 1 Key: b Value: 2>>> aList = [1,2,3] aDict = {"a" : 1, "b": 3} aNestedDict = {'key1' : aList, 'key2': aDict}>>> aNestedDict['key2']['a'] - 1>>> for key,value in aDict.iteritems() swapDict[value] = key](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-29-2048.jpg&f=jpg&w=240)






![• >>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]• >>> print filter(lambda x: x % 3 == 0, foo)• [18, 9, 24, 12, 27]• >>> print map(lambda x: x * 2 + 10, foo)• [14, 46, 28, 54, 44, 58, 26, 34, 64]• >>> print reduce(lambda x, y: x + y, foo)• 139](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-36-2048.jpg&f=jpg&w=240)






![• Regular expressions use the backslash character ('') to indicate special forms or to allow special characters to be used without invoking their special meaning.• What if we want to use ‘’ in file path?• The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'.• f = open(r"c:Windowsnotepad.exe", "r")• Special characters: ‘.’, ‘^’, ‘$’,’*’,’+’,’?’,{m}, {m,n},’’, ‘|’, ‘(…)’, ‘d’, ‘D’, ‘s’, ‘S’, ‘w’, ‘W’• re.split('[a-f]+', '0a3B9’) - ['0', '3', '9']](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-43-2048.jpg&f=jpg&w=240)













![Garbage Collection• Python's memory allocation and de-allocation method is automatic.• Python uses two strategies for memory allocation reference counting and garbage collection.• Reference counting works by counting the number of times an object is referenced by other objects in the system. When references to an object are removed, the reference count for an object is decremented. When the reference count becomes zero the object is de-allocated.• Caveat is that it cannot handle reference cycles.• def make_cycle(): l=[] l.append(l)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2ftutorial-on-python-programming-110927122223-phpapp02%2f75%2fTutorial-on-python-programming-57-2048.jpg&f=jpg&w=240)




