print()用于打印内容至屏幕
print("hello")
字符串需要用单引号'或双引号"包围
a = "hello"
b = 'hello'
如果字符串中有单引号时,需要用双引号包围
a = "That's why"
字符串中有双引号时,用单引号包围
a = '"你好"'
注释使用双引号包围
'''
这里是注释
'''
变量,用于接收一个值
a = "hello" #此处的a就是一个变量
判断变量类型
print(type(变量名))
从键盘接收字符串
score = input("请输入一个数字:")
类型转换
#类型(变量)
score = int(score) #将字符型的数值转换为int类型
条件判断
if...else...
if a == b:
print("相等")
else:
print("不相等")
if...elif...else...
if name == "小吴":
print("大美女")
elif name == "小王":
print("大帅哥")
else:
print("丑八怪")
判断成绩
#输入一个分数,大于90,优秀
#小于90,大于等于80,良好
#大于等于60,小于80,及格
#小于是60,不及格
score = int(input("请输入你的成绩:"))
if score >= 90:
print("优秀")
elif score < 90 and score >= 80:
print("良好")
elif score < 80 and score >= 60:
print("及格")
else:
print("不及格")
循环
循环指重复的做一件事情,也叫遍历、迭代
while循环
用while循环,必须得有一个计数器
count = 0 #计数器
#循环就是在重复执行循环体里的代码
while count < 10:
print("test")
count +=1 #每次循环都需要对计数器+1,不然会死循环
else: #程序正常结束之后执行的
print("什么时候执行我呢")
break,结束循
count = 0
while count < 3:
name = input("请输入一个名字:")
print("你输入的名字是", name)
if name == 'quit':
break #遇到break结束循环,后面的else也不会被执行
count += 1
else: #
print("什么时候执行我呢")
continue,结束当前循环,进入下次循环
count = 0
while count < 5:
if count == 2:
count += 1
continue #当遇到count=2时,跳出当前循环,进入下一次循环。
print("哈哈哈")
count += 1
猜数字
import random
num = random.randint(1, 100)
count = 0
while count < 7:
count +=1
guess = input("请输入一个数字:")
guess = int(guess)
if guess > num:
print("猜大了")
continue
elif guess == num:
print("恭喜你,猜对了")
break
else:
print("猜小了")
continue
else:
print("错误次数过多")
for循环
import random
num = random.randint(1, 100)
count = 0
for i in range(3):
guess = input("请输入一个数字:")
guess = int(guess)
if guess > num:
print("猜大了")
elif guess == num:
print("恭喜你,猜对了")
break
else:
print("猜小了")
print("错误次数过多")
字符串格式化
%s为字符串,%s可以接受各种类型的值,%d为整数,%f为小数,类型不匹配会报错,建议使用%s,如果想保留2位小数则写成%.2f
import datetime
name = '小王'
today = datetime.date.today()
welcome1 = "%s,周末了,亲手为家人泡上一道茶吧。今天的日期是%s" % (name, today)
print(welcome1)
name2 = '小张'
print(name + '爱' + name2)
words = '你的名字是%s,你的年龄是%s,你的分数是%s'% (name,28,17.3)
words2 = '你的名字是%s,你的年龄是%d,你的分数是%.2f'% (name,28,17.3)
print(words)
print(words2)
#如果参数比较少的时候可以使用占位符的方式
sql = 'insert into student (id, name, age, addr ,phone, sex, qq, email) values ' \
'("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s");'
#参数较多时使用format()方法
sqls = 'insert into student (id, name, age, addr, phone, sex, qq, email) values ' \
'({id},{name},{age}.{phone},{addr},{sex},{qq},{email})'
sql3 = sql.format(id=1,name='小张',age=18,sex='男',phone=12345678,qq=12346,email="123@qq.com")
print(sql3)
练习
# 1、写一个登陆的小程序
# username = admin
# passwd = 123456
# 1、输入账号密码, 输入正确就登陆成功,
# 提示欢迎xxxx登陆,今天的日期是多少
# 2、最多输入错误3次
# 账号 / 密码错误,请重新登陆
# 3、如果失败测试超过3次,提示,失败次数过多
# 4、要校验输入是否为空,如果输入为空,你要提示账号. / 密码不能为空
# 什么都不输入和输入一个空格多个空格都算空。
# 输入为空也算操作错误一次
import datetime
for i in range(3):
username = input("请输入你的用户名:").strip()
password = input("请输入你的密码:").strip()
if username == "admin" and password == "123456":
print("欢迎%s登陆,今天的日期是%s."%(username, datetime.datetime.today()))
break
elif username == '' or password == '':
print("用户和密码都不能为空!")
else:
print("账号或密码错误!")
else:
print("错误次数过多!")

作者:牧歌