Python-Based Conditional loop statements and basic python statements

Source: Internet
Author: User

Python-Based Conditional loop statements and basic python statements

The first two articles talk about data types and operations. This article describes conditional statements and cyclic statements.

0x00. Condition Statement

A condition statement is a code block that is determined by the execution result (True or False) of one or more statements.

You can briefly understand the execution process of conditional statements:

Python interprets non-zero valuesTrue.NoneAnd0Are interpretedFalse.

Python determines that values other than 0 are True, while None and 0 are considered False. Note that True and False are capitalized, and Python is case sensitive.

The basic format of the Condition Statement is:

If judgment condition: execution statement ...... Else: Execute the statement ......
You can use the following form to determine multiple values:

 

If condition 1: Execute Statement 1 ...... Elif condition 2: Statement 2 ...... Elif Condition 3: Statement 3 ...... Else: Execute Statement 4 ......

 

0x01. Cyclic statements

When we need to execute a statement or statement group multiple times, it is impossible to write the same statement multiple times. First, it is complicated, and second, it is not conducive to maintenance. At this time, the loop statement came into being. The loop statements are divided into a for loop and a while loop.

For Loop

A for loop can traverse projects in any sequence, such as a list or a string. The process is as follows:

The syntax format of the for Loop is as follows:

for iterating_var in sequence:   statements(s)
While Loop 

The while statement is used to execute a program cyclically under a certain condition to process the same task that needs to be processed repeatedly. The process is as follows:

The syntax format of the while loop is as follows:
While judgment condition: execution statement ......
0x02. Instance 
The following example uses nested loop output 2 ~ Prime number between 100:
#! /Usr/bin/python #-*-coding: UTF-8-*-I = 2 while (I <100): j = 2 while (j <= (I/j )): if not (I % j): break j = j + 1 if (j> I/j): print I, "is a prime number" I = I + 1 print "Good bye! "
 
0x03. References
Https://www.programiz.com/python-programming/variables-datatypes of variable and Data Type
Condition Statement https://www.programiz.com/python-programming/if-elif-else
Loop statement https://www.programiz.com/python-programming/for-loop
Https://www.programiz.com/python-programming/while-loop
W3cshool https://www.w3cschool.cn/python/python-tutorial.html

The basics end here, the next article, python advanced object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.