Python basics
Note: It is suitable for Mengxin to learn python and the content inside will be continuously updated!
About for loop
Example: Create a data set containing random integers from 1 to 10, totaling 100 numbers, and count the number of times each number appears.
// 方法1
import random //引入random模块
lst = [] //定义一个空列表
d = dict() //定义一个空字典
for i in range(100): //循环100次
n = random.randint(1,10) //每循环一次随机拿到1——10以内的任意一个数
if n in d :
d[n]+=1 //如果这个数作为键在字典里,值加1
else:
d[n]=1 //否则值等于1
print(d)
//方法2
import random
lst=[]
for i in range(100):
n=random.randint(1,10)
lst.append(n)
d={
}
for n in lst:
if n in d:
d[n]+=1
else:
d[n]=1
Functions commonly used in for loops
- range()
- zip()
- enumerate( )
- List comprehension
Example: Find a number within 100 that is divisible by three
lst=[]
for i in range(100):
if i % 3 == 0;
lst.append(i)
print(lst)
Use list analysis to find the number within 100 that is divisible by three
>>>[i for i in range(100) if i % 3 == 0]
结果是:[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
>>>range(0, 100, 3)
>>>list(range(0, 100, 3))
Use the range method to add the corresponding values of two lists
c=[1,2,3,4,5]
d=[6,7,8,9,1]
r=[]
for i in range(len(c))://把列表中的每一个元素所对应的索引拿出来了
r.append(c[i]+d[i])
Use the zip method to add the corresponding values of the two lists
c=[1,2,3,4,5]
d=[6,7,8,9,1]
r=[]
for x,y in zip(c,d):
r.append(x+y)
print(r)
enumerate() method: enumerate returns index value
>>>s=['one','two','three']
>>>list(enumerate(s))
[(0, 'one'), (1, 'two'), (2, 'three')]//得到索引和值
Example: The string s='life is short You need python'. Count the number of occurrences of each letter in this string.
s='life is short You need python'
lst = [] //定义一个空列表
d = dict() //定义一个空字典
for i in s: //循环获取s中的每个字母
if i.isalpha(): //判断循环得到的是否是一个字符,是字符则执行后面的代码块 否则返回false
if i in d :
d[i]+=1 //'键'放在字典里并且'值'加1
else:
d[i]=1
print(d)
About the while loop
格式: while[condition]:
statements
a=0
while a<3:
s=input("input your lang:")
if s=='python':
print("your lang is {0}".format(s))
break //当代码执行到这里时,会跳出当前循环(此处跳出while 执行print("the end a:",a))
else:
a+=1
print("a=",a)
print("the end a:",a)
a=11
while a>0:
a-=1;
if a%2==0:
continue
print(a)
else:
print(a)
Use the while loop to make a small game:
make a guessing game that meets the following functions:
1. The computer randomly generates a positive integer within 100;
2. The user enters a number through the keyboard and guesses the random number generated by the computer.
Note: There is no limit to the number of user input.
import random
computer_name = random.randint(1,100) #表示从1——100以内随机取一个数
while True: #表示一直循环,直到遇到结束标志
user_name = input("请输入你要猜测的数字:") #获得的是字符串类型
if not user_name.isdigit():
print("请输入1——100以内的整数" )
elif int(user_name)<0 or int(user_name)>=100:
print("请输入1——100以内的整数")
else:
if int(user_name)==computer_name:
print("you are win")
break
elif int(user_name)<computer_name:
print("you are smaller")
else :
print("you are bigger")
#注:并非是最优代码,但程序完全正确!因为此时作者也处在学习阶段!