latn.blogg.se

Writing code in python
Writing code in python












writing code in python

Generally, comments should describe what or why you are doing something, rather than how.Ĭomment any potentially confusing code, or better yet, rewrite the code so that it isn't confusing. Be careful to update the comments when you update the code.ĭo not write comments that merely restate the code. Make sure that comments agree with the code. Good programmers write code that documents itself. There is no widely agreed upon set of rules. Any text from the # to the end of the line is ignored. Python ignores comments.Ī comments begins with # (the pound or number-sign character) and ends at the end of the line on which the # appears. Comments can appear anywhere within a program where whitespace is allowed. As a general rule, the code explains to the computer and programmer how the program works the comments explain to the programmer what the program does. Programmers use comments to annotate a program and help the reader (or grader) understand how and why your program works. Begin with a lowercase letter and use camelCase for Use all capital letters and separate internal wordsĪ noun that communicates what the class represents.īegin with an uppercase letter and use camel case forĪ verb that communicates what the function or methodĭoes. Begin with a lowercase letterĪnd use camel case (mixed case, starting with lower case). To the casual observer what the variable represents, Name a constant variable by its meaning, not its value, e.g., name your variable DAYS_PER_WEEK instead of SEVEN.Ī short, but meaningful, name that communicates

writing code in python

Use terminology from the application domain when possible. Use more descriptive names for variables that serve an important purpose.Īvoid generic names like foo or tmp and meaningless names like fred. Use shorter names (e.g., i) for short-lived variables and loop-index variables. Name boolean variables, functions, and methods so that their meaning is unambiguous, e.g., isPrime or isEmpty() or contains(). Use polygon instead of p or poly or pgon.

writing code in python

For example, use wagePerHour or hourlyWage instead of wph. Choose names that are easy to pronounce, and avoid cryptic abbreviations.

writing code in python

Use meaningful names that convey the purpose of the variable, function, method, or class. When choosing names for your variables, functions, methods, and classes: Use straightforward logic and flow-of-control.Īvoid magic numbers (numbers other than -1, 0, 1, and 2) instead, give them meaningful symbolic names. Keep programs, functions, and methods short and manageable. This page complements the official Style Guide for Python Code by providing some recommendations that are appropriate for those who are new to computer programming.














Writing code in python