1 / 12

The Zen of Python

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.

Télécharger la présentation

The Zen of Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Zen of Python >>> import this

  2. What makes a good program?

  3. 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

  4. Miss Matthews’ Task • Input two values, height and width • Calculate (height*width)/2 and output the result • Simple …. but ….. • Everyone got it wrong. Why?

  5. Python division • What is 2/3? • Try it .. >>> print 2/3

  6. Import from the future from __future__ import division Or float(width) Now fix it!

  7. 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

  8. What’s wrong with this? def absolute_value(x): if x < 0: return -x if x > 0: return x Now do it!

  9. 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__

  10. Turn it into a module • Main code section def main(): #do stuff if __name__ == “__main__”: main() Try it out

  11. Writing a good program • Structure • Modules • Functions • Documentation strings • Highlighting the actual program

  12. The Zen of Python >>> import this

More Related