python----常见练习题
1. 冒泡排序
def bubble_sort(lists):
len_list=len(lists)
for i in range(len_list):
for j in range(len_list-i-1):
if lists[j]>lists[j+1]:
lists[j],lists[j+1]=lists[j+1],lists[j]
print(lists)
return lists
2. 插入排序
def insert_sort(lists):
for i in range(len(lists)):
position=i
while position>0:
if lists[position]<lists[position-1]:
lists[position],lists[position-1]=lists[position-1],lists[position]
position-=1
print(lists)
return lists
3. 列表去重
#第一种方式
s=[1,2,6,3,1,5,2]
list(set(s))
#第二种方式
l=[1,1,6,3,1,5,2]
def duplictae(lists):
L=[]
for i in lists:
if i not in L:
L.append(i)
return L
print(duplictae(l))
4.字符串反转,例如:将string反转输出为:gnirts
#第一种方式:
def str_reverse(str):
return str[::-1]
#第二种方式:
def str_reverse(str):
L=list(str)
L.reverse()
new_str=''.join(L)
return new_str
5.快速交换两个变量值的方法
# 方法一:通过新添加中间变量的方式,交换数值.
def test(a,b):
c=a
a=b
b=c
print(a,b)
# 方法二:(此方法是Python中特有的方法)
# 直接将a,b两个变量放到元组中,再通过元组按照index进行赋值的方式进行重新赋值给两个变量
def test(a,b):
a,b=b,a
print(a,b)
# 方法三:通过简单的逻辑运算进行将两个值进行互换
def test(a,b):
a=a+b
b=a-b
a=a-b
print(a,b) res = test(50,100)
6.删除字符串中的特殊字符,并输出指定字符串
如:str = 'welcome to shui&di',指定输出:shuidi
str = 'welcome to shui&di'
new_str=str.split(' ')[2].split('&')
res = ''.join(new_str)
7.在1-100之间生成一个随机数,并猜所生成的随机数值,根据猜的结果,给出正确、太大、太小的提示,如果5次均未猜对,游戏结束!
num = random.randint(1,100) for i in range(5):
new_num = int(input('请输入猜测数值:'))
if new_num > num:
print('太大了')
elif new_num < num:
print('太小了')
elif new_num == num:
print('猜对了!')
else:
print('游戏结束!')
python----常见练习题的更多相关文章
- python入门练习题1
常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...
- Python常见的错误汇总
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 错误: [错误分析]第二个参数必须为类,否则会报TypeError,所以正确的应 ...
- Python/ MySQL练习题(一)
Python/ MySQL练习题(一) 查询“生物”课程比“物理”课程成绩高的所有学生的学号 SELECT * FROM ( SELECT * FROM course LEFT JOIN score ...
- python/MySQL练习题(二)
python/MySQL练习题(二) 查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T ...
- python常见排序算法解析
python——常见排序算法解析 算法是程序员的灵魂. 下面的博文是我整理的感觉还不错的算法实现 原理的理解是最重要的,我会常回来看看,并坚持每天刷leetcode 本篇主要实现九(八)大排序算法 ...
- python字典练习题
python字典练习题 写代码:有如下字典按照要求实现每一个功能dict = {"k1":"v1","k2":"v2", ...
- Python常见十六个错误集合,你知道那些?
使用python会出现各种各样的错误,以下是Python常见的错误以及解决方法. 1.ValueError: 'Conv2d_1a_3×3' is not a valid scope name 这个是 ...
- Python 常见文件操作的函数示例(转)
转自:http://www.cnblogs.com/txw1958/archive/2012/03/08/2385540.html # -*-coding:utf8 -*- ''''' Python常 ...
- 转 Python常见数据结构整理
http://www.cnblogs.com/jeffwongishandsome/archive/2012/08/05/2623660.html Python常见数据结构整理 Python中常见的数 ...
- Python常见文件操作的函数示例
# -*-coding:utf8 -*- ''''' Python常见文件操作示例 os.path 模块中的路径名访问函数 分隔 basename() 去掉目录路径, 返回文件名 dirname() ...
随机推荐
- C#winform窗体利用系统抓取关闭按钮事件
const int WM_SYSCOMMAND = 0x112; const int SC_CLOSE = 0xF060; const int SC_MINIMIZE = ...
- Java面向对象概述和三大特性
Java 是面向对象的高级编程语言,类和对象是 Java 程序的构成核心.围绕着 Java 类和 Java 对象,有三大基本特性:封装是 Java 类的编写规范.继承是类与类之间联系的一种形式.而多态 ...
- ABP入门系列之3——创建实体/Code First创建数据表
一.首先来看看ABP体系结构 领域层就是业务层,是一个项目的核心,所有业务规则都应该在领域层实现.实体(Entity): 实体代表业务领域的数据和操作,在实践中,通过用来映射成数据库表.仓储(Repo ...
- jmeter java requst请求
https://wangym.iteye.com/blog/731729 http://www.cnblogs.com/yangxia-test/p/4019541.html https://blog ...
- ltp-ddt emmc_dd_rw
emmc_dd_rw EMMC_M_FUNC_DD_RW_500M source "common.sh"; install_modules.sh "emmc"; ...
- Linux内核源码分析 day01——内存寻址
前言 Linux内核源码分析 Antz系统编写已经开始了内核部分了,在编写时同时也参考学习一点Linux内核知识. 自制Antz操作系统 一个自制的操作系统,Antz .半图形化半命令式系统,同时嵌入 ...
- 7-27 Codeforces Round #499 (Div. 2)
C. Fly 链接:http://codeforces.com/group/1EzrFFyOc0/contest/1011/problem/C 题型:binary search .math. 题意:总 ...
- LeetCode120-Triangle-数组,动态规划
题目描述 Problem Description: Given a triangle, find the minimum path sum from top to bottom. Each ste ...
- php rsa
<?php $res=openssl_pkey_new(); // Get private key $ok = openssl_pkey_export($res, $privkey); // G ...
- [mysql]You must reset your password using ALTER USER statement before executing this statement.
原因分析: MySQL版本5.6.6版本起,添加了password_expired功能,它允许设置用户的过期时间.这个特性已经添加到mysql.user数据表,但是它的默认值是”N”,可以使用ALTE ...