Alibabacloud.com offers a wide variety of articles about nested while loops python, easily find your nested while loops python information here online.
5.5.5 skip Loop
1 break
Same as C.
>>> from math import sqrt>>> for n in range(99,0,-1):root = sqrt(n)if root == int (root):print nbreak81
2 continue
As in C, do not go into details.
3 while True/break Idioms
Example:
>>> while True:word=raw_input('p;ease enter a word:')if not word: breakprint ' the word was ' +wordp;ease enter a word: a the word was ap;ease enter a word: b the word was bp;ease enter a word: the word was p;ease enter a word:5.5.6 else clause in a loop
for n in range(
customize the condition of the loop end
Python's judgment
If-else format
if 满足某个条件 :
执行某些语句
else :
执行某些语句
NOTE: The IF statement and the Else statement are finally added: and the code to be executed after if and else is indented
Functions of Python
>>> new_movies = ["movie1","movie2","movie3",["movie4_1","movie4_2",["movie_4_3_1","movie_4_3_2"]],"movie5"]
>>> def list
count+1, or it becomes a dead loop, because count is always 0 .Dead loopThere is a cycle called the cycle of death, once triggered, will run an everlasting, lasting.While is performed as long as the condition behind it (i.e. the condition result is true) is executed.Count = 0while#true is true in itself print(" You are the wind I am the sand, tangled up to the horizon ... " , Count) +=1Loop Abort StatementIf, for some reason, you do not want to continue the loop during the loop, how do y
= {'A': 1, 'B': 2, 'C': 3}>>> Key, value = s. popitem ()>>> Print key, valueA 1The last example shows that it allows the function to return more than one value and package it into a metagroup. Then, it can be easily accessed through a copy statement.Note: The number of elements in the unwrapped sequence must be exactly the same as the number of variables placed on the left that match the value assignment. Otherwise, an exception is thrown.2.2 chain assignmentChained assignment is a shortcut to
():Print key + ":" + str (value)For set unorderedC_set = set ()C_set.add (' a ')C_set.add (' B ')C_set.add (' C ')C_set.add (' d ')C_set.add (' e ')For value in C_set:Print valueNumber accumulationsum = 0For value in range (1,11):Sum + = valuePrint sumWhileCNT =2While CNT > 0:print ' Love Python 'CNT-= 1Print DualI =1While I i + = 1If i%2 > 0:ContinuePrint IString lengthstr = ' ABCDEFG 'Print Len (str)String interceptionPrint Str[2:5]String uppercase
:print ' The number is positive 'Elif num print ' The number is negative 'Elseprint ' The number is zero '_______________________________ The = = Budget character Determines whether 2 objects are equal, and is to determine whether the two are equivalent (the same object)An Assert statement is available if you need to make sure that a condition in the program must be true for the program to function4. CirculationWhile loopWhile xPrint XX=x+1For loopwords=[' This ', ' was ', ' an ', ' ex ', ' Par
, 8, 9]matrix=[[1,2,3 , 4],[5,6,7,8],[9,10,11,12]] #list中的每个元素都是list, the elements in each list element in the list are combined by index into a new listprint [[Row[i] for row in the matrix] for I in range (4)]#[[1, 5, 9], [2, 6, ten], [3, 7, one], [4, 8, 12]]transposed=[]for I in range (4): Transposed.append ([row[i ] for row in matrix]) print transposed#[[1, 5, 9], [2, 6, ten], [3, 7, one], [4, 8,]] #等价于: transposed=[]for i in Ran GE (4): transposed_row=[] for row in Matrix:transposed_row.app
In Python, assignment and comparison operations can be connected, and operators can be used together, such as: 0
The = = operator Determines whether two objects are equal, is to determine whether the two are equal (the same object)
Assert that the program crashes directly when an error condition occurs: Assert + expression
The zip function compresses two sequences together and returns a list of tuples: Zip (names, ages)
With the Else state
]List parsing also provides the syntax for an extended version number:[Expr for Iter_var in iterable if COND_EXPR]The core of this statement is the for loop, which iterates through all the entries of the Iterable object. The preceding expr is applied to each member of the sequence, and the final result value is the list produced by the expression. Iterative variables do not have to be part of an expression.>>> [x * * 2 for X in range (6)][0, 1, 4, 9, 16, 25]>>> seq = [11, 10, 9, 9, 10, 10, 9, 8,
1 ImportRandom2 3 """4 #查看源代码日后爬虫用5 Import Urllib.request6 7 # Coding=utf-88 9 url = "http://www.baidu.com"Ten data = Urllib.request.urlopen (URL). Read () One data = Data.decode (' UTF-8 ') A - print (data) - the # Print practiced hand - x = 5/2 - y = 3/2 - + print (' x = ', x, ' \ n ', ' y ', y) - print (' x and y are%.1f,%.1f '% (x, y) respectively) + A for z in range (1, one): at print (' {0:5d} {1:5d} {2:4d} '. Format (z, z * z, z * z * z)) - - print ("What??" + "Water") - Print (5 + 8
Conditional statement: If ... else elifLoop statement: while forExit: Break continue return exit passApplication Scenario: Level Three menu Enter City serial number enter the menu input b return input Q exit#!/usr/bin/env Python3ImportOSImportSYS" "make a level three menu Shandong Rizhao East Haven Lanshan Wulian" "defShowcd (dir):" ":p Aram dir: The directory to go: return: Returns the list under the directory to go" " if(Os.path.exists (dir)): Dirlist=os.listdir (dir) Num= 1 forDirNameinchd
1\IF statement: If condition is true, executes the IF clause statement and skips the subsequent elif or else statements; The 2\while statement, as long as the while condition is true, executes the WHILE clause statement, and then looks back at the judgment value of the while condition as long as the condition is true indefinitelyWhile clause continue statement: Skip the following execution statement, go back and continue to judge while condition;Break statement: Immediately jump out of the loop,
#while循环‘‘‘while 条件判断: 满足条件执行的代码 ...条件判断的根本是True和False的判断 True才会执行 False不会执行通过一下方法来打印条件是True或者False a = 10 b = 20 print(a > b) print(b > a)‘‘‘# 值条件判断:# a = 3# b = 5# 正常条件判断# while a > b:# print("啦啦啦")# 死循环# while True:# print("啦啦啦")#打印1-50数字到终端‘‘‘方法一:直接使用条件判断‘‘‘# count = 1# while count "Python Basics" process controls-while Loops
First, prefaceThe concepts of closures, higher-order functions, function nesting, and decorators, as well as their relationship 1:30, are difficult to understand during the initial learning of Python, which is the essence of the Python function and takes some time to comprehend. Which decorator is the most difficult to understand, why? Because only the variable scope, the closure function,
This article mainly introduces the nested use of circular statements in Python, which is the basic knowledge in Python, and the friends who need it can refer to the following
The Python programming language allows another loop to be nested within one loop. A few examples ar
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.