Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

Python Basics

Más Contenido Relacionado

Audiolibros relacionados

Gratis con una prueba de 30 días de Scribd

Ver todo

Python Basics

  1. 1. Introduction to Python Tushar Panda 1
  2. 2. Overview • Background • Syntax • Types / Operators / Control Flow • Functions • Classes • Tools 2
  3. 3. What is Python • Multi-purpose (Web, GUI, Scripting, etc.) • Object Oriented • Interpreted • Strongly typed and Dynamically typed • Focus on readability and productivity 3 3
  4. 4. Python IDE • Everything is an Object • Interactive Shell • Huge set of libraries • Cross Platform • CPython, Jython, IPython, PyPy 4
  5. 5. IDEs • Emacs • Vim • Komodo • PyCharm • Eclipse (PyDev) 5
  6. 6. Python IDE 6
  7. 7. Hello World hello_world.py 7
  8. 8. Comments Comments • Start comments with # – the rest of line is ignored. • Can include a documentation string as the first line of any new function or class that you define. • The development environment, debugger, and other tools use it: its good style to include one. def my_function(x, y): """This is the docstring. This 8
  9. 9. Indentation • Most languages don’t care about indentation • Most humans do • We tend to group similar things together 9
  10. 10. Indentation You should always be explicit 10
  11. 11. Indentation Text Python embraces indentation 11
  12. 12. Assignment • Binding a variable in Python means setting a name to hold a reference to some object. • Assignment creates references, not copies • Python determines the type of the reference automatically based on the data object assigned to it. • You create a name the first time it appears on the left side of an assignment expression: x = 3 12
  13. 13. Naming Rules • Names are case sensitive and cannot start with a number. They can contain letters, numbers, and underscores. name Name _name _ • There are some reserved words: and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, not, or, pass, print, raise, return, try, while 13
  14. 14. Importing Modules • Use classes & functions defined in another file. • A Python module is a file with the same name (plus the .py extension) • Like Java import, C++ include. import somefile from somefile import * from somefile import className What’s the difference? What gets imported from the file and what name refers to it after it has been imported. 14
  15. 15. more import... import somefile • Everything in somefile.py gets imported. • To refer to something in the file, append the text “somefile.” to the front of its name: somefile.className.method(“abc”) somefile.myFunction(34) from somefile import * • Everything in somefile.py gets imported • To refer to anything in the module, just use its name. Everything in the module is now in the current namespace. • Caveat! Using this import command can easily overwrite the definition of an existing function or variable! className.method(“abc”) myFunction(34) 15
  16. 16. Where does Python look for module files? • The list of directories in which Python will look for the files to be imported: sys.path (Variable named ‘path’ stored inside the ‘sys’ module.) • To add a directory of your own to this list, append it to this list. sys.path.append(‘/my/new/path’) • default directory /usr/local/lib/python2.7/dist-packages 16
  17. 17. Operators & Expressions Python Operators: • Arithmetic Operators • Comparison (Relational) Operators • Assignment Operators • Logical Operators • Bitwise Operators • Membership Operators • Identity Operators Python Expressions: • Expressions in programming are like formulas in maths • both use values to compute a result. 17
  18. 18. Arithematic Operators c = a + b c = a - b c = a * b c = a / b c = a % b a = 2 b = 3 c = a**b print "Line 6 - Value of c is ", c a = 10 b = 5 c = a//b print "Line 7 - Value of c is ", c 18
  19. 19. Comparision operator if ( a == b ): print "Line 1 - a is equal to b" else: print "Line 1 - a is not equal to b" if ( a != b ): print "Line 2 - a is not equal to b" else: print "Line 2 - a is equal to b" if ( a <> b ): print "Line 3 - a is not equal to b" else: print "Line 3 - a is equal to b" if ( a < b ): print "Line 4 - a is less than b" else: print "Line 4 - a is not less than b" 19
  20. 20. Assignment Operators a = 21 b = 10 c = 0 c = a + b c += a c *= a c /= a c %= a c **= a c //= a 20
  21. 21. Logical Operators 21
  22. 22. Strings Strings are sequences of characters Indexed exactly like lists name = 'somename' print name[5] name = 'myname' for c in name: print c print 'in','dia' print 'in dia' ' == " but one a time SyntaxError: EOL while scanning string literal 22
  23. 23. Strings String are compared by charecter: print 'a'<"b" print 'cde'<"xy" print 'cde'<"cda" print '10'<'9' Strings are immutable: name = 'tushar' name[1] = 'i' TypeError: 'str' object does not support item assignment Formatting: emp_id = 100 percentage_business = 8 print 'employee id:' + str(emp_id) + ' produced ' + str(percentage_business) + '% business' print 'employee id:%s produced %d%% business' %(str(emp_id),percentage_business) percentage_yield = 12.3 print 'yield: %6.2f' % percentage_yield 23
  24. 24. Concantenate var1 = 'kill bill!' print "Updated String :- ", var1[:6] + 'all' name = 'tushar' + ' ' + 'ranjan' name+ = 'panda' print name Supported escape charecters: a Bell or alert b Backspace cx Control-x C-x Control-x e Escape f Formfeed M-C-x Meta-Control-x n Newline r Carriage return s Space t Tab v Vertical tab 24
  25. 25. SpecialOperators + * [] [ : ] in not in % 25
  26. 26. More strings... Raw Strings: print 'D:filename' print r'D:filename O/P D:filename D:filename Unicode String # -*- coding: UTF-8 -*- print u"àçñ" title = u"Klüft skräms inför på fédéral électoral große" import unicodedata Print unicodedata.normalize('NFKD', title).encode('ascii','ignore') 'Kluft skrams infor pa federal electoral groe' 26
  27. 27. Python Typing _Dynamic Typing_ Python determines the data types of variable bindings in a program automatically. _Strong Typing_ But Python_s not casual about types, it enforces the types of objects. Note: You can_t just append an integer to a string. You must first convert the integer to a string itself. x = "the answer is " y = 23 print x + y (oops mistake...) 27
  28. 28. Functions 28
  29. 29. Calling a Function.. 29
  30. 30. Containers • List • Tuple • Dictionary • Sets 30
  31. 31. List List is heterogeneous variable-sized array. fruits = ['apple', 27, 'python', [3,'hello']] ex: list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print list1[2] print list2[1:5] 31
  32. 32. List Methods: • insert(index,item) • update • append • delete • remove • extend • join • reverse 32
  33. 33. List Operations: concantenate (['a', 'b', 'c'] + [4, 5, 6]) repeat (['hello'] * 3) Searching List: using member of (5 in [1, 2, 5]) range for x in [1, 2, 3]: print x, Notes: you can put all kinds of objects in lists, including other lists, and multiple references to a single object. 33
  34. 34. List The List sequence can be any kind of sequence object or iterable, including tuples and generators. If you pass in another list, the list function makes a copy. creates a new list every time you execute the [] expression. A = []; B = [] No more, no less. And Python never creates a new list if you assign a list to a variable. A = B = [] # both names will point to the same list A = [] B = A # both names will point to the same list 34
  35. 35. List Comprehensions python supports computed lists called list comprehensions. L = [expression for variable in sequence] A powerful feature of the Python language. • Generate a new list by applying a function to every member of an original list. • Python programmers use list comprehensions extensively. The syntax of a list comprehension is somewhat tricky. • Syntax suggests that of a for-loop, an in operation, or an if statement • all three of these keywords (_for_, _in_, and _if_) are also used in the syntax of forms of list comprehensions. 35
  36. 36. List Comprehensions ... Ex: list1 = [3, 6, 2, 7] print [element*2 for element in list1] [6, 12, 4, 14] • The expressions in list comprehension can be anything. • all kinds of objects in lists, including other lists, and multiple references to a single object. • If different types of elements are present , expression must operate correctly on all the types. • If the elements of list are other containers, then the name can consist of a container of names that match the type and shape of the list members. list1 = [(a, 1), (b, 2), (c, 7)] print [ n * 3 for (x, n) in list1] [3, 6, 21] 36
  37. 37. Iterators List has support for iterator protocol. i = iter(L) item = i.next() Locate first element index: try: index = L.index(value) except ValueError print "No match" Locate all element index: while 1: i = L.index(value, i+1) print "match at", i print L.count() 37
  38. 38. Tuple A tuple is like an immutable list. It is slightly faster and smaller than a list. t = () t = ("iit") t = ("iit","nit") print t t[1] = "trp" TypeError: 'tuple' object does not support item assignment 38
  39. 39. Tuple list to tuple: a1 = ["python","java"] a2 = tuple(a1) print type(a1) print type(a2) unpacking values: t = ('lakshmi','mittal') first_name,family_name = t print first_name,family_name Tuple Slicing: t = ('Lakshmi','Niwas','Mittal') print t[0::2] ('Lakshmi', 'Mittal') 39
  40. 40. Dictionary operate as key-value pairs. dict = {} dict['Name'] = 'Raj' dict['Age'] = 7 dict['Class'] = 'First' • Dictionaries are used for non-integer index containers. • Any immutable type can be used as index. removing an item: del dict['Class'] getting keys/values/items: print dict.keys() print dict.values() print dict.items() print dict.has_key('name') 40
  41. 41. Sets A set is gathering of definite & distinct objects. The objects are called elements of the set. Creating Set: x = set("A Python Tutorial") print x No element duplication in set Immutable sets cities = set((("Python","Perl"), ("Delhi", "Mumbai", "Pune"))) print cities Add: colours = {"red","green"} colours.add("yellow") 41
  42. 42. Sets Clear: cities = {"chennai", "hyderabad", "bangalore"} cities.clear() Copy: cities = {"chennai", "hyderabad", "bangalore"} cities2 = cities.copy() cities.clear() print cities2 copy() is NOT EQUAL TO assignment. cities = set((["Python","Perl"], ["Delhi", "Mumbai", "Pune"])) print cities TypeError: unhashable type: 'list' Reason: assignment fails as two pointers points to a blank(previously full) location. 42
  43. 43. Control Statements • While • for • nested • break • loop • pass • continue 43
  44. 44. Simple Loop count = 10 while (count < 20): print 'Counter value:', count count = count + 1 for loop can iterate list , The list can be heterogenous . A for loop can also iterate over a "generator", which is a small piece of code instead of an actual list. the range function can be used with loops. Ex: n = int(input('how many iterations ??')) for i in range(0,n): print "iteration",n 44
  45. 45. infinite loop: ========================= While True: // do something infinite loop with break: ========================= While True: condition match: hit the break condition, move out else keep moving ex: with open("logfile",'rb') as f: while True: line=f.readline() if not line: break print line 45
  46. 46. Exceptions string1 = "I like Python" print string1[54] IndexError: string index out of range An exception in general is an event, • which occurs during the execution of a program • that disrupts the normal flow of the program's instructions. why exceptions are important ?? 1. handle errors 2. prevent program flow disruption. 3. nothing matches, use else. 46
  47. 47. Exceptions In Python: Exception is a object that represents an error. x = 5 y =0 try: z = x/y except ZeroDivisionError: print "divide by zero" 47
  48. 48. Class & Objects OOP Terminology: Class, Objects, Methods • Class variable • Data member • Function overloading • Instance • Instance variable • Inheritance • Instantiation • Operator overloading 48
  49. 49. Class & Objects Class: A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation. Class variable: A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are. Data member: A class variable or instance variable that holds data associated with a class and its objects. Function overloading: The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects or arguments involved. Instance variable: A variable that is defined inside a method and belongs only to the current instance of a class. Inheritance: The transfer of the characteristics of a class to other classes that are derived from it. Instance: An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle. Instantiation: The creation of an instance of a class. Method : A special kind of function that is defined in a class definition. Object: A unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods. Operator overloading: The assignment of more than one function to a particular operator. 49
  50. 50. Class & Objects Objects Everything in Python is an object that has: - an identity (id) - a value (mutable or immutable) , Objects whose value can change are said to be mutable. Ex: a = 7 print id(a) 632887 Mutable: id remains same. Dictionary, List Immutable: id changes. String, Integer, Tuple 50
  51. 51. Class & ObjectsMore on mutable: b = ["hello"] print id(b) 139908350192024 b.append("world") print id(b) 139908350192024 a = "super" print id(a) 140304291482864 a = "super man" print id(a) 140304290186416 51
  52. 52. Class & Objects Creating Classes class Employee: empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount def displayEmployee(self): print "Name : ",self.name, print "Salary: ",self.salary 52
  53. 53. Class & Objects creating instance objects: emp1 = Employee("alex","45") emp2 = Employee("hari","45") Accessing Attributes print emp1.displayCount() print emp2.displayCount() 53
  54. 54. Class & Objects class Employee: empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount def displayEmployee(self): print "Name : ", self.name, ", Salary: ", self.salary emp1 = Employee("alex","45") emp2 = Employee("hari","45") print emp1.displayCount() print emp2.displayCount() 54
  55. 55. Class build-in attributes class Employee: 'Common base class for all employees' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount def displayEmployee(self): print "Name : ", self.name, ", Salary: ", self.salary print Employee.__doc__ print Employee.__name__ print Employee.__module__ print Employee.__bases__ print Employee.__dict__ 55
  56. 56. Defn: __doc__: Class documentation string or none, if undefined. __name__: Class name. __module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode. __bases__: A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list. __dict__: Dictionary containing the class's namespace. Answer: Common base class for all employees Employee __main__ () {'__module__': '__main__', 'displayCount': <function displayCount at 0x7facd5bbd668>, 'empCount': 0, 'displayEmployee': <function displayEmployee at 0x7facd5bbd6e0>, '__doc__': 'Common base class for all employees', '__init__': <function __init__ at 0x7facd5bbd5f0>} 56
  57. 57. Files What You Need In Order To Read Information From A File 1. Open the file and associate the file with a file variable. 2. A command to read the information. 3. A command to close the file. 57
  58. 58. Files opening file: <file variable> = open(<file name>, "r") Example: inputFile = open("data.txt", "r") What open does ?? A. Links the file variable with the physical file (references to the file variable are references to the physical file). B. Positions the file pointer at the start of the file. Dynamic file naming: filename = input("Enter name of input file: ") inputFile = open(filename, "r") 58
  59. 59. Files reading file: Example: for line in inputFile: print(line) # Echo file contents back onscreen Notes: - Typically reading is done within the body of a loop - Each execution of the loop will read a line from the - put file into a string 59
  60. 60. Files Closing File: • Format: <name of file variable>.close() • Example: inputFile.close() Caution: Always explicitly close the files. Reason: - if the program encounters a runtime error and crashes before it reaches the end, the file remain ‘locked’ in an inaccessible state because it’s still open. 60
  61. 61. Files What You Need In Order To Write Information From A File 1. Open the file and associate the file with a file variable. 2. A command to read the information. 3. A command to close the file. 61
  62. 62. writing to file: iFile = input("file to be read") oFile = input("fileto be written ") inputFile = open(iFile, "r") outputFile = open(oFile, "w") ... ... for line in inputFile: if (line[0] == "A"): grade = 4 elif (line[0] == "B"): grade = 3 elif (line[0] == "F"): grade = 0 else: print "Invalid Grade" outputFile.write (grade) ... ... inputFile.close () outputFile.close () print ("Completed reading ", iFile) print ("Completed writing ", oFile) 62
  63. 63. Pickle storing retrieving complex types: ex: dictionary import pickle f = open('helloworld.file', 'wb') dict1 = {'name': 'tushar'} pickle.dump(dict1, f) f.close() import pickle f = open('helloworld.file', 'rb') load_info = pickle.load(f) print load_info['name'] 63
  64. 64. Login using PickleAuto Login to Microsoft Webmail =============================== from selenium import webdriver from selenium.webdriver.common.keys import Keys import pickle import time import sys profile = webdriver.FirefoxProfile("/home/tusharpanda.1988/.mozilla/firefox/ylytpp1m.default") driver = webdriver.Firefox(profile) driver.get("http://webmail.<COMPANY_NAME>.com") ###GETTING COOKIES import pickle element = driver.find_element_by_id('username') element.send_keys("<DOM><USERNAME>") element = driver.find_element_by_id('password') element.send_keys("PASSWORD") element.send_keys(Keys.ENTER) pickle.dump(driver.get_cookies() , open("webmail.pkl","wb")) driver.quit() 64
  65. 65. Login using Pickle... ###SETTING COOKIES import pickle driver.get("http://webmail.<COMPANY_NAME>.com") sys.stdout.flush() for cookie in pickle.load(open("webmail.pkl", "rb")): driver.add_cookie(cookie) sys.stdout.flush() driver.get("http://webmail.<COMPANY_NAME>.com") sys.stdout.flush() driver.quit() 65
  66. 66. Regular Expressions 66
  67. 67. Regular Expression Why need it ?? Data files generated by a third party. No control over the format. Files badly need pre-processing Its used to perform pattern match/search/replace over the data. Ex: s = 'This is the main road' print s.replace('road', '0') This is the main 0 s = 'This is a broad road' s.replace('road', 'ROAD.') This is a bROAD. ROAD. s[:-4] + s[-4:].replace('ROAD', 'RD.') '100 NORTH BROAD RD.' 67
  68. 68. Regular Expression import re data = "Python is great. I like python" m = re.search(r'[pP]ython',data) print m.group() Python import re data = "I like python" m = re.search(r’python’,data) m.group() m.start() m.span() python 7 (7,13) 68
  69. 69. Regular Expression import re data = "Python is great. I like python" m = re.search(r'[pP]ython',data) print m.group() 'Python' ['Python', 'python'] import re data = "Python is great. I like python" l = re.findall(r'[pP]ython',data) print l ['Python', 'python'] re.search() returns only the first match, re.findall() return all matches. 69
  70. 70. Database Data in a Python application can be stored and referenced in multiple ways. - files - flat file database - XML, json - Relational Database (SQL) - Non Relational Database (NOSQL) 70
  71. 71. Database Sqlite: steps: • import sqlite3 module. • create a Connection object which will represent databse. • provide a database name. • if exists, file is loaded and database is opened. • create/access a table • execute command using cursor • retrieve data from database 71
  72. 72. Database Common Errors: SQLITE_ERROR 1 /* SQL error or missing database */ SQLITE_BUSY 5 /* The database file is locked */ SQLITE_PERM 3 /* Access permission denied */ SQLITE_READONLY 8 /* Attempt to write a readonly database */ SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ 72
  73. 73. import sqlite3 conn = sqlite3.connect("company.db") cursor = connection.cursor() cursor.execute("""DROP TABLE salary;""") sqlite3_command = """ CREATE TABLE salary ( emp_id INTEGER PRIMARY KEY, name VARCHAR(20), gender CHAR(1), joining DATE, );""" cursor.execute(sqlite3_command) sql_command = """ INSERT INTO salary (emp_id,name,gender,joining) VALUES(NULL, "abc", "m", "1985-02-15"); """ cursor.execute(sql_command) sql_command = """ INSERT INTO salary (emp_id,name,gender,joining) VALUES(NULL, "xyz", "f", "1987-11-04"); """ cursor.execute(sql_command) conn.commit() conn.close() 73
  74. 74. Database mysql: sudo apt-get install python-MySQLdb steps: • import MySQLdb module • Open a connection to the MySQL server • Send command and receive data • Close connection Example: import MySQLdb db = MySQLdb.connect(<SERVER>,<DATABASE_NAME>,<USERNAME>,<PASSWORD>) cursor = db.cursor() cursor.execute("SELECT VERSION()") data = cursor.fetchone() print "Database version : %s " % data db.close() 74
  75. 75. Python & Web WWW • craze for user generated content. • frameworks & tools available: a click away. • dynamic frameworks --->>> We too support MVC. The Low-Level View : user & server request: user enters a web site -> browser connects to server-> response: server looks for requested file -> sends file back to browser -> Dynamic Sites: host dynamic pages. - display posts, - show news board, - show your email, - configure software. HTTP servers are written C++, python bridges required to interact with them. fork.c 75
  76. 76. CGI & FCGI Common Gateway Interface - the oldest & supported everywhere web server. - 1 request = 1 python interpreter - simple 3 lines of code supported cgi servers: apache httpd lighttpd FastCGI no interpreter. module/library talk with independent background processes(mostly cpp). FCGI is used to deploy WSGI applications. 76
  77. 77. FCGI Setting up FastCGI Each web server requires a specific module - mod_fastcgi or mod_fcgid Apache has both. lighttpd ships its own FastCGI module. nginx also supports FastCGI. Once you have installed and configured the module, you can test it with the following WSGI-application: =================== # -*- coding: UTF-8 -*- from flup.server.fcgi import WSGIServer from <YOURAPPLICATION> import app if __name__ == '__main__': WSGIServer(app).run() =================== 77
  78. 78. Why CGI not used ?? CGI Example: import cgitb cgitb.enable() print "Content-Type: text/plain;charset=utf-8" print "Hello World!" cgitb: display error instead of crashing. risk: risk os exposing confidential data better try/catch stuff. 78
  79. 79. WSGI Standard Interface defined in pep-0333. proposed standard interface between web servers and Python web applications or frameworks. remove framework dependancy like Java Servelet API. Applications can run on multiple servers. Middleware can be reused easily. A Simple Complete Response HTTP/1.x 200 OK Server: SimpleHTTP/0.6 Python/2.4.1 Content-Type: text/html <html><body>Hello World!</body></html> def application(environ, start_response): start_response('200 OK',[('Content-type','text/html')]) return ['<html><body>Hello World!</body></html>'] What makes this a WSGI application ?? It is a callable taking ONLY two parameters. It calls start_response() with status code and list of headers. It should only be called once. The response it returns is an iterable (in this case a list with just one string). 79
  80. 80. SimpleHTTPServer import SimpleHTTPServer import SocketServer import signal import sys def receive_signal(signum, stack): print 'Stopping Server !!!', signum sys.exit(0) signal.signal(signal.SIGINT, receive_signal) PORT = 8001 Handler = SimpleHTTPServer.SimpleHTTPRequestHandler Server = SocketServer.TCPServer(("", PORT), Handler) print "serving at port", PORT Server.serve_forever() 80
  81. 81. Sockets Server #############SERVER import portfile portno = portfile.portnumber s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #s.settimeout(100) s.setblocking(0) #s.settimeout(0.5) s.bind((socket.gethostname(),portno)) s.listen(5) while True: c, addr = s.accept() print 'Got connection from', addr # print 'Peername ', c.getpeername() timestamp1 = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') c.send(timestamp1) data = c.recv(100) if data == "stopnow": print "stopnow received : Server Stopped" s.close() sys.exit(0) else: print "%s received." %data s.close() 81
  82. 82. Sockets Client #############CLIENT import socket import portfile portno = portfile.portnumber send_text = portfile.client_text s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((socket.gethostname(),portno)) while True: data = s.recv(100) print "nrecv:",data s.send(send_text) s.close() 82
  83. 83. Web Frameworks • Django • Flask • Pylons • TurboGears • Zope • Grok 83
  84. 84. Wake up !!!!
  85. 85. Thanks! 85

×