第二天
今天接着昨天的继续
重温一下昨天知识
https://blog.csdn.net/weixin_44467344/article/details/95213809
今天的内容涉及到 : if语句、循环语句、input和random、列表、进制转换、运算符等等
首先介绍一下,非静态方法:
需要由具体的对象取调用
调用的方法是:对象.方法名(参数)
函数的定义中第一个参数是self
import random
#string类型的变量不要定义为str,str本身是一个函数
test_str = 'python'
print(test_str.capitalize()) #首字母大写
hello = 'hello,world' #count() 方法用于统计字符串里某个字符出现的次数。
print(hello.count('o',0,len(hello))) # 可选参数为在字符串搜索的开始与结束位置。
print(hello.find('world')) #find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。
print(hello.startswith('he')) #startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。
moviename = '◎片 名 The Best of Enemies'
print(moviename.split('\u3000'))#split()分隔符切片字符串
print(moviename.split('\u3000')[3])
result = moviename.replace('\u3000\u3000','').split('\u3000',1) #replace(old,new)new替换old
print(result)
方法的调用: 类名/模块名.函数名(参数)
运算符摘要
比较运算符:什么等于,不等于,大于小于,大于等于,小于等于、、、简直 so easy!
== != < >
<= >=
逻辑运算符:and 、or 、 not
提一句and是短路运算符,即and前的条件若不满足,为False,没必要验证后面条件了。验证一下短路运算:
#验证 and 的短路运算
def test(): #定义函数test()
print('test')
return False
if False and test():
print('and的短路运算')
else:
print('验证and的短路')
if语句
if语句,用random随机一个1-100的整数,然后进行简单的分数分析
import random
score = random.randint(1,100)
print(score)
if score>=90:
print('优秀')
elif 70<=score<90:
print('良好')
elif score>=60 and score<70:
print('及格')
else:
print('不及格')
很简单,改改版,练习练习。随机一下今天吃什么吧,每天为了吃什么而纠结,唉~~~
num=random.randint(1,40)
print(num)
if num <= 10:
print('今天吃刀削面')
elif num<= 20 and num>10:
print("今天吃盖浇饭")
elif num>20 and num<=30:
print("今天吃鱼粉")
else:
print('今天吃麻辣烫')
啊哈哈哈,麻辣烫走起~
循环
循环语句while、for
#简单循环,打印1-100数字
num=1
while num<101:
print(num,end=' ')
num+=1
#死循环
'''while True:
pass'''
whlie循环
用while循环和if语句结合input和random,大杂烩,来个猜数字游戏叭,好无聊的说~~~~
ps:python的缩进很重要哦
#猜数字游戏
while True:
num = random.randint(1,100) #生成一个随机数
while True:
# 提示用户输入数字
temp_num = input('请输入猜测的数字:')
temp=int(temp_num) #临时记录一下猜测的数字,转为int类型
#判断数字的大小, 若猜正确,打印‘你猜对了’
# 若不正确,继续猜测,直到真确
if temp == num:
print('好棒呀,你猜对了。')
break #跳出循环
else:
if temp>num:
print("你猜的太大了,请继续:")
else:
print("你猜的太小了,请继续:")
#print('你猜错了,游戏继续。')
print('还要再玩一次吗?')
res = input('y/n')
if res == 'y':
pass
else:
break
游戏不想玩儿了,查查话费?随便写写吧,练练手的说~
#话费查询系统
while True:
while True:
num = random.randint(1, 10)
temp_num = input('业务查询请按1,业务办理请按2,充值缴费请按3,人工服务请按0')
temp = int(temp_num)
if temp == 1:
temp1=input('话费查询请按1,流量查询请按2,退出请挂机。')
temp11=int(temp1)
if temp11 == 1:
print('您的话费余额为···元,稍后短信将发送至您的手机')
break
if temp11 == 2:
print('您的剩余流量为···M,稍后短信将发送至您的手机')
break
elif temp == 2:
temp2=int(input('话费充值请按1,流量充值请按2,'))
if temp2 ==1:
temp22=int(input('请输入充值金额:'))
print('充值成功')
break
if temp2 ==2:
temp23=int(input('请输入充值金额:'))
print('充值成功')
break
for循环
练练手,贼拉简单的打印数字啊、99乘法表啊、三角形啊、菱形啊乱七八糟的什么
#打印1-10数字
#横向打印1-9
for i in range(1,10):
print(i,end=' ')
#循环嵌套
'''for i in range(1,10):
print(i,end=' ')
for j in range(1, 10):
print(j,end=' ')'''
#打印乘法表
for i in range(1,10):
print(i)
for j in range(i):
j=j+1
print('%d*%d=%-3d' % (i,j,i*j),end=' ')
print('')
#打印下三角形
for i in range(1,10):
for j in range(1,10):
if i>=j:
print("*",end=' ')
print()
温馨提示
break、continue、return三者的区别:
break跳出当前循环
continue跳出本次循环,继续下一次
return彻底跳出循环
列表
话不多说,上图叭!
#列表
test_list = [1,2,3,4]
test_list1 = [1,'a',[2,3,4]]
print(test_list[0])
#遍历
for i in range(len(test_list)):
print(test_list[i],end=' ')
for item in test_list:
print(item,end=' ')
print()
test_list.append('asas') #末尾加
test_list.insert(0,'ss') #指定位置插入
test_list.remove('ss') #移除指定字符
test_list[4]=-1 #改动元素
test_list.sort() #排序
test_list.reverse() #反转
print(test_list)
进制转换
这没啥好说的,都会,哈?
温故知新,figthing!