day2 python基础

1.python的基础

input接收输入的数据传到变量,可以对变量进行引用,输入的数据类型都是string类型

name=input("请输入一个名字:")

2.判断语句if  else 

    score=input("请输入分数:")

#用Input接收到都是字符串,需求强制转换
score=int(score)
if score>= 90:
print("优秀")
elif score >= 80:
print("良好")
elif score >= 60:
print("及格")
else:
print("不及格")

3.循环while, for

1)while 循环 if 条件嵌套使用,使用while循环时很多情况下时=是要有一个计数器用来控制循环的次数

count=0        #计数器
while(count<10):
print("嘿嘿")
count+=1 #没有这一行的时候,进入死循环
结果是:输出10次test

2)break #当循环执行到符合条件时,整个循环都会停止 ; continue 代码执行到符合条件的语句时,跳过本次循序,继续执行下一次循环;猜小游戏代码
import  random
rand=random.randint(1,100) #随机产生数字
count=0
while count <7:
count+=1
guess=int(input("请输入一个数字:"))
if guess>rand:
print("猜大了")
# continue
elif guess==rand:
print("恭喜你才对了")
break
else:
print("猜小了")
#continue
else:
print("你的机会用光了")
3)for循环
import  random
rand=random.randint(1,100) #随机产生数字
count=0
for count in range(7):
count+=1
guess=int(input("请输入一个数字:"))
if guess>rand:
print("猜大了")
# continue
elif guess==rand:
print("恭喜你才对了")
break
else:
print("猜小了")
#continue
else:
print("你的机会用光了")

4.字符串格式化
import datetime
name="牛教授"
today=datetime.date.today()

do="%s%s在上课"%(name,today) #%s是后面跟字符串,%d是后面跟数字的,一般直接使用%s就可以了

print(do)

read="吃"
print(name+"爱"+read) #也可以直接使用字符串的拼接

sql='insert into student (id,name,classname,email,sex) VALUE ({id},{name},{classname},{email},{sex})' #使用format格式化

sql1=sql.format(id=1,name="zxg",classname="金牛",email="11@qq.com",sex="男")

print(sql1)


原文地址:https://www.cnblogs.com/zzzao/p/9497996.html