G-10 Lesson-1(Pep 8 Standards) Answer Key

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Answers of textbook exercises

Fill in the Blanks

1. Zen of Python contains T W E N T Y principles used in Python


programming.
2. PEP 8 suggests that lines should be limited to S EVENTY N
INE characters.
3. Line breaks around mathematical operators should occur BEFO
R E the operand.
4. You can use I N L I N E comment to document a single line of
code.
5. L I N T E R S are programs that analyze the source code for
semantic discrepancies.

State True or False

1. PEP 8 standards improve the readability of Python code. -True


2. Mix of space and tab can be used for indenting Python code. -False
3. As per PEP 8 standards, limit the line length of comments and
docstrings to 72 characters when adding comments. -True
4. Documentation strings appear on the last line of any function,
class, method, or module. -False
5. When there's more than one binary operator in a statement, add
whitespace around the operators with the highest priority. -False

Match the Following

1. Linter Flake 8

2. PEP 257 docstring


3. Automformatter Black

4. Hanging indent Vertical alignment

5. Characters in a line 79

Multiple choice questions

1. By following PEP 8, one can make sure that:


a. Variables are named properly.
b. Enough white space is added to follow logical steps in the
code.
c. Comments have been added to your code properly.
d. All of the above

2. Which of the following PEP documents covers rules applying to


docstrings?
a. PEP 8
b. PEP 257
c. PEP 20
d. PEP 572

3. As per PEP 8 standards , in which of the following situation(s)


would you avoid using white space?
a. Immediately inside parentheses,brackets, or braces.
b. Before a comma,semicolon or colon.
c. Before the open bracket that starts an index or slice.
d. All of the above

4. As per PEP 8, binary operators should be surrounded with:


a. Single space on either side.
b. Double space on either side.
c. No space on either side.
d. None of the above.

5. Which of the following is the outcome of Linting?


a. Helps to prevent bugs in your program.
b. Helps in creating better quality, readable code.
c. Saves our time as a developer.
d. All of the above.

Answer in one word or one sentence

1. What is the full form of PEP?


Python Enhancement Proposal

2. What is linting?
Linters are programs that analyze source code for symantec
discrepancies and flag errors.

3. What is an auto formatter?


Autoformatters are programs that refactor your code to conform
with PEP 8 automatically.

4. What is flake 8?
Flake 8 is a linting tool.

5. What is a block comment?


Use block comments to document a section of code. They are
useful to explain several lines of code, to perform a single action,
such as importing data from a file or updating a database entry.

Answer the following

1. What is PEP 8? What are the advantages of following PEP 8?


PEP 8 standards improve the readability of Python codes. Writing a
clear, readable code shows professionalism. By following PEP 8,
one can make sure that:
- Variables are named properly.
- Enough whitespace is added to follow logical steps in the
code.
- Comments are added to the code properly.
Programmers may need to collaborate with others. Writing a
readable code is crucial in this situation. Other people will have to
read and understand your code. Following PEP standards will make
it easier for others to read your code.

2. Explain the convention to be followed while naming variables,


functions, classes, modules, etc. as per PEP 8?

Function/ Use a lowercase word or words. find_sum(


Method Separate words by underscores to ),
improve readability. validate(
)

Variable Use a lowercase single letter, word, or x,


words. Separate words with length,
underscores to improve readability. student_a
ge

Class Use camel case, i.e. start each word Student,


with a capital letter. Words are UserAuthe
separated by making the first letter ntication
capital.

Constant Use an uppercase single letter, PI,


word, or words. Separate words USER_TYP
with underscores to improve E, THETA
readability.
Module Use a short, lowercase word or module1.
words. Separate words with py,
underscores to improve readability. student_
module.p
y

Package Use a short, lowercase word or Userutit


words. Do not separate words with lites,
underscores. student

3. Explain how to use vertical spaces to improve readability of


code, with examples.
Vertical spaces, also known as blank lines, can be used to separate
different parts of the code and improve its readability. For example,
we can use vertical spaces to separate the function definitions from
each other, or to separate different sections of the code.
Here is an example in Python:
def greet(name):
print("Hello, " + name + "!")

def calculate_area(length, width):


area = length * width
return area

def main():
name = input("Enter your name: ")
greet(name)

length = float(input("Enter the length of the


rectangle: "))
width = float(input("Enter the width of the rectangle:
"))
area = calculate_area(length, width)

print("The area of the rectangle is:", area)

if _name_ == "_main_":
main()
In the above example, we have used vertical spaces to separate the
function definitions from each other, making it clear where each
function starts and ends. We have also used a vertical space to
separate the main function from the if _name_ == "_main_": block,
making it clear that the main function is the entry point of the
program. By using vertical spaces to separate different parts of the
code, we can improve its readability and make it easier to
understand.

4. What is flake 8? How can you install and use flake 8? Explain
with examples.
Flake8 is one of the linting tools available. It is a great toolkit for
checking your code base against.
coding style (PEP8) and programming errors (like “library imported
but unused” and "Undefine
name”).
Use the following command to install flake8.
python<version> -m pip install flake8

Running flake8:
● Check particular file
○ flake8 path/to/code/code.py

● Check entire project


○ flake8 path/to/project/

Flake8 will list all the errors in the file if any.


Monishs-MacBook-Air:Desktop monish$ flake8 code.py
code.py:5:1: W391 blank line at end of file
code.py:5:1: W293 blank line contains whitespace
Monishs-MacBook-Air:Desktop monish$
5. Describe PEP 8 standards for docstrings and different types of
comments.
Block comments:
● Indent block comments to the same level as the code they
describe.
● Start each line with a # followed by a single space.
● Separate paragraphs by a line containing a single #.

Inline comments:
● Use inline comments sparingly.
● Write inline comments on the same line as the statement
they refer to.
● Separate inline comments by two or more spaces from the
statement.
● Start inline comments with a # and a single space, like block
comments.
● Don't use them to explain the block.

Docstrings:
● Surround docstrings with three double quotes on either side.
Ex: “”This is a docstring”””.
● Write docstrings for all public modules, functions, classes,
and methods.
● Put the “"" that ends a multi-line docstring on a line by itself.

You might also like