Python while, for loop, list of lists

Source: Internet
Author: User

1. While loop (loops can also be called iterations, traversal)

A while loop must have a counter

Count=0

While count<10:

Print (' Hello ')

Count=count+1

2. Guess the number game example

Import Random #引用随机数模块
Num=random.randint (1,100) #随机产生一个数字, 1 to
count=0
print (num)
While count<7:
guess=input (' Please enter the number you guessed: ')
Guess=int (guess)
if Guess>num:
print (' Guess big ')
Continue
elif Guess<num:
print (' Guess small ')
Continue
Else:
print (' Congratulations on your guess right ')
Break #立即结束循环
count=count+1
Else:
print (' Run out of money, please recharge ')

3,count=count+1 equivalent to count+=1(+ 、-、 *,/the same wording)

4, Breakpoint: Click on the front of the statement, a small red dot, using debug run.

5. For loop  

num=10
For i in range: #表示循环10次
guess = input (' Please enter the number you guessed: ')
guess = int (guess)
if guess > num:
print (' Guess big ')
elif guess < num:
print (' Guess small ')
Else:
print (' Congratulations on your guess right ')
Break # immediate End loop
Else:
print (' Run out of money, please recharge ')

6. The difference between break and continue:

Break is the immediate end of the bounce loop, continue is the end of the current loop into the next loop.

7, List (array)

stus=[' Saki ', ' Yuri ', ' MIMO ' #定义数组

Subscript (angle, index, number) 0 1 2

Stus[0] means Saki stus[1] represents Yuri, subscript starting from 0

8, list additions and deletions to check

# Add
stus.append (' Sami ') #在列表末尾增加一个元素
stus.insert (0, ' Kumi ')#在指定位置添加一个元素
# Delete
Stus.pop (2)#删除指定位置的元素
del stus[3]#删除指定位置的元素
stus.remove (' Saki ')#删除指定的元素
stus.clear ()#清空整个list
# Modify
stus[2]= ' Miumiu '
# Check
Print (stus[2]) #查询取下标
Print (stus[-1]) #查询list最后一个元素
stus.count ()#统计这个元素在list里面出现了几次
Print (Stus.index (' Saki '))#返回这个元素第一次出现的下标, if the element does not exist, an error will be
stus.reverse ()#反转列表
print (Stus)
stus2=[' Yuri ', ' Yuro ', ' Yumi ']
stus.extend (stus2) #把后面list里面的值加入到第一个list里面
stus3=stus+stus2 #合并两个list
print (Stus)

9. List sort

NUMS=[12,33,11,55,19,25,88]

Nums.sort () # Default Ascending

Nums.sort (reverse=true) #降序

10, three-dimensional array:

my=[

[1,2,3,4,5],

[' Name ', ' age ', ' sex ', [' Saki ', ' Mimo ', ' Yuri '],

890]

Three-dimensional array fetch mimo:my[1][3][1]

11. Two-dimensional array: my[[1,2,3,4,5],[' name ', ' age ', ' sex ']

print (my) #取数组长度

12. Whether the user exists

Username=input (' User: ')

Method 1:

Count=stus.count (username)

If count>0:

Print (' User already exists ')  

Method 2:

If username in Stus:

Print (' User already exists ')

If username not in Stus:

Print (' User not present ')

Python while, for loop, list of lists

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.