1.7.2 python练习题15道

1、求出1 / 1 + 1 / 3 + 1 / 5......+1 / 99的和 (1分之一+1分之三+1分支5....)

2、用循环语句,计算2 - 10之间整数的循环相乘的值 (2*3*4*5....10)

3、用for循环打印九九乘法表

4、求每个字符串中字符出现的个数如:helloworld

5、实现把字符串str = "duoceshi"中任意字母变为大写(通过输入语句来实现)

6、分别打印100以内的所有偶数和奇数并存入不同的列表当中

7、请写一段Python代码实现删除一个list = [1, 3, 6, 9, 1, 8]#

8、将字符串类似:"k:1|k3:2|k2:9" 处理成key:value或json格式,比如{"k": "1", "k3": "2"}

9、把字符串user_controller转换为驼峰命名UserController大驼峰在java用作变量命名

(前英文为大写后英文为小写) 小驼峰:作为变量命名

10、给一组无规律的数据从大到小或从小到大进行排序如:list = [2, 6, 9, 10, 18, 15, 1]

11、分析以下数字的规律, 1 1 2 3 5 8 13 21 34用Python语言编程实现输出

#分析题目:根据规律 1+1=2 2+1=3 2+3=5 3+5=8....

#此为斐波那契数列 (考试题非常多次题目)

12、如有两个list:a =['a','b','c','d','e']

b =[1,2,3,4,5] 将a中的元素作为key b中的元素作为value,将a,b合并为字典

13、有如下列表,统计列表中的字符串出现的次数

a = ['apple','banana','apple','tomao','orange','apple','banana','watermeton']

14、、列表推导式求出列表所有奇数并构造新列表 a =[1,2,3,4,5,6,7,8,9,10]

15、有如下url地址, 要求实现截取出"?"号后面的参数, 并将参数以"key value"的键值形式保存起来, 并最终通过#get(key)的方式取出对应的value值。

#url地址如下:http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0"

================================================================================================

练习讲解:

1、求出1 / 1 + 1 / 3 + 1 / 5......+1 / 99的和 (1分之一+1分之三+1分支5....)

方法1:

|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 | sum``=``0 for i ``in range``(``1``,``100``,``2``): ``sum = 1``/``i ``+ sum print``(``sum``)<br><br> 方法``2``:``sum``=``0``<br>``for i ``in range``(``1``,``100``):<br> ``if i``%``2``=``=``1``:<br> ``sum``=``sum``+``1``/``i<br>``print``(``sum``) ``#2.937774848474907<br>方法3:a=1<br>sum=0<br>while a<100:<br> sum+=1/a<br> a+=2<br>print(sum) |

2、用循环语句,计算2 - 10之间整数的循环相乘的值 (2*3*4*5....10)

|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 | 方法``1``:<br>i``=``2 a``=``1 while i<``11``: ``a``=``a``*``i ``i``+``=``1 print``(a)<br> 方法``2``:ji``=``1``<br>``for i ``in range``(``2``,``11``):<br> ji``=``ji``*``i<br>``print``(ji) ``#3628800<br>方法3:a=1<br>b=1<br>while a<10:<br> a += 1<br> b *= a<br>print(b)<br><br> |

3、用for循环打印九九乘法表

\n 表示换行,相当于一个回车键

\t 表示4个空字符,类似文旦中的缩进功能,按tab

end=" " 不换行

f 是format的函数的缩写,用于格式化输出

|-------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 | 方法``1``: for i ``in range``(``1``,``10``): ``for j ``in range``(``1``,``10``): ``if j<``=``i: ``print``(``'%d*%d=%d'``%``(i,j,i``*``j),end``=``' '``) ``print``(``'\n'``)<br> 方法``2``:``for i ``in range``(``1``,``10``):<br> ``for j ``in range``(``1``,i``+``1``):<br> ``print``(f``"{i}*{j} ={i*j}"``,end``=``" "``)<br> ``print``()<br> 方法``3``:``for i ``in range``(``1``,``10``):<br> ``for j ``in range``(``1``,i``+``1``):<br> ``print``(``'%d*%d=%d'``%``(i,j,i``*``j),end``=``' ' )<br> ``print``()<br> 方法``4``:i``=``1``<br>``while i<``10``:<br> j``=``1``<br> ``while j<``=``i:<br> ``print``(j,``'x'``,i,``'='``,i``*``j,end``=``' '``)<br> j``+``=``1``<br> ``print``()<br> i``+``=``1 |

4、求每个字符串中字符出现的个数如:helloworld

|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | 方法``1``: str1``=``'helloworld' str2``=``'' for i ``in str1: ``if i ``not in str2: ``print``(``'str1中有%d个%s'``%``(str1.count(i),i)) ``str2``=``str2``+``i<br> 方法``2``:a``=``'helloworld'``<br>count``=``{}<br>``for i ``in a:<br> ``if i ``in count:<br> count[i]``+``=``1``<br> ``else``:<br> count[i]``=``1``<br>``print``(count)<br> 方法``3``:``sum``=``0``<br>``for i ``in s2:<br> ``for j ``in s1:<br> ``if j``=``=``i:<br> ``sum``=``sum``+``1``<br> ``print``(f``"{i}出现的次数为:{sum}"``)<br> ``sum``=``0``<br> 方法``4``:str1``=``'helloworld'``<br>``for i ``in set``(str1):<br> c``=``str1.count(i)<br> ``print``(``'{}出现的次数:{}次'``.``format``(i, c)) 方法``5``:str1``=``'hello world'``<br>d``=``{}<br>``for i ``in str1:<br> d[i]``=``str1.count(i)<br>``print``(d)<br><br>方法``6``:str1``=``'hello world'``<br>``print``({i:str1.count(i) ``for i ``in str1})<br><br> |

5、实现把字符串str = "duoceshi"中任意字母变为大写(通过输入语句来实现)

|-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 | 方法``1``; str = "duoceshi" s ``= input``(``"请输入要变大小的字母:"``) str1``=``str``.replace(s,s.upper()) ``#replace替换;str.upper大写(str文件中字母大写) print``(str1) <br> 方法``2``:``str = "duoceshi"``<br>s ``= input``(``"请输入要变大小的字母:"``)<br>``if s ``in str``:<br> ``for i ``in s:<br> j``=``i.upper()<br> ``print``(``str``.replace(s,j))<br>``else``:<br> ``print``(``"字母不存在"``)<br> |

6、分别打印100以内的所有偶数和奇数并存入不同的列表当中

|-------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 | 方法``1``:<br>ji``=``[] ou``=``[] for i ``in range``(``1``,``101``): ``if i ``%``2 =``=``0``: ``ou.append(i) ``else``: ``ji.append(i) print``(``"奇数"``,ji) print``(``"偶数"``,ou) <br> 方法``2``:list1``=``[]<br>list2``=``[]<br>a``=``-``1``<br>b``=``0``<br>``while a<``99``:<br> a``+``=``2``<br> list1.append(a)<br>``print``(list1)<br>``while b<``99``:<br> b``+``=``2``<br> list2.append(b)<br>``print``(list2)<br> 方法``3``:a``=``1``<br>b``=``0``<br>``while a<``100``:<br> list1.append(a)<br> a``+``=``2``<br>``print``(list1)<br>``while b<``100``:<br> list2.append(b)<br> b``+``=``2``<br>``print``(list2) |

7、请写一段Python代码实现删除一个list = [1, 3, 6, 9, 1, 8]#去重 ( 重点

|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 | 方法``1``: list1 ``= [``1``, ``3``, ``6``, ``9``, ``1``, ``8``] c``=``set``(list1) print``(c) <br> 方法``2``:list1 ``= [``1``, ``3``, ``6``, ``9``, ``1``, ``8``]<br>dict1``=``{}<br>``for i ``in list1:<br> ``if i ``in dict1:<br> dict1[i]``=``dict1[i]``+``1``<br> ``else``:<br> dict1[i]``=``1``<br>``print``(``list``(dict1.keys()))<br><br> 方法``3``:通过``not in 不存在list1 ``= [``1``, ``3``, ``6``, ``9``, ``1``, ``8``]<br>list2``=``[]<br>``for i ``in list1:<br> ``if i ``not in list2:<br> list2.append(i)<br>``print``(list2) 方法``4``:通过字典中键的唯一性list1 ``= [``1``, ``3``, ``6``, ``9``, ``1``, ``8``]<br>``dict``=``{}<br>c``=``dict``.fromkeys(list1)<br>``print``(``list``(c))<br> 方法``5``:通过统计的方法list1 ``= [``1``, ``3``, ``6``, ``9``, ``1``, ``8``]<br>``for i ``in list1:<br> ``if list1.count(i)>``1``:<br> list1.remove(i)<br>``print``(list1)<br> |

8、将字符串类似:"k:1|k3:2|k2:9" 处理成key:value或json格式,比如{"k": "1", "k3": "2"}

|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | 方法``1``: str1``=``"k:1|k3:2|k2:9" str2``=``str1.split(``"|"``) dict1``=``{} for i ``in str2: ``dict1[i.split(``":"``)[``0``]] ``= i.split(``":"``)[``1``] print``(dict1)<br> 方法``2``:``def parse_string_to_dict(input_string):<br> ``# 使用'|'分割字符串<br> pairs = input_string.split('|')<br> # 创建一个空字典来存储结果<br> result_dict = {}<br> # 遍历每对键值<br> for pair in pairs:<br> # 使用':'分割键和值<br> key, value = pair.split(':')<br> # 将键和值添加到字典中<br> result_dict[key] = value<br> return result_dict<br># 使用函数<br>input_str = "k:1|k3:2|k2:9"<br>output_dict = parse_string_to_dict(input_str)<br>print(output_dict) # 输出: {'k': '1', 'k3': '2', 'k2': '9'}<br> 方法3:str1="k:1|k3:2|k2:9"<br>str2=str1.split("|")<br>dict1={}<br>for i in str2:<br> k,v=i.split(":")<br> dict1[k]=v<br>print(dict1) |

9、把字符串user_controller转换为驼峰命名UserController大驼峰在java用作变量命名

(前英文为大写后英文为小写) 小驼峰:作为变量命名

|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | 方法``1``: str_old``=``"user_controller" list1``=``str_old.split(``"_"``) str_new``=``"" for i ``in list1: ``str_new``=``str_new``+``str``(i).capitalize() print``(str_new) <br><br> 方法``2``:<br>``def to_camel_case(s):<br> components ``= s.split(``'_'``) ``# 将字符串按下划线分割成组件<br> return ''.join(x.capitalize() for x in components) # 将每个组件的首字母大写,并拼接起来<br># 示例字符串<br>s = "user_controller"<br># 转换为驼峰命名法<br>camel_case_str = to_camel_case(s)<br># 输出结果<br>print(camel_case_str) # 输出: UserController<br> 方法3:str_old="user_controller"<br>list1=str_old.split("_")<br>print(list1[0].title()+list1[1].title())<br>方法4:str_old="user_controller"<br>list1=str_old.split("_")<br>print(list1[0].capitalize()+list1[1].capitalize())<br> |

10、给一组无规律的数据从大到小或从小到大进行排序如:list = [2, 6, 9, 10, 18, 15, 1]

|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 | 方法``1``: list1 ``= [``2``, ``6``, ``9``, ``10``, ``18``, ``15``, ``1``] list1.sort() print``(list1) ``#[1, 2, 6, 9, 10, 15, 18] <br> 方法2:list = [2, 6, 9, 10, 18, 15, 1]<br>list.sort(reverse=True)<br>print(list)<br> 方法3:list = [2, 6, 9, 10, 18, 15, 1]<br>print(sorted(list,reverse=False)) #[1, 2, 6, 9, 10, 15, 18]<br>print(sorted(list,reverse=True)) #[18, 15, 10, 9, 6, 2, 1]<br> 方法4:冒泡排序<br>list = [2, 6, 9, 10, 18, 15, 1]<br>for i in range(0,len(list)-1):<br> for j in range(0,len(list)-1):<br> if list[j]> list[j+1] :<br> list[j],list[j+1] = list[j+1], list[j]<br>print(list) |

11、分析以下数字的规律, 1 1 2 3 5 8 13 21 34用Python语言编程实现输出

#分析题目:根据规律 1+1=2 2+1=3 2+3=5 3+5=8....

#此为斐波那契数列 (考试题非常多次题目)

如:兔子的繁殖问题:如果一开始有一对兔子,它们每月生育一对兔子,小兔在出生后一个月又开始生育且繁殖情况与最初的那对兔子一样,那么一年后有多少对兔子?

答案是,每月兔子的总数可以用以下数列表示:1,1,2,3,5,8,13,21,34,55,89,144,233...

方法1:

|-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 | <em>``list``=``[``1``,``1``] for i ``in range``(``10``): ``list``.append(``list``[``-``1``]``+``list``[``-``2``]) print``(``list``) <br> 方法``2``:<br><``/``em>a``=``0``<br>b``=``1``<br>``for i ``in range``(``11``):<br> ``sum``=``a``+``b<br> a``=``b<br> b``=``sum``<br> ``print``(a,end``=``" "``)<br> 方法``3``:a,b``=``1``,``1``<br>``print``(a,end``=``" "``)<br>``print``(b,end``=``" "``)<br>``for i ``in range``(``10``):<br> a,b``=``b,a``+``b<br> ``print``(b,end``=``" "``)<br> 方法``4``:list2``=``[]<br>``for i ``in range``(``10``):<br> ``if i``=``=``0 or i``=``=``1 :<br> list2.append(``1``)<br> ``else``:<br> list2.append(list2[i``-``1``]``+``list2[i``-``2``])<br>``print``(list2)<em ``id``=``"__mceDel"``><br> <``/``em> |

12、如有两个list:a =['a','b','c','d','e']

b =[1,2,3,4,5] 将a中的元素作为key b中的元素作为value,将a,b合并为字典

|-----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 | 方法``1``: a ``=``[``'a'``, ``'b'``, ``'c' , ``'d'``, ``'e'``] b ``=``[``1``, ``2``, ``3``, ``4``, ``5``] c ``= dict``(``zip``(a,b)) print``(c) |

13、有如下列表,统计列表中的字符串出现的次数

a = ['apple','banana','apple','tomao','orange','apple','banana','watermeton']

|-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 | 方法``1``: a ``= [``'apple'``, ``'banana'``, ``'apple'``, ``'tomao'``, ``'orange'``, ``'apple'``, ``'banana'``, 'waterm b ``=``set``(a) for i ``in b: ``total``=``0 ``for j ``in a: ``if i ``=``=``j: ``total``=``total``+``1 ``print``(f``"{i}出现的次数为:{total}次"``) <br> 方法``2``: ``#:使用字典<br># python<br># 复制代码<br>a = ['apple', 'banana', 'apple', 'tomao', 'orange', 'apple', 'banana', 'watermelon']<br># 初始化一个空字典来存储字符串及其出现的次数<br>count_dict = {}<br># 遍历列表中的每个字符串<br>for item in a:<br> # 如果字符串已经在字典中,增加其计数<br> if item in count_dict:<br> count_dict[item] += 1<br> # 如果字符串不在字典中,添加到字典并设置计数为1<br> else:<br> count_dict[item] = 1<br> # 打印结果<br>print(count_dict) #{'apple': 3, 'banana': 2, 'tomao': 1, 'orange': 1, 'watermelon': 1}<br> 方法3:a = ['apple', 'banana', 'apple', 'tomao', 'orange', 'apple', 'banana', 'watermelon']<br>count_dict = {}<br>for i in a:<br> count_dict[i] = a.count(i)<br>print(count_dict)<br> 方法4:a = ['apple','banana','apple','tomao','orange','apple','banana','watermeton']<br>for i in set(a):<br> b=a.count(i)<br> print('{}出现的次数:{}次'.format(i,b)) |

14、列表推导式求出列表所有奇数并构造新列表 a =[1,2,3,4,5,6,7,8,9,10]

|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | 方法``1``: list4``=``[``1``,``2``,``3``,``4``,``5``,``6``,``7``,``8``,``9``,``10``] list5``=``[] for i ``in list4: ``if i``%``2``=``=``1``: ``list5.append(i) print``(list5) |

15、有如下url地址, 要求实现截取出"?"号后面的参数, 并将参数以"key value"的键值形式保存起来, 并最终通过#get(key)的方式取出对应的value值。

#url地址如下:http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0"

|----------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 | 方法``1``: url``=``r"http:``/``/``ip:port``/``extername``/``get_account_trade_record.json?page_size``=``20``&page_index``=``1``&user_i str1``=``url.split(``"?"``) [``1``] print``(str1) str2``=``str1.split(``"&"``) print``(str2) d``=``{} for i ``in str2: ``k,v``=``i.split(``"="``) ``d[k]``=``v print``(d) print``(d.get(``"user_id"``)) <br><br> 方法``2``:url1 ``=``"http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user<br>str1="``"<br>dict1={}<br>for i in range(url1.index("``?``")+1,len(url1)):<br> str1=str1+url1[i]<br>#print(str1)<br>str2=str1.split("``&``")<br>#print(str2)<br>for i in str2:<br> str3=i.split("``=``")<br> dict1[str3[``0``]]``=``str3[``1``]<br>``print``(dict1) |

相关推荐
Gu Gu Study3 分钟前
枚举与lambda表达式,枚举实现单例模式为什么是安全的,lambda表达式与函数式接口的小九九~
java·开发语言
时光の尘18 分钟前
C语言菜鸟入门·关键字·float以及double的用法
运维·服务器·c语言·开发语言·stm32·单片机·c
以后不吃煲仔饭31 分钟前
Java基础夯实——2.7 线程上下文切换
java·开发语言
进阶的架构师32 分钟前
2024年Java面试题及答案整理(1000+面试题附答案解析)
java·开发语言
前端拾光者36 分钟前
利用D3.js实现数据可视化的简单示例
开发语言·javascript·信息可视化
程序猿阿伟38 分钟前
《C++ 实现区块链:区块时间戳的存储与验证机制解析》
开发语言·c++·区块链
戎梓漩1 小时前
windows下安装curl,并集成到visual studio
ide·windows·visual studio
傻啦嘿哟1 小时前
如何使用 Python 开发一个简单的文本数据转换为 Excel 工具
开发语言·python·excel
大数据编程之光1 小时前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
初九之潜龙勿用1 小时前
C#校验画布签名图片是否为空白
开发语言·ui·c#·.net