Python Exercise 3

Name of Innovation

Python Exercise 3

October 19, 2017 Uncategorized 0
-------> file name hello.py
def foo():
    print "Hello Amar Sharma"

if __name__ == "__main__":
 foo()
 Exercise: Take the following list, nlis, and compute its average. That is, write a function 'average(numlis)' that uses a 'for' loop to sum up the numbers in numlis and divide by the length of numlis. Just to be sure that you got all the numbers in numlis, print each one in your 'for' loop and print the length of the the list. nlis = [2,4,8,105,210,-3,47,8,33,1] # average should by 41.5 Exercise : Write a function multiply(num1,num2) which prints the multiplication of the two numbers. Careful to take care of the input type.

------------------- Sample for Debugging and Profiler ---------------------
import re

import sys 

import time

def if_statement():

 """ Three slightly difference versions of if: if, if-else, if-elif-else"""

 time.sleep(1)

 x = 5

 y = 0

 z = 0

 if x > 0:

     print "x is positive" 

 if y > 0:

     print "y is positive" 

 else:

     print "y is not positive" 

 # elif can be repeated as often as necessary 

 if z > 0:

     print "z is positive" 

 elif z < 0:

     print "z is negative" 

 else:

     print "z must be 0" 

def foo():

    print "Hello Amar Sharma"

def hello():

 """ prints hello, world """

 print("Hello, world!")

if __name__ == "__main__":

 foo()

 hello()

 if_statement()




Sample Output 
  

Anants-MacBook-Pro:pythonexamplesset1 amars$ python -m pdb /tmp/d.py 
> /tmp/d.py(1)<module>()

-> import re

(Pdb) n

> /tmp/d.py(2)<module>()

-> import sys

(Pdb) n

> /tmp/d.py(3)<module>()

-> import time

(Pdb) n

> /tmp/d.py(5)<module>()

-> def if_statement():

(Pdb) n

> /tmp/d.py(28)<module>()

-> def foo():

(Pdb) n

> /tmp/d.py(30)<module>()

-> def hello():

(Pdb) n

> /tmp/d.py(34)<module>()

-> if __name__ == "__main__":

(Pdb) n

> /tmp/d.py(35)<module>()

-> foo()

(Pdb) s

--Call--

> /tmp/d.py(28)foo()

-> def foo():

(Pdb) s

> /tmp/d.py(29)foo()

-> print "Hello Amar Sharma"

(Pdb) s

Hello Amar Sharma

--Return--

> /tmp/d.py(29)foo()->None

-> print "Hello Amar Sharma"

(Pdb) s

> /tmp/d.py(36)<module>()

-> hello()

(Pdb) s

--Call--

> /tmp/d.py(30)hello()

-> def hello():

(Pdb) s

> /tmp/d.py(32)hello()

-> print("Hello, world!")

(Pdb) s

Hello, world!

--Return--

> /tmp/d.py(32)hello()->None

-> print("Hello, world!")

(Pdb) s

> /tmp/d.py(37)<module>()

-> if_statement()

(Pdb) s

--Call--

> /tmp/d.py(5)if_statement()

-> def if_statement():

(Pdb) s

> /tmp/d.py(8)if_statement()

-> time.sleep(1)

(Pdb) s

> /tmp/d.py(9)if_statement()

-> x = 5

(Pdb) p x

*** NameError: NameError("name 'x' is not defined",)

(Pdb) n

> /tmp/d.py(10)if_statement()

-> y = 0

(Pdb) p x

5

(Pdb) h

Documented commands (type help <topic>):

========================================

EOF    bt         cont      enable  jump  pp       run      unt   

a      c          continue  exit    l     q        s        until 

alias  cl         d         h       list  quit     step     up    

args   clear      debug     help    n     r        tbreak   w     

b      commands   disable   ignore  next  restart  u        whatis

break  condition  down      j       p     return   unalias  where 

Miscellaneous help topics:

==========================

exec  pdb

Undocumented commands:

======================

retval  rv

(Pdb) q