Python�
Raje Ramrao Mahavidyalaya, Jath.
Department of B.C.A.
Name:-Miss Balikai J.I.
Python Introduction
Features of Python
Syntax Rules
Control Structures
while condition:� instructions
else: # optional� instructions
for var in S: � instructions
else: # optional� instructions
for i in range(n):� instructions
Built-in Data Structures
Functions and Parameters
More Built-in Functions
Artificial Intelligence – D. Vrajitoru
Example of Conditional
def check_type(x):
if type(x) == type(0):
print x, "is an integer"
elif type(x) == type(1.0):
print x, "is a float"
elif type(x) == type(""):
print x, "is a string"
elif type(x) == type([]):
print x, "is an array"
...
Artificial Intelligence – D. Vrajitoru
Example of while/else
def Euler(a, b):
if b==0:
return a
r = a % b
while r:
a = b
b = r
r = a % b
else:
print "a divisible by b"
return b
return r
Artificial Intelligence – D. Vrajitoru
Booleans
x = 0
if not x:
print “0 is False”
Artificial Intelligence – D. Vrajitoru
Default Values for Parameters
Artificial Intelligence – D. Vrajitoru
Variables and Scope
Artificial Intelligence – D. Vrajitoru
Example Scope
def test_scope():
for i in range(4):
for j in range (3):
x = i*10+j
if x>20:
print x,
print x
test_scope()
21 22 30 31 32 32
Artificial Intelligence – D. Vrajitoru
Try - Except
Artificial Intelligence – D. Vrajitoru
def scope1():
y = 15
y = 20
def scope2():
y = 25
def scope3():
try:
print y
except:
print "cannot access global y"
print days
y = 25
print y
days=["monday", "tuesday"]
Artificial Intelligence – D. Vrajitoru