130 likes | 312 Views
The Zen of Python. >>> import this. What makes a good program?. Where we are. Mock exam next week, period 4 Will be screen based, requiring coding Then five and a half weeks between half term and Easter. And three weeks before your real exam – Monday 11 th May.
E N D
The Zen of Python >>> import this
Where we are • Mock exam next week, period 4 • Will be screen based, requiring coding • Then five and a half weeks between half term and Easter. • And three weeks before your real exam – Monday 11th May Most of you need practice. Just practice. So please come on Wednesday
Miss Matthews’ Task • Input two values, height and width • Calculate (height*width)/2 and output the result • Simple …. but ….. • Everyone got it wrong. Why?
Python division • What is 2/3? • Try it .. >>> print 2/3
Import from the future from __future__ import division Or float(width) Now fix it!
Turn this into a function • Objective …. • Create a function • With two arguments – length and width • Get the value back, (length*width) / 2 • Need the keyword return
What’s wrong with this? def absolute_value(x): if x < 0: return -x if x > 0: return x Now do it!
The docstring def myweirdfunction(length): “””Draws a line of breadcrumbs which the cursor has to follow … takes single integer argument length””” #dostuff Print myweirdfunction.__doc__
Turn it into a module • Main code section def main(): #do stuff if __name__ == “__main__”: main() Try it out
Writing a good program • Structure • Modules • Functions • Documentation strings • Highlighting the actual program
The Zen of Python >>> import this