shprint语句基本使用
print("abc") #用print直接输入
Print("a","b","c") #a b c中间用空格分隔
print("a","b","c",sep=".") #abc中间用“.”号分隔
print("a",end="") #a后什么也不接
print("b",end="\n") #b后换行
print("c",end="\t") #c后空格
print(type(a)) #判断a类型,前面需要定义a=""
a=input("输入:") #input默认str类型
#修改input输入类型为int
a=int(input("输入:"))
条件判断语句
#if,else与elif为判断句式常用后面可加判断条件,结束后要加":"
#成绩判断程序
score = int(input("输入:"))
if score >= 90 and score <= 100 :
print("本次考试你的等级为:A")
elif score >= 80 and score < 90 :
print("本次考试你的等级为:B")
elif score >= 70 and score < 80 :
print("本次考试你的等级为:C")
elif score >= 60 and score < 70 :
print("本次考试你的等级为:D")
elif score >= 0 and score < 60 :
print("本次考试你的成绩不及格")
else :
print("输入成绩错误")
生成随机数
import random #引入随机库
x = random.randint(0,2) #随机生成【0-2】的随机数,包含0.1.2三个数
print(x)
?for循环
for i in range(0,11,3): #从0开始,到11结束,步进值为3(每次加3)
print(i)
name = "chengdu"
for x in name;
print(x,end="\t" #结果为c h e n d d u
a = ["aa","bb","cc""dd"]
for i in range(len(a)) #len()取数组成员数量
print(i,a[i]) #结果为0 aa\n1 bb\n2 cc\n3dd
while语句
i=0
while i<5 :
print("i=%d"%i)
i += 1 #i递进加一
#1-100求和
i = 1
sum = 0
while i <= 100 :
sum = sum + i
i += 1
print("和为%d"%sum)
?break? 可以跳出for和while的循环体
continue 跳过当前循环,直接进行下一轮循环
pass 空语句,一般用作占位语句,不做任何事情
This is my first day learning Python,accorse the study of .What I've learned today is that Python is a very compact language and not very difficult to learn
|