AmitDiwan has Published 11365 Articles

Coding standards (style guide) for Python programs?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2022 11:04:44

4K+ Views

The coding standards i.e., the style guide for Python are provided by a document called PEP8. The PEP8 is Python Enhancement Proposal 8. It is a document that provides coding conventions for the Python code. Here’s the style guide − Naming Conventions The following are the currently recommended naming standard. ... Read More

How do I test a Python program or component?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2022 11:03:15

2K+ Views

To test a Python program, use the unittest module in Pygthon. The doctest and unittest modules or third-party test frameworks can be used to construct exhaustive test suites that exercise every line of code in a module. The doctest module The doctest module searches for pieces of text that look ... Read More

Why does Python allow commas at the end of lists and tuples?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2022 11:00:55

3K+ Views

Python allow commas at the end of lists and tuples. It is optional and makes the items more readable and you can reorder the items without getting any error. You don’t need to remember again and again to add a trailing comma after every item, if you add a comma ... Read More

Why are colons required for the if/while/def/class statements in Python?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2022 10:59:48

587 Views

The colon is required for all these keywords, if, while, def, class, etc in Python to enhance readability. The colon makes it easier for editors with syntax highlighting i.e. they can look for colons to decide when indentation needs to be increased. Let’s see an example of an if statement ... Read More

Why doesn’t Python have a “with” statement for attribute assignments?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2022 10:59:02

119 Views

Python definitely has a with statement. It wraps the execution of a block, calling code on the entrance and exit from the block. Some languages have a construct like the below − with obj: a = 1 total = total + 1 The ... Read More

Why can’t raw strings (r-strings) end with a backslash in Python?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2022 10:58:04

941 Views

The r in r-strings means raw strings. String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences. When an 'r' or 'R' prefix is present, a character following a backslash is included in ... Read More

Why is there no goto in Python?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2022 10:57:19

11K+ Views

Yes, there is no goto statement in Python. Let us first understand what is a goto in C Language. However, the usage of goto is also discouraged in C. The goto statement in C programming provides an unconditional jump from the 'goto' to a labelled statement in the same function. ... Read More

How do you specify and enforce an interface spec in Python?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2022 10:54:24

101 Views

Let us first see what is an interface spec. The interface spec is an interface specification for a module provided by programming languages such as C++ and Java. It describes the prototypes for the methods and functions of the module. The abc module The abc module introduced in Python 2.6 ... Read More

Why doesn’t list.sort() return the sorted list in Python?

AmitDiwan

AmitDiwan

Updated on 20-Sep-2022 10:53:24

354 Views

Example In this example, let’s first see the usage of list.sort() before moving further. Here, we have created a List and sorted in Ascending order using the sort() method − # Creating a List myList = ["Jacob", "Harry", "Mark", "Anthony"] # Displaying the List print("List = ", myList) ... Read More

Why must dictionary keys be immutable in Python?

AmitDiwan

AmitDiwan

Updated on 19-Sep-2022 14:25:30

1K+ Views

To understand why dictionary keys must be immutable. Let us related it to hash table. The hash table implementation of dictionaries uses a hash value calculated from the key value to find the key. If let’s say the key was a mutable object, its value could change, and thus its ... Read More

Advertisements