python笔记

这篇笔记介绍了Python中的字符串内建函数,包括capitalize(), title(), istitle(), upper(), lower()等,并探讨了条件判断语句中的and和or运算。同时,讲解了如何使用rfind(), replace()以及编码解码、字符串的startswith(), endswith(), isalpha(), isdigit()等方法。" 78560233,6886675,稀疏矩阵乘法算法实现,"['矩阵运算', '数据结构', '算法', '压缩存储']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一章

链接地址
python字符串内建函数 https://www.cnblogs.com/guoguoba/p/5620561.html

条件判断语句

202004
条件判断语句
#布尔型变量做and or not运算

a = True
b = False
print('a and b is {}'.format(a and b))
print("a or b is {}".format(a or b))
a and b is False
a or b is True

#非布尔型变量做and or not 运算;返回的是两个变量的一个,或者是值本身的内容
a 成立,b成立的话,它会返回True.
b判断完了,才会进行返回.
(and成立的话返回的是b的内容,不成立的话,返回的是a的内容)
(or先运算a,如果a成立的话,就不会继续b的检测了.如果a改为空字符,则输出b[123])

a ='hello world'
b =[1,2,3]
print('a and b is {}'.format(a and b))
print('a or b is {}'.format(a or b))
a and b is [1, 2, 3]
a or b is hello world

#and句式。a为空,b为字符:先运行a,然后运行b,运行完成,返回。成立b,不成立,a,此时返回空
Or句式,a不成立,b成立,运行到b直接输出。

a =''
b =[1,2,3]
print('a and b is {}'.format(a and b))
print('a or b is {}'.format(a or b))
输出
a and b is 
a or b is [1, 2, 3]

#and句式。a为空,b为[]:先运行a,然后运行b,运行完成,返回.不成立,返回a,空
#or句式.a为空,b为[]:先运行a,由于a为空,所以返回b。输出b

a =''
b =[]
print(bool(b))
print('a and b is {}'.format(a and b))
print('a or b is {}'.format(a or b))

False
a and b is 
a or b is []
#使用while循环实现1-50之间的偶数的和:
#方法1:
i=0
j=0
while i<=50 :
	j+=i
	print(i)
	i+=2
print(j)
#方法2:
sum=0
n=1
while n<=50:
	n+=1
	if n%2==0:
		sum+=n
		print(n)
print(sum)
#方法3:
h=0
for k in range(1,51):
	if k%2==0:
		h=k+h
print(h)
计算10以内不能被三整除的和:
sum=0
for i in range(10):
	if i%3!=0:
		sum+=i
print(sum)
sum=0
for i in range(10):
	if i%3==0:
		#break #跳出整个for循环结构,结束整个循环
		continue#跳过循环体中下方的语句不执行,直接进行下一次的循环
	sum+=i
print(sum)

掷骰子游戏

# import random

# print('欢迎进入掷骰子游戏')
# username=input('请输入你的用户名')
# print('''
# 	请充值,
# 	金额必须是100的倍数,
# 	100元30个币
# 	''')

# for i in range(3):
# 	coin=int(input('输入充值金额'))
# 	if coin%100==0 and coin>=100:
# 		coins=int(0.3*coin)

# 		print('充值成功,当前余额{},当前硬币{}'.format(coin,coins))
# 		break
# 	else:
# 		print('充值失败,金额必须是100的倍数.')

# while coins>0 :
# 	num_ture=random.randint(1,6)
# 	num_guess=int(input('输入你猜的数字'))
# 	if num_guess==num_ture :
# 		print('bingo!继续还是退出!')
# 		coins-=1
# 		choose=input('contine or quit')
# 		if choose=='q':
# 			print('退出游戏,当前硬币为%d'%coins)
# #			exit()
# 			break
# 		else:
# 			print('继续游戏')
# 	else:
# 		print('猜错了,扣两个币')
# 		print('当前数字%s'%num_ture)
# 		coins-=2
# 	print('当前硬币为%d'%coins)
# else :
# 	exit()
# #以上自己写的

import random
print('*'*20)
print('欢迎进入游戏')
print('*'*20)
username=input('请输入姓名')

money=0
answer=input('是否想要继续游戏')
if answer=='y':
	while money<2:
		print('金币不足,请充值')
		jine=int(input('请输入充值金额'))
		if jine%100==0 and jine>0:
			money=(jine//100)*30
			print('当前游戏币',money)
	print('进入游戏。。。。。。')
	while True:
		num_1=random.randint(1,6)
		num_2=random.randint(1,6)
		num=num_1+num_2
		guess=input('请输入大小')
		if (num<=6 and guess=='小') or (num >6 and guess=='大'):
			print('猜对了')
			money-=1
		else:
			print('猜错了')
			money-=2
		if answer!='y' or  money<2:
			print('退出游戏')
			break
		print(money)

s1=‘abc’
s2=“abc”
print(s1=s2) #比较的是内容
print(s1 is s2) #比较的是地址

message=‘wo hen hao’
print(message.capitalize()) #将字符串的第一个字符串转换成大写
print(message.title()) #将每个单词的首字母大写
print(message.istitle()) #返回布尔类型
print(message.upper()) #全部转换成大写
print(message.lower()) #全部转换成小写

#验证码
import random
s='QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0987654321'
#print(len(s)) #求字符串的长度len
while True:
	code=''
	for i in range(4):
		ran=random.randint(0,len(s)-1 )
		code+=s[ran]
	print(code)
	shuru=input('请输入验证码')
	if shuru.lower()==code.lower():
		print('ok')
		break
	else:
		print('wrong')
		continue

字符串内建函数

20200427
python字符串内建函数
https://www.cnblogs.com/guoguoba/p/5620561.html
url=‘https://www.baidu.com/img/bd_logo1.png’
img=url.rfind(’/’)
print(img)
filename=print(url[img+1:])
print(url[url.rfind(’.’)+1:])#打印bd_logo1.png,rfind右侧起查找
print(url.replace(‘a’,’ ‘,2))#将a替换成空两次
encode编码 decode解码
startswith() endswith()返回值true false判断是否以什么开头,什么结尾
isalpha()是否是字母 isdigit()是否是数字
join() ‘-’.join(‘abc’)将abc用-连接构成一个新的字符串a-b-c
strip(’ aa ‘)去除字符串左右两侧的空格 lstrip()去除左侧空格
split()分割字符串,将分割后的字符串保存到列表中
a.count(’’)求a字符串中’'参数的个数

##上传文件,只支持img,png,jpg格式,其他格式不允许上传
##路径D:\zzz\123.jpg
while True:
	path=input('请输入上传文件路径')
	filename=path[path.rfind('/')+1:]
	print(filename)
	if filename.endswith('jpg') or filename.endswith('png') or filename.endswith('img'):
		print('允许上传')
		break
	else:
		print('不是图片格式,不允许上传')
#输入三次,如果是数字相加
su=0
for i in range(3):
	num=input('输入数字')
	if num.isdigit():
		num=int(num)
		su	+=	num
print(su)
#输入三次,如果是数字相加,不是数字再次输入相加
sum=0
i=0
while i<3:
	num=input('输入数字')
	if num.isdigit():
		num=int(num)
		sum+=num
		i+=1
print(sum)
#输入两个字符串,从第一个字符串中删除第二个字符串中所有的字符
a=input('输入a')
b=input('输入b')
c=''
for i in a:
	if i not in b :
		c+=i
print(c)
a=c
print(a)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值