In [1]:
x = 2
print x == 2 # prints out True
print x == 3 # prints out False
print x < 3 # prints out True
In [8]:
Out[8]:
In [11]:
name = "John"
age = 23
if name == "John" and age == 23 :
print "Your name is John, and you are also 23 years old."
if name == "John" or name == "Rick":
print "Your name is either John or Rick."
In [17]:
age=50
if age<0:
print('age<0')
elif age <50:
print('age<50')
else:
print('age>=50')
In [18]:
age = input("Age of the dog: ")
print
if age < 0:
print "This can hardly be true!"
elif age == 1:
print "about 14 human years"
elif age == 2:
print "about 22 human years"
elif age > 2:
human = 22 + (age -2)*5
print "Human years: ", human
###
raw_input('press Return>')
In [25]:
a=3
b=2
max1 = 1 if (a == 1) else 0
print max
In [10]:
In [18]:
if name in ["John", "Rick"]:
print "Your name is either John or Rick."
In [24]:
age = 6
if age in [x for x in range(10)]:
print age
In [4]:
x = 2
if x == 2:
print "x equals two!"
else:
print "x does not equal to two."
In [5]:
x = [1,2,3]
y = [1,2,3]
print x == y # Prints out True
print x is y # Prints out False
In [6]:
print not False # Prints out True
print (not False) == (False) # Prints out False
In [25]:
primes = [2, 3, 5, 7]
for prime in primes:
print prime
In [39]:
xs = [100,200,300,400]
xs2 = []
for x in xs:
xs2.append(x+1)
In [41]:
for x in range(len(xs)):
print xs[x]
In [50]:
range(6,-1,-1)
Out[50]:
In [73]:
# Prints out the numbers 0,1,2,3,4
for xfaas in xrange(5): # or range(5)
print xfaas
# Prints out 3,4,5
for x in xrange(3, 6): # or range(3, 6)
print x
# Prints out 3,5,7
for x in xrange(3, 8, 2): # or range(3, 8, 2)
print x
In [60]:
# Prints out 0,1,2,3,4
x=[]
count = 0
while count < 5:
count += 1 # This is the same as count = count + 1
x.append(count)
In [61]:
x
Out[61]:
In [62]:
# Prints out 0,1,2,3,4
count = 0
while True:
print count
count += 1
if count >= 5:
break
In [63]:
# Prints out only odd numbers - 1,3,5,7,9
for x in xrange(10):
# Check if x is even
if x % 2 == 0:
continue
print x
In [64]:
phonebook = {
"John" : 938477566,
"Jack" : 938377264,
"Jill" : 947662781
}
In [72]:
for x, y in phonebook.iteritems():
print "Phone number of %s is %d" % (x, y)
In [70]:
for key in phonebook.iterkeys():
print key
In [78]:
for x in range(1,10):
for y in range(1,10):
print(x * y)