>>> def my_function():
... print "this is my function"
...
>>> my_function()
this is my function
>>> def print_stars():
... print " *"
... print "* *"
... print " * "
...
>>> print_stars(); print_stars()
*
* *
*
*
* *
*
def print_n_stars(i):
while i > 0:
print "*",
i = i - 1
print ""
>>> print_n_stars(1)
*
>>> print_n_stars(5)
* * * * *
def add_one(i):
return i + 1
>>> print add_one(5)
6
>>> i = add_one(7)
>>> print i
7
>>> str = "5 plus 1 is " + add_one(5)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects
>>> range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(0, 10, 2)
[0, 2, 4, 6, 8]
>>> i = 0
>>> for j in range(0, 10):
... i = i + j
...
>>> print i
45
*
**
***
*****
******
*******
********
*********
>>> def confusion(i):
... i = i + 1
... print "i: ", i
...
>>> i = 3
>>> confusion(i)
i: 4
>>> print i
3
>>> i = 0
>>> while i < 2:
... for i in range(0, 2):
... print "*",
... print
... i = i + 1
...
* *
>>> def print_stars(i):
... for i in range(0, i):
... print "*",
... print
...
>>> i = 0
>>> while i < 2:
... print_stars(2)
... i = i + 1
...
* *
* *