blog image

What is a Statement in Python?

A statement is a piece of code that can be executed by a Python interpreter. So, to put it simply, anything python - Based is a statement.

The token NEWLINE character is used at the end of a Python statement. In a Python script, this indicates that each line is a statement.

An assignment statement, for instance, is a = 10. where a is the name of the variable and 10 is its amount. Other types of statements, such as if statements, for statements, while statements, and so on, will be covered in the classes that follow.

Print statements, Assignment statements, Conditional statements, and Looping statements are the four primary types of statements in Python.

Typically, print or assignment statements are used. A value is returned by a print statement. Assignment statements do not yield a result; instead, they assign the value to an operand on the left side of the statement.

A Python script is typically made up of a series of statements. If there are many statements, the result is only displayed once when all of them are executed.

Multiline Statement

The token NEWLINE character is used at the end of a Python statement. However, we can use the line continuation character to extend the sentence across many lines (\). An explicit continuation is what this is called.

Implicit Continuation

To create a multi-line statement, we could use parentheses (). Inside it, we can put a line continuation statement. Even if we put something inside the parenthesis (), it will be treated as a single statement, even if it is on many lines.

To make a list, we could use square brackets []. Then, if necessary, each list item can be placed on a separate line for easier reading.

We could use curly, just like square brackets, to build a dictionary with each key-value combination on a separate line for easier reading.

Compound Statement

  • Compound statements are statements that include (groups of) similar statements and affect or control how those other statements are executed in some way.
  • The conditional, as well as loop statements, are included in the compound statement.
  • If statement - If the condition is true, it is a control flow statement that will run the statements under it. Also goes by the conditional statement.
  • While statement - The while loop statement performs a code block continuously while a specific condition is true. It’s also called a looping statement.
  • For statement - We may repeat any series or iterable variable using the for loop statement. A string, list, dictionary, set, or tuple can be used as the sequence.
  • Try statement - Exception handlers are specified using the try statement.
  • With statement - The with statement is used to cleanup code for a set of statements, and it also allows initialization and finalisation code to be executed around a block of code.

What is an Indentation in Python?

Braces are used to mark a block of code in most computer languages, including C, C++, and Java. Python embraces indentation.

Indentation begins with the first unindented line and ends with the first indented line in a code block (body of a function, loop, etc.). You can choose the amount of indentation, but it has to be uniform across the block.

Indentation is usually done with four whitespaces and is favoured over tabs. Line continuation can be done without indentation, however, it's always a great idea to indent.

What does Comment Refer to in Python?

When writing a programme, comments are crucial. They explain what happens inside a programme so that someone viewing the source code doesn't have trouble figuring it out.

In a month, you may forget the crucial aspects of the software you just built. As a result, taking some time to clarify these ideas through comments is always beneficial.

To begin creating a comment in Python, we utilise the hash (#) symbol.

It goes all the way to the newline character. Comments help programmers understand a process better. Comments are often overlooked by the Python interpreter.

Multiline Comments

We can have comments that span many lines. Using the hash(#) sign at the start of each line is one method.

The usage of triple quotations is another option.

For multi-line strings, triple quotes are commonly employed. They can, however, be used as multi-line comments. They don't create any extra code unless they're docstrings.

Python's Docstrings

The term "docstring" refers to a documentation string.

The string literals that occur immediately after the declaration of a function, method, class, or module are known as Python docstrings (documentation strings).

When writing docstrings, triple quotes are utilised.

Docstrings appear after a function, class, or module has been defined. Using triple quotes, divide docstrings from multiline comments.

The object's __doc__ attribute is used to correlate the docstrings with it.