1. python中tuple和list的转换
>>>tuple1 =(1,2,3)
>>>list1 = list(tuple1)
>>>list1
[1, 2, 3]
>>>tuple2 = tuple(list1)
>>>tuple2
(1, 2, 3)
2. except的用法和作用
try:
pass
except Exception as e:
print(e)
finally:
pass
捕获try except中间代码发生的异常,如果发生异常执行except的代码,不管是否发生异常都执行finally中的代码。
except可以有0个或多个,如果有多个从上到下依次根据异常类型匹配,匹配某个Exception再执行对应的except中代码。
Exception
是常规错误的基类。
#第一种形式
try:
a=b
b=c
except Exception,e:
print (Exception,":",e)
<type 'exceptions.Exception'> : name 'b' is not defined
#第二种形式
try:
a=b
b=c
except Exception as e:
print (e)
name 'b' is not defined
3. 求和:1+3!+5!+7!+9!+50!
# 求1+3!+5!+7!+9!
m = 0
for i in range(1,6):
n = 1
for j in range(1,2*i):
n = n*j
m += n
# 求50!
l = 1
for x in range(1,51):
l = l*x
# 两部分和
sum = m+l
print(sum)
4. 大小写互换
把字符串“HELLO PYTHON”从大写字母全部转换成小写字母并换行显示;
然后输出到计算机c盘的hello.txt文件中保存
str = 'HELLO PYTHON'
with open(r'C:/hello.txt','a') as f: # with open('C:\\hello.txt','a') as f:
for letter in str:
letter = letter.lower()
f.write(letter)
f.write('\n')
python脚本中为什么要在目录前加一个r
r是保持字符串原始值的意思,就是说不对其中的符号进行转义。因为windows下的目录字符串中通常有斜杠”\”,而斜杠在Python的字符串中有转义的作用。例如:\n表示换行如果路径中有\new就会被转义。加上r就是为了避免这种情况。
在不同系统的情况系 windows下是,linux和unix下是/ 但在win中没有本质区别。
但是由于 \ 也是转义字符的起始字符, 所以, 路径中的 \ 通常需要使用 \,如果是 / 就不需要使用转义了
如C中文件操作,c:\kkk\dd.txt c:/kkk/dd.txt一样
open() 将会返回一个 file 对象,基本语法格式如下:
open(filename, mode)
filename:包含了你要访问的文件名称的字符串值。
mode:决定了打开文件的模式:只读,写入,追加等。所有可取值见如下的完全列表。这个参数是非强制的,默认文件访问模式为只读(r)。
a 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
5. 判定当前日期在本年是第几天
采用任意数据结构方法,输入年、月、日后能判定当前日期在本年是第几天
def judge_dayth(y=None,m=None,d=None):
# 判断是否是润年
def judge_dayth(y,m,d):
dayth = 0
if y%4==0 and y%100 != 0 or y%400==0:
mlist = [31,29,31,30,31,30,31,31,30,31]
else:
mlist = [31,28,31,30,31,30,31,31,30,31]
if m == 1:
dayth = d
else:
for i in range(m-1):
dayth += mlist[i]
dayth += d
print(dayth)
6. 找出数组中第二大的整数
给定一个值为整数的数组int_array,找出其中第二大的整数
说明:如果最大的整数在int_array中出现不止一次,则最大整数为第二大整数。
例:
输入:[1,2,3,4,5]
输出:4
输入:[5,5,4,4,3]
输出:5
json.loads 用于解码JSON 字符串。该函数返回 Python 字段的数据类型
import json
def find_max(int_array):
max_int = 0
for i in int_array:
if i > max_int:
max_int = i
return max_int
int_array = json.loads(input()) # 将字符串转化为列表
max_int = find_max(int_array)
int_array.remove(max_int)
max_int = find_max(int_array)
print(max_int)
remove()方法语法:list.remove(obj)
remove() 函数用于移除列表中某个值的第一个匹配项。
7. 列举几种常用的python包及其功能与用法
- os模块
os模块包装了不同操作系统的通用接口,使用户在不同操作系统下,可以使用相同的函数接口,返回相同结构的结果。
# 输出给定文件夹中文件的全路径
import os
def print_directory(spath):
files_list = os.listdir(spath)
for file in files_list:
print(os.path.realpath(file))
#获得当前工作目录
print os.getcwd()#获得当前工作目录
print os.path.abspath('.')#获得当前工作目录
print os.path.abspath('..')#获得当前工作目录的父目录
print os.path.abspath(os.curdir)#获得当前工作目录
- random模块: 随机数
random.random() #(0,1)随机数
random.randint(1,100) #1到100以内随机整数
- re 模块: 使 Python 语言拥有全部的正则表达式功能。
re.match
re.search
9. 合并列表
extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
extend()方法语法: list.extend(seq)
>>> a = [1,2,3,4]
>>> b = [5,6,7]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6, 7]
9. 正序排列、倒序排列和逆序排列
如有列表a
a.sort()
正序排列
a.sort(reverse=True)
倒序排列
a.reverse()
逆序排列
>>> a=[17,2,8,9,3,4]
>>> a.sort() #正序排列
>>> a
[2, 3, 4, 8, 9, 17]
>>> b=[17,2,8,9,3,4]
>>> b.sort(reverse=True) #倒序排列
>>> b
[17, 9, 8, 4, 3, 2]
>>> c=[17,2,8,9,3,4]
>>> c.reverse() #逆序排列
>>> c
[4, 3, 9, 8, 2, 17]
10. 使用python将字符串“1.2.3.4”转换为字符串“4|3|2|1”
>>> str = "1.2.3.4"
>>> li = str.split(".")
>>> li
['1', '2', '3', '4']
>>> li.reverse()
>>> li
['4', '3', '2', '1']
>>> str = "|".join(li)
>>> str
'4|3|2|1'
split()方法返回分割后的字符串列表。
reverse()方法语法:list.reverse()
该方法没有返回值,但是会对列表的元素进行反向排序。