a = None
b = None
a is b
a == b
a != b
a = 'hello world!'
b = 'hello world!'
print 'a is b :', a is b,', a is not b :',a is not b
print 'a == b :', a == b
We can write also : "not (logical expression)" :
print not a is b , not a == b
or and not
True or False
True and False
not True
'a' in ['a','b','c','d']
'x' in 'ooooooooxooooooo'
'y' in 'ooooooooxooooooo'
'not' can be inserted before 'in' operator, like in natural language :
'x' not in 'ooooooooxooooooo'
In the described expressions, the different zones are differentiated by the indentation.
Logical expression is delimited by ':'
Back to the previous indentation tells the end of the zone.
if 1 in [1,2,3]:
print "OK"
a = 10
if a < 100:
print "a is lower than 100"
else:
print "a is upper 100"
if a == 10:
print "a is 10"
else:
print "a is not 10"
if a == 1:
print "one"
elif a == 2:
print "two"
elif a == 3:
print "three"
else:
print "a out of domain"
names = ['Fabio', 'Karl', 'Marc', 'Olivier', 'Alan', 'Jean-Marc','Damien','David']
for name in names:
print name
for i in [1,2,3]:
print 'i:',i
range(1,10,1)
for i in range(1,4):
print 'i:',i
for i in xrange(1,4):
print 'i:',i
print 'range(1,4):',range(1,4)
print
print 'xrange(1,4):',xrange(1,4)
i = 0
while i < 10:
i += 1
print 'i:',i
i = 6
while i > 0:
i -= 1
print " ***",str(i).center(8),"***"
else:
print " *** Ignition ***"
for i in range(5,0,-1):
print ">>",i
else:
print ">> Go !!"
Stops the loop
for i in [1,2,3,4,5]:
if i >= 4:
print "Found breaking point : exiting"
break
print "i :",i
Go to next item
for i in range(1,100):
if i%10 != 0:
continue
print "i:",i
Does nothing in test but process the following commands
for i in range(1,10):
if i%3 == 0:
pass
else:
print 'i : ',i
print '***',i
for i,num in enumerate(['zéro','un','deux','trois','quatre','cinq']):
print num,':',i
squares = [x**2 for x in range(10)]
print squares
a = 15
x = 1 if a > 10 else 0
print 'x:',x
print 'introduction'[:5]
print 'intersection'[5:]
print "Hello"
print " %d is %s" % (42,'the answer')
x=42
print " {} is {}".format(x,'the answer')
nw = raw_input("Enter the right number: ")
if int(nw) != 5:
print "*** You did not find the right number. Try again ***"
else:
print "*** Congratulations, the right number is indeed",nw,"***"
f = open("toto","w")
for i,num in enumerate(['zéro','un','deux','trois','quatre','cinq']):
print >>f, num,':',i
f.close()
f = open("toto","r")
lines = f.readlines()
f.close()
for line in lines:
print line
f = open("toto","a")
for i,num in enumerate(['six','sept','huit']):
print >>f,num,':',i+6
f.close()
f = open("toto","r")
lines = f.readlines()
f.close()
for line in lines:
print line
import urllib2
response = urllib2.urlopen('http://physics.nist.gov/cuu/Constants/Table/allascii.txt')
for line in response:
if 'constant' in line :
print line
for i in range(1,6):
print '1/i-3:',1/(i-3)
print 'i*i:',i*i,'\n'
print A/0
A=1
print A/0
'2' + 3
f = open("notexistingFile","r")
for line in f:
print f
f.close()
None = 1
True = 2
False = 1
print False
print 3==4
print False
A=1
try:
print A/0
except ZeroDivisionError:
print "*** Error dividing by 0 ***"
print
try:
print A/0
except ZeroDivisionError,e:
print "*** ZeroDivisionError:",e
print
A = int(raw_input('Enter integer:'))
try:
print A/0
except ZeroDivisionError,e:
if A==1:
print "*** Error dividing by 0 ***"
else:
raise e
try:
x = int(raw_input('Please enter a number: '))
finally:
print('Thank you for your input')
def square(x):
return x*x
assert square(2)==4,"Assertion 1 : not verified"
print ".."
assert square(2)==-1,"Assertion 2 : Wrong output for square function !!"
print ".."