Lazy Programming Series – Shorthand If-else notation & Docstrings
In Python, you can use a shorthand if-else notation called a ternary conditional operator. It has the following syntax:
Here’s an example:
In this example, if x
is divisible by 2 (i.e., even), it will print “Even”; otherwise, it will print “Odd”.
Functions and Docstrings:
Docstrings in Python are used to document functions, classes, modules, or methods. They are written as a string literal that appears as the first statement in a module, function, class, or method definition.
Here’s an example of a function with a docstring:
It’s good practice to include informative docstrings in your code to make it easier for others (and yourself) to understand its purpose and usage.