Expressions and Indentation of Python Statements

  • 2021-12-04 10:46:27
  • OfStack

Directory 1. Statement 1.1 in Python What is a declaration?
1.2 What is Expression 1.3 Simple Assignment Statements 1.4 Enhanced Assignment Statements 2. Multiline Statements in Python 2.1 Explicit Continuation 2.2 Implicit Continuation 3. Python Indentation 3.1 How many spaces are there in an indentation in Python? 3.2 Why is indentation important?

1. Statements in Python

1.1 What is a statement?

Python The statements in are logical instructions that the Python interpreter can read and execute. In Python Which can be an expression or an assignment statement.

The assignment statement is Python The foundation of. It defines how expressions create and save objects.

1.2 What is an expression

An expression is a type of Python statement that contains a logical sequence of numbers, strings, objects, and operators. The value itself is a valid expression, as is the variable.

Using expressions, we can perform addition, subtraction, connection and other operations. It can also call functions that evaluate results.

Examples:


#  Use arithmetic expressions 
>>> ((10 + 2) * 100 / 5 - 200)
40.0

#  Using functions in expressions 
>>> pow(2, 10)
1024

#  Use in an expression  eval
>>> eval( "2.5+2.5" )
5.0

1.3 Simple assignment statements

In a simple assignment, we create new variables, assign values and change values. This statement provides an expression and a variable name as a label to hold the value of the expression.


# Syntax
variable = expression
# LHS <=> RHS

Now let's take a closer look Python To see what happened.

Case 1: The right side (RHS) is just a value-based expression.

Let's consider the most basic assignment form in Python under 1.


>>> test = "Learn Python"

Python will create 1 string in memory " Learn Python "And assign a name to it" test ". You can use a built-in function called id () to confirm the memory address.


>>> test = "Learn Python"
>>> id(test)
6589040

This number is the address where the data is located in memory. Now, here's one interesting thing you should know.

If you create another string with the same value, Python creates a new object and allocates it to a different location in memory. So this rule applies to most cases.

>>> test1 = "Learn Python"
>>> id(test1)
6589104
>>> test2 = "Learn Python"
>>> id(test2)
6589488

However, Python also allocates the same memory address in the following two scenarios.

The string has no spaces and contains less than 20 characters.
If the integer ranges from-5 to +255.
This concept is called Interning . Python This is done to save memory.

Case 2: On the right (RHS) is the current Python variable.

Let's discuss the next type of assignment statement, where RHS Is the current Python Variables.


>>> another_test = test

The above statement does not trigger any new allocations in memory. Both variables point to the same memory address. This is like creating an alias for an existing object. Let's use the id () function to verify this 1 point.


>>> test = "Learn Python"
>>> id(test)
6589424
>>> another_test = test
>>> id(another_test)
6589424

Case 3: Right side ( Python 0 ) is an operation.

In this type of statement, the result will depend on the result of the operation. Let's analyze it with the following example.


>>> test = 2 * 5 / 10
>>> print(test)
1.0
>>> type(test)
<class 'float'>

In the above example, the assignment results in the creation of a "float" variable.


>>> test = 2 * 5
>>> print(test)
10
>>> type(test)
<class 'int'>

In this example, the assignment will result in the creation of a " int "Variables.

1.4 Enhanced assignment statements

We can combine arithmetic operators in assignment to form extended assignment statements.

Check out the following example for enhanced assignment statements.


x += y

The above statement is short for the following simple statement.


# Syntax
variable = expression
# LHS <=> RHS

0

The next one is a clearer example. We add new elements to tuples.


# Syntax
variable = expression
# LHS <=> RHS

1

The next example uses a vowel list. It is demonstrating adding missing vowels to the list.


# Syntax
variable = expression
# LHS <=> RHS

2

2. Multiline statements in Python

Usually, every Python Statements end with a line break. However, we can use the line continuation character () to extend it to multiple lines.

Python Provides us with two ways to enable multi-line statements in our program.

2.1 Explicit Line Continuation

When you immediately use the line continuation character () to split the statement into multiple lines.

Examples:


#  Initialize a list with a multiline statement 
>>> my_list = [1, \
... 2, 3\
... ,4,5 \
... ]
>>> print(my_list)
[1, 2, 3, 4, 5]

#  Evaluate expressions using multiline statements 
>>> eval ( \
... " 2.5 \
... + \
... 3.5")
6.0

2.2 Implicit Line Continuation

Implicit line continuation is a split statement using any one of parentheses (), square brackets [], and curly braces {}. You need to enclose the target statement using the construction mentioned.

Examples:


# Syntax
variable = expression
# LHS <=> RHS

4

Another example:


# Syntax
variable = expression
# LHS <=> RHS

5

3. Python Indentation

Many high-level programming languages (such as C, C + +, C #) use curly braces {} to mark code blocks. Python is implemented by indentation.

A code block representing the body of a function or loop begins with an indentation and ends with the first unindented line.

3.1 How many spaces are there for indentation in Python?

Python The Style Guide (ES 166EN 8) states that the indentation size should be kept at 4. However, Google has its own unique style guide, which limits indentation to up to two spaces. So you can also choose different styles, but we recommend following PEP 8.

3.2 Why is indentation important?

Most programming languages provide indentation for better code formatting and do not enforce it.

However, in Python Indentation rules must be observed. Usually, we indent every 1 line in the code block by 4 spaces (or the same amount).

In the examples in the previous sections, you may have seen that we wrote simple expression statements without indentation.

However, in order to create compound statements, indentation will be very necessary.

Examples:


# Syntax
variable = expression
# LHS <=> RHS

6

Now, you can also see the scene where unwanted indentation leads to errors. Therefore, let's try to indent a simple expression statement.


# Syntax
variable = expression
# LHS <=> RHS

7

Related articles: