diff --git a/chapter2_exercises.py b/chapter2_exercises.py index 38647d5..7f460ce 100644 --- a/chapter2_exercises.py +++ b/chapter2_exercises.py @@ -1,3 +1,26 @@ # Exercises for chapter 2: -# Name: \ No newline at end of file +# Name:Shishira Adiga + +#Exercise 2.1--appending 0 indicates octal system hence outputs octal value of the integer typed. + +#Exercise 2.2 +print 5 +#print x = 5 --this as per textbook produceses syntax error +x = 5 +print x+1 +#this code outputs 5 and 6 + +# Exercise 2.3 +#1.width/2 -- value = 8;type = integer. +#2.widht/2.0 -- value = 8.5;type = float. +#3.height/3 -- value =4.0 ; type = float. +#4.1+2*5 -- value = 11; type=integer. +#5.delimiter*5 -- value = .....;type = string; + +#Exercise 2.4 +#1--4/3.0*pi*5**3 = 523.333 +#2--$1574.2 +#3--7 hours 30 mins 6 seconds + + diff --git a/chapter3_exercises.py b/chapter3_exercises.py index 67d3fa6..cf5765d 100644 --- a/chapter3_exercises.py +++ b/chapter3_exercises.py @@ -1,3 +1,73 @@ # Exercises for chapter 3: -# Name: \ No newline at end of file +# Name:Shishira Adiga + +#3.1 +'''def print_lyrics(): + print "I'm a lumberjack, and I'm okay." + print "I sleep all night and I work all day." + + def repeat_lyrics(): + print_lyrics() + print_lyrics() + + repeat_lyrics()''' + +#3.1 +#NameError: 'repeat_lyrics' is not defined + +#3.2 +#I'm a lumberjack, and I'm okay. +#I sleep all night and I work all day. +#I'm a lumberjack, and I'm okay. +#I sleep all night and I work all day. +#question--how come you get the right answer?since you're calling function print_lyrics before defination. + +#3.3 +def right_justify(s): + b=len(s) + print(' '*(70-b)+s) + +right_justify('time') + +#3.4 +#1 +def do_twice(f): + f() + f() + +def print_spam(): + print 'spam' + +do_twice(print_spam) + +#2 +def do_twice(f,g): + f(g) + f(g) + +def print_twice(s): + print s*2 + +do_twice(print_twice,'start') + +#3 +def print_twice(s): + print s*2 + +print_twice('start') + +#4 +def do_four(f,g): + do_twice(f,g) + do_twice(f,g) + +def do_twice(f,g): + f(g) + f(g) + +def print_twice(s): + print s*2 + + +do_four(print_twice,'start')