SlideShare una empresa de Scribd logo
1 de 85
Descargar para leer sin conexión
Introduction to Python
Tushar Panda
1
Overview
• Background
• Syntax
• Types / Operators / Control Flow
• Functions
• Classes
• Tools
2
What is Python
• Multi-purpose (Web, GUI, Scripting, etc.)
• Object Oriented
• Interpreted
• Strongly typed and Dynamically typed
• Focus on readability and productivity
3
3
Python IDE
• Everything is an Object
• Interactive Shell
• Huge set of libraries
• Cross Platform
• CPython, Jython, IPython, PyPy
4
IDEs
• Emacs
• Vim
• Komodo
• PyCharm
• Eclipse (PyDev)
5
Python IDE
6
Hello World
hello_world.py
7
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
Indentation
• Most languages don’t care about indentation
• Most humans do
• We tend to group similar things together
9
Indentation
You should always be explicit 10
Indentation
Text
Python embraces indentation 11
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
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
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
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
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
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
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
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
Assignment Operators
a = 21
b = 10
c = 0
c = a + b
c += a
c *= a
c /= a
c %= a
c **= a
c //= a
20
Logical Operators
21
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
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
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
SpecialOperators
+
*
[]
[ : ]
in
not in
%
25
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
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
Functions
28
Calling a Function..
29
Containers
• List
• Tuple
• Dictionary
• Sets
30
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
List
Methods:
• insert(index,item)
• update
• append
• delete
• remove
• extend
• join
• reverse
32
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
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
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
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
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
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
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
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
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
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
Control Statements
• While
• for
• nested
• break
• loop
• pass
• continue
43
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
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
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
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
Class & Objects
OOP Terminology:
Class, Objects, Methods
• Class variable
• Data member
• Function overloading
• Instance
• Instance variable
• Inheritance
• Instantiation
• Operator overloading
48
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
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
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
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
Class & Objects
creating instance objects:
emp1 = Employee("alex","45")
emp2 = Employee("hari","45")
Accessing Attributes
print emp1.displayCount()
print emp2.displayCount()
53
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
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
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
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
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
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
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
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
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
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
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
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
Regular Expressions
66
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Web Frameworks
• Django
• Flask
• Pylons
• TurboGears
• Zope
• Grok
83
Wake up !!!!
Thanks!
85

Más contenido relacionado

La actualidad más candente

Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Pedro Rodrigues
 
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | EdurekaPython Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | EdurekaEdureka!
 
Introduction to-python
Introduction to-pythonIntroduction to-python
Introduction to-pythonAakashdata
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
Python 3 Programming Language
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming LanguageTahani Al-Manie
 
Function arguments In Python
Function arguments In PythonFunction arguments In Python
Function arguments In PythonAmit Upadhyay
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python amiable_indian
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonMohammed Rafi
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaEdureka!
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programmingSrinivas Narasegouda
 
Data types in python
Data types in pythonData types in python
Data types in pythonRaginiJain21
 
Introduction To Python | Edureka
Introduction To Python | EdurekaIntroduction To Python | Edureka
Introduction To Python | EdurekaEdureka!
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaEdureka!
 

La actualidad más candente (20)

Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Beginning Python Programming
Beginning Python ProgrammingBeginning Python Programming
Beginning Python Programming
 
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | EdurekaPython Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
 
Introduction to-python
Introduction to-pythonIntroduction to-python
Introduction to-python
 
Python
PythonPython
Python
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Python 3 Programming Language
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming Language
 
Function arguments In Python
Function arguments In PythonFunction arguments In Python
Function arguments In Python
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Python Basics.pdf
Python Basics.pdfPython Basics.pdf
Python Basics.pdf
 
Python Presentation
Python PresentationPython Presentation
Python Presentation
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Introduction To Python | Edureka
Introduction To Python | EdurekaIntroduction To Python | Edureka
Introduction To Python | Edureka
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
 

Destacado

Creating Your First Predictive Model In Python
Creating Your First Predictive Model In PythonCreating Your First Predictive Model In Python
Creating Your First Predictive Model In PythonRobert Dempsey
 
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationPython Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationP3 InfoTech Solutions Pvt. Ltd.
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basicsLuigi De Russis
 
AmI 2016 - Python basics
AmI 2016 - Python basicsAmI 2016 - Python basics
AmI 2016 - Python basicsLuigi De Russis
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programmingKiran Vadakkath
 
Python for Big Data Analytics
Python for Big Data AnalyticsPython for Big Data Analytics
Python for Big Data AnalyticsEdureka!
 
Classification of computers
Classification of computersClassification of computers
Classification of computerssunil kumar
 
Introduction to python 3 2nd round
Introduction to python 3   2nd roundIntroduction to python 3   2nd round
Introduction to python 3 2nd roundYouhei Sakurai
 
Evolution and classification of computers
Evolution and classification of computersEvolution and classification of computers
Evolution and classification of computersAVINASH ANAND
 
Classification of computers
Classification of computersClassification of computers
Classification of computersMariam Naseer
 

Destacado (20)

python codes
python codespython codes
python codes
 
Creating Your First Predictive Model In Python
Creating Your First Predictive Model In PythonCreating Your First Predictive Model In Python
Creating Your First Predictive Model In Python
 
Python - basics
Python - basicsPython - basics
Python - basics
 
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationPython Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and Indentation
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
PythonIntro
PythonIntroPythonIntro
PythonIntro
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
Python Workshop
Python WorkshopPython Workshop
Python Workshop
 
Python basics
Python basicsPython basics
Python basics
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
AmI 2016 - Python basics
AmI 2016 - Python basicsAmI 2016 - Python basics
AmI 2016 - Python basics
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
Python basics
Python basicsPython basics
Python basics
 
Python for Big Data Analytics
Python for Big Data AnalyticsPython for Big Data Analytics
Python for Big Data Analytics
 
Classification of computers
Classification of computersClassification of computers
Classification of computers
 
Introduction to python 3 2nd round
Introduction to python 3   2nd roundIntroduction to python 3   2nd round
Introduction to python 3 2nd round
 
Evolution and classification of computers
Evolution and classification of computersEvolution and classification of computers
Evolution and classification of computers
 
Python programming language
Python programming languagePython programming language
Python programming language
 
Classification of computers
Classification of computersClassification of computers
Classification of computers
 
Introduction to Python Basics Programming
Introduction to Python Basics ProgrammingIntroduction to Python Basics Programming
Introduction to Python Basics Programming
 

Similar a Python Basics

Similar a Python Basics (20)

Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
MODULE. .pptx
MODULE.                              .pptxMODULE.                              .pptx
MODULE. .pptx
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 
PYTHON
PYTHONPYTHON
PYTHON
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptx
 
Python.pdf
Python.pdfPython.pdf
Python.pdf
 
python1.ppt
python1.pptpython1.ppt
python1.ppt
 
iPython
iPythoniPython
iPython
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Two
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
ENGLISH PYTHON.ppt
ENGLISH PYTHON.pptENGLISH PYTHON.ppt
ENGLISH PYTHON.ppt
 

Último

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Último (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Python Basics

  • 2. Overview • Background • Syntax • Types / Operators / Control Flow • Functions • Classes • Tools 2
  • 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. Python IDE • Everything is an Object • Interactive Shell • Huge set of libraries • Cross Platform • CPython, Jython, IPython, PyPy 4
  • 5. IDEs • Emacs • Vim • Komodo • PyCharm • Eclipse (PyDev) 5
  • 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. Indentation • Most languages don’t care about indentation • Most humans do • We tend to group similar things together 9
  • 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. 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. 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. 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. 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. 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. 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. 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. Assignment Operators a = 21 b = 10 c = 0 c = a + b c += a c *= a c /= a c %= a c **= a c //= a 20
  • 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. 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. 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
  • 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. 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
  • 30. Containers • List • Tuple • Dictionary • Sets 30
  • 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. List Methods: • insert(index,item) • update • append • delete • remove • extend • join • reverse 32
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Control Statements • While • for • nested • break • loop • pass • continue 43
  • 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. 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. 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. 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. Class & Objects OOP Terminology: Class, Objects, Methods • Class variable • Data member • Function overloading • Instance • Instance variable • Inheritance • Instantiation • Operator overloading 48
  • 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. 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. 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. 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. Class & Objects creating instance objects: emp1 = Employee("alex","45") emp2 = Employee("hari","45") Accessing Attributes print emp1.displayCount() print emp2.displayCount() 53
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Web Frameworks • Django • Flask • Pylons • TurboGears • Zope • Grok 83