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 condition, assertion and Loop statement in Python

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces how to use the condition, assertion and loop sentence in Python. The article is very detailed and has certain reference value. Interested friends must read it!

1. Python code block

A code block is not really a statement, but to understand the contents of conditional statements and loop statements, you must be familiar with code blocks.

A code block is a set of statements that can be executed when conditions are met (if statements), multiple times (loops), and so on. Python code blocks are created by indenting code (that is, preceded by spaces). (code blocks are represented in the form of {} in C++, Java, etc.)

In many languages, a special word or character (such as begin or {) is used to identify the starting position of the code block and another special word or character (such as end or}) is used to identify the end position. In Python, use a colon (:) to indicate that there is a code block next, and indent each line of code in that block by the same degree. When you find that the amount of indentation is the same as before, you know that the current block of code is over. For example, I'm going to output 1-10 numbers, as shown below, where the area marked in red can be called a code block.

If 1 subscription 1:

Print ("1 and 1 are equal")

The output is that 1 and 1 are equal

Conditions and conditional statements

1. Boolean types in python: 0 and 1, the difference between false and true.

In python, the standard truth values are 0 (false) and 1 (true). In fact, True and False are just aliases for 0 and 1, and although they look different, they have the same effect. So, if you see an expression that returns 1 or 0 (probably written in an older version of Python), you know

This actually means True or False.

2. If...else... in python Statement, which means: if the statement following the if statement is true, execute the statement block under if, and vice versa, execute the statement block under else. As shown in the following example:

Name=input ("what is your name")

If name.endswith ("Jane"):

Print ("Yes")

Else:

Print ("No")

Running result: what is your name Jane

Yes

3. Elseif statement in python: it is mainly used to check multiple conditions. For example, to determine whether a number is even, odd, or 0.

Num = int (input ('Enter a number:'))

If num > 0:

Print ('The number is positive')

Elif num < 0:

Print ('The number is negative')

Else:

Print ('The number is zero')

4. Chain comparison of python. Compared with other languages, it is quite convenient for python to have a chain comparison. If you want to determine whether a number X is greater than 4 and less than 19, you can directly use the following ways:

Xero5

If 3

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

Internet Technology

Wechat

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

12
Report