Python Learning _4_if_while_for

Source: Internet
Author: User

#条件语句 Loop Statements

#条件语句: The condition is met before execution

#语法 sentence patterns

#python代码的特点: Using indentation to control the level code is performed from top down

#1) Meet the criteria to execute if or other elif code

#2) must be if the start can be without elif but must have if

#3) If you have elif then elif must have conditional expressions

#4) There can be multiple elif in a conditional statement, only one if, an else

#5) You cannot add any expression after else inside a conditional statement

#最简单的句式
A = 1if a < 0 :print("A's value greater than 0")#If the expression is true Execute the following code #If the expression after is false does not execute the following code
#2: Upgrade Statement
A = 5
If a > 10:
Print ("A's value is greater than 0")
Elif A < 0:
If a <-10:
Print ("negative number ....") ")
Print ("A's value is less than 0")
elif A = = 0:
Print ("0000")
Else
Print ("Done")

Small exercise: Late class questions

time = Int (input ("input the time:"))ifTime >0:ifTime <= 5:        Print("not late .")    elifTime <= 10:        Print("Shandao.")    Else:        Print("absenteeism.")Else:    Print("wrong input")

#循环语句 while loop while keyword

#语法: While conditional expression:

#代码块

The rules that #while execute first determine the condition after the while

#满足就执行while下面的代码块 finished executing the code block.

#继续判断while条件 meet and then execute again, not meet to do the cycle

#如何跳出死循环或者避免自己的代码出现死循环

#1) Set a good judgment condition, can not meet the conditions at the right time, jump out of the loop

#2) skillfully breaks the current loop with break

A = 0 while a > 0:    print("python test 6666")
Break
A = A-1

#练习题:

# use while loop in order greater than 10,9....0num = ten while num >= 0:    print' ' )    -= 1
#结果为: 10 9 8 7 6 5 4 3 2 1 0

#for cyclic single-layer cyclic multilayer loops

#for循环 to traverse the element's keyword for

#for item in data range: #字符串 A list of tuple dictionaries can also be other data types

list_1 = [' Don't leave ' ' little star ''tiger'  sha'] for in list_1:     # traversing elements will put in The elements in the subsequent data range are all taken over the    # essence: Get these values in turn    print(item)
The second way:
For item in range (LEN (list))
Print (List_1[item])

#问题: The For loop is executed several times by who decides-----> length of data

#遍历元组或字符串:

' Python8 '  for inch list_1:     Print  '666',' flowers ' hahaha ' ) for in tuple_1:    print(i)

#遍历字典:

dict_1 = {'age'sex'girl'}   for in dict_1:    print (key) #遍历字典里的键值对, printing key     Print (Dict_1[key]) #打印key对应的值

#字典名. Keys () gets the dictionary all key values
#字典名. VALUES () gets all the value values

#range function It can generate a sequence of integers of a specified range

#str () int () list ()

res = list (range (5)) #可强制转换为list

#range (m,n,k) m integer sequence of beginning n integer sequence ending with K step

Res_1 = List (range (1,5,1))  #1,2,3,4 res_2 = List (range (0,5,2))  #0,2,4 Res_3 = List (range (6, 0,-1))  #6,5,4,3,2,1 res_4 = List (range (6,0,-2))    #  6,4,2,

#嵌套循环 Two for loop there's a loop inside.

List_1 = [[1,2,3],[4,5,6]]# traverse this list: for in range (len (list_1)):      for in range (len (list_1[i))        :print(List_1[i][j])

Practice:

#遍历整个列表元素
L = [['Apple','Google','Microsoft'], ['Java','Python','Ruby','PHP'], ['Adam','Bart','Lisa'] ] forIinchRange (len (L)): forJinchRange (len (l[i)):Print(L[i][j])

Python Learning _4_if_while_for

Related Article

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.