Python Statements: Definition, Types & How to Use Them

There are different entities that come together in the source code to make a successful execution of a program. Each entity is important to get the execution transcended from one line of code to another. In this article, we shall explore in detail about a particular entity known as Statements in Python.

Also Read: An Introduction to Python Comments


Defining Statements in Python

Strings of texts or logical conditions that are within the source code of a program are termed as Statements in Python. They provide a specific set of instructions that shall channel the flow of executing the codes in a Python program.

These Statements are pre-defined within the Python library to carry out a specific task, for instance, the IF statement decides the course of code execution on whether a particular condition is met or not. Now, let us have a look at the different types of Statements available in Python programming.


Types of Statements in Python

Statements in Python can be broadly classified into, but not limited to the following categories.

  • Conditional Statements – Used in verifying whether a particular condition is met or not
  • Arithmetic Statements – Used in executing various arithmetic operations such as addition, subtraction, multiplication and division
  • Function Statements – Used in the creation of user-defined functions for carrying out exclusive tasks
  • Assigning Statements – Used in assigning any particular value to a given variable
  • Looping Statements – Used in executing a particular set of instructions in multiple iterations until the desired outcome is obtained

Python also provides us with a feature to extend these Statements using either of the following depending on which best serves the purpose.

  • Parentheses ( )
  • Brackets [ ]
  • Braces { }
  • Semicolon :
  • Continuation character \

For those who begin to wonder how do any of these extension techniques work? The next section bears the answer to your question. It also goes beyond with a listing of furthermore examples to convey a deeper understanding of the Statements in Python programming.


Using Statements in Python

Let us start things off by understanding how an assigning statement works & then demonstrate how the continuation character can be put into use while constructing a simple statement in Python.

Following is a set of codes to calculate a variable x1.

x1 = 1 + 3 +\
     5 + 7 +\
     9 - 2
print(x1)
Extending Python Statements Using \
Extending Python Statements Using \

The line of code which is extended by using ‘\’ where the value of ‘x1’ is set to the resulting value of the arithmetic operations is a typical example of assigning statement. Besides, the above example also depicts how the arithmetic operations are carried forward to the next line by the usage of the continuation character ‘\’.

Moving on, we shall now have a look at how the extension can be done using brackets. To know that, let us try printing a set of text strings as shown below.

currencies = ['US Dollar',
              'Euro',
              'Dirham',
              'Dinar',
              'Riyal']
print(currencies)
Extending Python Statements Using []
Extending Python Statements Using [ ]

It is time to have a look at extending the statements using a semicolon ‘;’. Following is an example where we are going to assign some client details using the ‘;’ extension & then refer them for printing a string of text.

Name = 'Bulbul'; Age = 49; E_Code = 2345457           
print("Client", Name, "whose code is",E_Code, "has an age of", Age)
Extending Python Statements Using ;
Extending Python Statements Using ;

To conclude let us have a look at a looping statement which prints each entity assigned to a particular variable till it reaches the last one.

currencies = ['US Dollar',
              'Euro',
              'Dirham',
              'Dinar',
              'Riyal']              
for x in currencies:
              print(x)
Construction of a Looping Statement In Python
Construction of a looping statement in Python

Conclusion

Now that we have reached the end of this article, hope it has elaborated on what are statements and how to effectively use them in Python. Here’s another article that can be your definitive guide to the comments in Python. There are numerous other enjoyable and equally informative articles in CodeforGeek that might be of great help for you to gain valuable insights. Until then, ciao!


Reference

https://docs.python.org/3/reference/simple_stmts.html

Arulius Savio
Arulius Savio
Articles: 26