How to write a Python statement, indentation, and comment

How to write a Python statement, indentation, and comment

In this article, we will discuss “How to write a python statement”. Let’s dive deep into this.

Introduction

People widely use Python, a popular and powerful programming language, in various fields such as web development, data analysis, and scientific computing. Its simplicity and ease of use make it an ideal language for both beginners and experienced programmers. Let’s move to see how to write a python statement.

What is python?

Python is a high-level scripting language used in many fields. It uses indentation, statement and comment syntax which helps us write more readable codes. The designers of the language designed it to make programs more readable and to have clear semantics.

Python uses English keywords instead other languages using punctuation. It has very fewer syntactic structures than other languages which enable clear programming on both small and large scales. In 1991, Guido van Rossum created Python, and since then the Python Software Foundation has developed it. It can run on Windows, Linux/Unix, Mac OS X or even mobile phones (such as iPhone).

What is a statement in Python?

Statements are the commands and instructions that you can use to control the flow of your program. Besides statements, they are also called comments or expressions. Python has many different kinds of statements.

If/then/elif – This is the most common kind of conditional statement in Python. The compiler uses the if statement to check if something is true or false in code and then only executes another block if it is true. For example:

if 1 == 1: print('Yes')
if 2 == 2: print('No')

fruitList = ['banana','apple','orange']
item = input()
if item in fruitList:
    print('Yes',item, 'is present in FruitList')
else: print('No',item, 'is not present in FruitList')Code language: Python (python)

Output

Yes
No
>>>guava
No guava is not present in FruitListCode language: Python (python)

Types of statements

In Python, the statement is one of the key concepts. A statement is a piece of code that performs a specific task or operation. Python has various types of statements. Such as control statements, assignment statements and import statements.

Python’s basic statements include:

  • Declaration, which defines a variable or constant value
  • Assignment statement, to assign one value to another(such as a=2)
  • Printing the value of an expression (print(x))

Conditionals are statements that change the flow of execution based on some condition. They can be simple or complex and use any combination of if-else statements and loops to perform actions based on logical rules.

Multi-Line Statements(syntax and example)

A multi-line statement is a Python statement that contains multiple statements on the same line.

In Python, you can write multiple statements on the same line using a semicolon (;). However, I will not recommend this as it makes your code less readable. Further, a more common way to write multiple statements is to use multiple lines.

The syntax for a multi-line statement is:

x = 1; y = 2; z = 3;
print(x,y,z)
# but this method is not recommendedCode language: Python (python)

Output

1 2 3Code language: Python (python)

In this example, we have three statements on one line.

# Instead use this method

p = 1
q = 2
r = 3
print(p,q,r)Code language: Python (python)

Output

1 2 3Code language: Python (python)

This code assigns the values 1, 2, and 3 to the variables p, q, and r, respectively. The statements are executed in the order they are written, and each line is a separate statement.

To make it easier to read, you can indent each of your lines by using curly braces, parentheses and brackets also. If you wish to use curly brace syntax in your code but don’t want them at the beginning of each sentence (as in this example), then simply place an empty pair of parentheses after each bracketed block:

#using curly braces
s = {1*2*3
    *'hi'*2
    }
print(s)

#use brackets
s = ["Hi how",
"are you?"]
print(s)

#use of parentheses
s = (1+2+3+6+
7+10*
2+4+5)
print(s)Code language: Python (python)

Output

{'hihihihihihihihihihihihi'}
['Hi how', 'are you?']
48Code language: Python (python)

What is Indentation in python?

Indentation is a key concept in Python. It refers to the use of a whitespace at the beginning of a line to indicate the level of code organization. In Python, blocks of code, such as the body of a function or loop, can be indicated using indentation.

Although, Indentation in blocks or functions is used to group them and to identify the block of code that is being executed. For example:

def greet(name):
    print("Hello, " + name)Code language: Python (python)

This code defines a function called “greet” that takes a single argument, “name”. After that, the body of the function is indented one level, indicating that it belongs to the function definition.

What are Comments in python? 

In Python, comments are lines of text in a program that are ignored by the interpreter. They are used to provide explanations or notes about the code and can help understand the code or for remembering the purpose of certain lines of code.

Types of Comments

Comments are lines of code that the Python Interpreter ignores. You can use them to provide explanations or notes in the code. Further, They can also be helpful for other programmers reading and understanding your code.

There are three types of comments in Python:

  • Single-line comments
  • Multi-line comments
  • Documentation comment

Single-line comments

To add a single-line comment, you need to put the # symbol at the beginning of your line. This will cause Python to ignore what you have written and instead treat it as if it were part of an existing statement.

You can place your single-line comment anywhere on the line, or multiple lines if needed. Moreover, the only restriction is that there must be no whitespace between the opening character (the hash) and any subsequent characters for this construct to be the valid syntax for Python functionality; Otherwise, the interpreter will treat it as an error.

#This is a single-line comment.Code language: Python (python)

Multi-line comments

You can use multi-line comments to add extra details and explanations to your code. These start with a hash symbol (#) and end with a newline character. Besides, Multi-line comments can be used to temporarily remove multiple lines of code from being executed.

"""
This is a multi-line comment.
It can be used to provide more detailed explanations or notes about the code.
"""Code language: Python (python)

Documentation comment

Docstrings are specifically designed to provide documentation for functions or modules in Python. They are written as the first statement in a function or module and are enclosed in triple quotes. Docstrings are multi-line comments.

  • The first line of a documentation string can be indented up to three spaces but must be preceded by a blank line.
  • The rest of the docstring may be indented any amount you like.
def greet(name):
    """
    This is a docstring. It provides documentation for the greet function.
    The function takes a single argument, 'name', and prints a greeting.
    """
    print("Hello, " + name)
name = input("Enter name: ")
x = greet(name)
print(x)Code language: Python (python)

Output

Enter name: Codedamn
Hello, CodedamnCode language: Python (python)

Docstrings are useful for providing information about the purpose, arguments, and return value of a function or module. Other parts of the code or tools that extract documentation from the code can access docstrings.

Here are two examples that include all of them

Here are two examples of Python statements:

# a  single-line statement

"""
This is a 
multi-line comment.
"""

class Node:
    """
    Create a Node and print it. 
    """
    def __init__(self,data):
        self.data = data
        self.next = next
        
    def __repr__(self):
        return "<Node data is: %s>" % self.data
           
N1 = Node(10)
N2 = Node(20)
N1.next = N2
print(N1)
print(N1.next)Code language: Python (python)

Output

<Node data is: 10>
<Node data is: 20>Code language: Python (python)
"""
Create a linked list and check if it is empty.
"""
class linkedList:
    """
    Singly linked list
    """
    def __init__(self):
        self.head = None
    
    #check if it is empty 
    def isEmpty(self):
        if self.head !=None:
            return None
        return 1 

x = linkedList()
x.head = N1
print(x.isEmpty())Code language: Python (python)

Output

NoneCode language: Python (python)

Where we use it

  • Python is used to write programs.
  • It’s also used to make games and websites, apps, robots, movies and music.
  • You can even use it if you want to learn how computers work or just have fun!

Conclusion

I hope that you have enjoyed this article and found it informative. Now, You must know how to write python statement and indentations and comments. Feel free to comment if you have any issues. For more information, you can check this link.

Frequently Asked Questions

How to write a statement in python?

To write a statement in Python, you simply type it out.

number = 10Code language: Python (python)

Why is important indentation and comment in python?

Indentation and comments are important in Python because they help to make your code more readable and easier to understand.

What is an indentation comment?

Indentation is used to indicate the block of code that belongs to a particular control structure, such as a for loop or an if statement. Comments are used to add notes and explanations to your code.

How do you write indentation in python?

To write an indentation in Python, you simply need to use a certain number of spaces or tabs at the beginning of a line of code.

for i in range(10):
    print(i)Code language: Python (python)

Output

0 1 2 3 4 5 6 7 8 9Code language: Python (python)

How do you write up a statement?

A statement can be a simple expression, such as assigning a value to a variable. You can simply type it out to write a statement.

Sharing is caring

Did you like what Srishti Kumari wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far