总结
相关了解:“Python是一种多范式,通用,解释的高级编程语言。Python允许程序员使用不同的编程风格来创建简单或复杂的程序,获得更快的结果并编写代码,就像用人类语言说话一样。” 今天学了python基础。
pythob基础题
1.请手写一个函数,用来去出1-80(均包括)中7的倍数和7的所有整数
def l(): for i in range(1,81): if i%7==0 or '7' in str(i): print(i) #l()
2. str='那车水马龙的人世间,那样地来 那样地去,太匆忙’ 写一个函数,输出最后一次出现'那'的下标。
str = "那车水马龙的人世间,那样地来 那样地去,太匆忙" index = str.rfind("那")
rfind反向查找
print(index)
1. str = " fgh " 写一个函数,只去掉字符串右侧的空格,左侧的空格保留
def x(): str = " fgh " str = str.rstrip() # 去右侧空白 print(str) x()
2. 输入10个数字到列表中,如果输入的不是数字,则跳过,不存
def y(): lxy=[] b=1 while True: b += 1 if len(lxy)==5: break a = input("请输入数字:") if a.isdigit(): a = int(a) lxy.append(a) else: continue print(lxy) y()
3. 写一个函数,可以判断一个字符串是否为回文例子qwewq,函数返回true或者false
lxy = "qwewq" print(lxy[::-1] == lxy)
4. 请手写一个函数,可以打印出 I'm "ok" it's your's 注意必须是原样输出
def w(): #定义函数 print('I’m "ok" it’s your’s') #输出 w() #函数调用
5. str2 = "This is the voa special English,health,report" 写一个函数,统计字符串中单词出现的个数,注意是单词而不是字母
def r(): str2 = "This is the voa special English,health,report" str2 = str2.replace(","," ") # 替换符号一致 l = str2.split() #分割 print(len(l)) #计算长度输出 r()
6. My_str = ‘11sdsfsdf45sfxcv67qwe_9’ 手写一个函数,计算出字符串中所有数字的和
'''
计算字符串中所有数字的和
1.字符串中只有小写字母和数字
2.数字可能连续,也可能不连续
3.连续数字要当做一个数处理
如:'12abc34dc5' => 12 + 34 + 5 => 51
'''
My_str = '11sdsfsdf45sfxcv67qwe_9' def sum_of_num(My_str): num = 0 for i in My_str: if not i.isdigit(): My_str = My_str.replace(i, ' ') print(My_str) lt = My_str.split(' ') for j in lt: if j.isdigit(): num += int(j) return num print(sum_of_num(My_str))
7. s = 'test' 写一个函数,能将字符串中的网址提取出来,即提取出www.test.com
s = 'test' print(s.split('"')[1])
8. str = "卡巴斯基#杀毒软件#免费版#俄罗斯#" 手写一个函数,将该字符串解析为['卡巴斯基', '杀毒软件', '免费版', '俄罗斯']
str = "卡巴斯基#杀毒软件#免费版#俄罗斯#" #截取到-1,-1不取 str = str[:-1] 按照#分割 print(str.split("#")) split分开
1. str = " fgh " 写一个函数,只去掉字符串右侧的空格,左侧的空格保留
def func(): str = " fgh " str = str.rstrip() # 去右侧空白 print(str) func()
2. 输入10个数字到列表中,如果输入的不是数字,则跳过,不存
def s(): l=[] b=1 while True: b += 1 if len(l)==5: break a = input("请输入数字:") if a.isdigit(): a = int(a) l.append(a) else: continue print(l) s()
3. 写一个函数,可以判断一个字符串是否为回文例子qwewq,函数返回true或者false
def func(): a = input("输入字符串:") if a == a[::-1]: # 判断 a 等于 倒序的 a 就是回文数 return True # 是输出 True else: return False print(func())
4. 请手写一个函数,可以打印出 I'm "ok" it's your's 注意必须是原样输出
def s(): print('I’m "ok" it’s your’s')
s()
5. str2 = "This is the voa special English,health,report" 写一个函数,统计字符串中单词出现的个数,注意是单词而不是字母
def func(): str2 = "This is the voa special English,health,report" str2 = str2.replace(","," ") # 替换符号一致 l = str2.split() #分割 print(len(l)) #计算长度输出 func()
6. My_str = ‘11sdsfsdf45sfxcv67qwe_9’ 手写一个函数,计算出字符串中所有数字的和
def func(): #定义函数 l=[] #创建列表 My_str = '11sdsfsdf45sfxcv67qwe_9' #字符串 for i in My_str: #循环字符串 if i.isdigit(): #判断是否数字 l.append(int(i)) #是添加列表 print(sum(l)) #算和输出 func() #调用
7. s = 'test' 写一个函数,能将字符串中的网址提取出来,即提取出www.test.com
def func(): s = 'test' l = s.split('"') #按 " 分割 print(l[1]) func()
8. str = "卡巴斯基#杀毒软件#免费版#俄罗斯#" 手写一个函数,将该字符串解析为['卡巴斯基', '杀毒软件', '免费版', '俄罗斯']
def func(): str = "卡巴斯基#杀毒软件#免费版#俄罗斯#" l=str.split("#") #用#分割 del l[-1] #删除最后一个元素 print(l) #输出
func()
变量的定义
程序就是用来处理数据的,而变量就是用来存储数据的
Python3 的六个标准数据类型中: 不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组); 可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)。
###变量的命名规则 在Python程序中,变量是用一个变量名表示,变量名必须是大小写英文、数字和下划线(_)的组合,且不能用数字开头
###字符串常用方法 #1. find方法可以在一个较长的字符串中查找子串,他返回子串所在位置的最左端索引,如果没有找到则返回-1
a = 'abcdefghijk' print(a.find('abc')) #the result : 0 print(a.find('abc',10,100)) #the result : 11 指定查找的起始和结束查找位置
#2. join方法是非常重要的字符串方法,他是split方法的逆方法,用来连接序列中的元素,并且需要被连接的元素都必须是字符串。 a = ['1','2','3'] print('+'.join(a)) #the result : 1+2+3
#3. split方法,是一个非常重要的字符串,它是join的逆方法,用来将字符串分割成序列 print('1+2+3+4'.split('+')) #the result : ['1', '2', '3', '4']
#4. strip 方法返回去除首位空格(不包括内部)的字符串 print(" test test ".strip()) #the result :“test test”
#5. replace方法返回某字符串所有匹配项均被替换之后得到字符串 print("This is a test".replace('is','is_test')) #the result : This_test is_test a test