Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use expressions and indents of Python statements

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use the expression and indentation of Python sentences. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

What is a declaration of statement 1.1in Python?

Statements in Python are logical instructions that can be read and executed by the Python interpreter. In Python, it can be an expression or an assignment statement.

Assignment statements are the basis of Python. 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, and so is the variable.

Using expressions, we can perform operations such as addition, subtraction, join, and so on. It can also call functions that evaluate the results.

Example:

# using arithmetical expressions > ((10 + 2) * 100 / 5-200) 40. Functions in expressions using functions > pow (2,10) 102 expressions using eval > eval ("2.5 to 2.5") 5.01.3 simple assignment statements in expressions

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

# Syntaxvariable = expression# LHS RHS

Now let's take a closer look at the three assignment statements in Python and see what happens.

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

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

> test = "Learn Python"

Python creates a string "Learn Python" in memory and assigns the name "test" to it. 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 of the location of the data in memory. Now, here are some interesting things you should know.

If you create another string with the same value, Python creates a new object and assigns 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 allocates the same memory address in the following two scenarios.

The string has no spaces and contains less than 20 characters.

If the integer range is between-5 and + 255.

This concept is called Interning. Python does this 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 variable.

> 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 aliases for existing objects. Let's use the id () function to verify this.

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

Case 3: the right side (RHS) 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)

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

> test = 2 * 5 > print (test) 10 > type (test)

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

1.4 enhanced assignment statement

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

Look at the following example to see the enhanced assignment statement.

X + = y

The above statement is an abbreviation of the following simple statement.

X = x + y

The next example is a clearer example of adding new elements to the tuple.

> my_tuple = (10,20,30) > my_tuple + = (40,50,) > print (my_tuple) (10,20,30,40,50)

The next example is to use a vowel list. It is demonstrating the addition of missing vowels to the list.

> list_vowels = ['axiaozhongzhengjie'] > list_vowels + = ['oozy,' upright,] > print (list_vowels) ['axiangdai', 'oasis,' u'] II. Multi-line sentences in Python

Typically, each Python statement ends with a newline character. However, we can extend it to multiple lines using the line continuation ().

Python provides us with two ways to enable multiline statements in a program.

2.1 explicit renewal

When you immediately split the statement into multiple lines using the line continuation ().

Example:

# use multiline statements to initialize the list > my_list = [1,\... 2, 3\, 4 print 5\] > print (my_list) [1, 2, 3, 4 paraphrase 5] # use multiline statements to evaluate expressions > eval (\. "2.5\... +\... 3.5") 6.02.2 implicit continuation

Implicit line continuation refers to the use of any split statement in parentheses (), square braces [], and curly braces {}. You need to use the mentioned constructs to enclose the target statements.

Example:

> result = (10 + 100... * 5-5. / 100 + 10. ) > > print (result) 519.95

Another example:

> > subjects = [. 'Maths',... 'English',... 'Science'... > print (subjects) ['Maths',' English', 'Science'] > type (subjects) III, Python indentation

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

The block of code that represents the body of a function or loop begins with indentation and ends with the first line that is not indented.

How many spaces are indented in 3.1Python?

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

3.2 Why is indentation important?

Most programming languages provide indentation for better code format and do not force indentation.

However, in Python, indentation rules must be followed. Typically, we indent each line in the code block by four spaces (or the same number).

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

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

Example:

Def demo_routine (num): print ('I am a demo function') if num% 2 = 0: return True else: return Falsenum = int (input ('Enter a number:')) if demo_routine (num) is True: print (num,' is an even number') else: print (num,'is an odd number')

Now, you can also see the scenario where unwanted indentation leads to errors. So let's try indenting a simple expression statement.

> If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report