Blog

Python: Statements, Indentation and Variables

Python Statement

Instructions that a Python interpreter can execute are called statements. For example, A = 5 is an assignment statement.

In Python, the end of a statement is marked by a newline character. But we can make a statement extend over multiple lines with the line continuation character (\).

For Example:      

This is an explicit line continuation. In Python, line continuation is implied inside parentheses ( ), brackets [ ], and braces { }. 

We can also put multiple statements in a single line using semicolons, as follows: a = 1; b = 2; c = 3

Python Indentation

Most of the programming languages like C, C++, and Java use braces { } to define a block of code. Python, however, uses indentation. A code block (body of a function, loop, etc.) starts with indentation and ends with the first unindented line. The amount of indentation is up to you, but it must be consistent throughout that block. The enforcement of indentation in Python makes the code look neat and clean. This results in Python programs that look similar and consistent. It makes the code more readable.

For Example:

Both are valid and do the same thing, but the former style is clearer.

Variables

A variable is a named location used to store data in the memory. It is helpful to think of variables as a container that holds data that can be changed later in the program.

For Example:

Here, we have created a variable named number. We have assigned the value 10 to the variable. You can think of variables as a bag to store books in it and that book can be replaced at any time.

Assigning values to Variables

You can use the assignment operator = to assign a value to a variable.

The statement assigns the integer value 20 to the variable x. Second statement prints the variable x’s current value.

 Assigning multiple values to multiple variables

Assigning same values to different variables

If we want to assign the same value to multiple variables at once, we can do this as:

Leave a Reply

Your email address will not be published. Required fields are marked *