120 likes | 236 Vues
Dive into the fundamentals of programming with Python through engaging exercises and clear concepts. This guide addresses common pitfalls such as division errors, presents key programming structures including functions and modules, and emphasizes the importance of proper documentation. You will learn to create a simple function to calculate area and discover how to improve your coding skills before your upcoming exams. Embrace 'The Zen of Python' philosophy to write better, more elegant code.
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