码迷,mamicode.com
首页 > 其他好文 > 详细

函数和模块的使用

时间:2018-09-24 14:48:29      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:复制   class   oca   个人   round   each   orange   mys   rgs   

 1 """
 2 Python内置函数
 3 
 4 Date:2018-09-17
 5 """
 6 
 7 
 8 def myfilter(mystr):
 9     return len(mystr) == 6
10 
11 
12 print(chr(0x5f20))
13 print(hex(ord(‘张‘)))
14 print(abs(-12.345))
15 print(round(-12.345))
16 print(pow(2, 3))
17 fruits = [‘orange‘, ‘peach‘, ‘durian‘, ‘watermelon‘]
18 print(fruits[slice(1, 3)])
19 fruits2 = list(filter(myfilter, fruits))
20 print(fruits)
21 print(fruits2)

 

 1 """
 2 Python常用模块
 3 """
 4 import time
 5 import shutil
 6 import os
 7 
 8 # 返回当前时间的时间戳
 9 seconds = time.time()
10 print(seconds)
11 # 接受时间戳并返回当地时间下的时间元组
12 locattime = time.localtime(seconds)
13 print(locattime)
14 # 年
15 print(locattime.tm_year)
16 # 月
17 print(locattime.tm_mon)
18 # 日
19 print(locattime.tm_mday)
20 # 接受时间元组,返回一个可读的形式表示时间的字符串。例如:Mon Sep 17 16:18:32 2018
21 asctime = time.asctime(locattime)
22 print(asctime)
23 # 把一个时间元组转化为格式化的时间字符串。
24 strtime = time.strftime(‘%Y-%m-%d %H:%M:%S‘, locattime)
25 print(strtime)
26 # 把一个格式化的时间字符串转化为struct_time
27 make_time = time.strptime(‘2018-9-17‘, ‘%Y-%m-%d‘)
28 print(make_time)
29 
30 # 复制文件内容
31 shutil.copy(‘type.py‘, ‘sum.py‘)
32 
33 # 将字符串转化为命令行在平台中运行
34 os.system(‘dir‘)  # 执行字符串里面的命令
35 
36 os.chdir(‘/‘)  # 进入指定的目录
37 os.system(‘dir‘)
38 os.chdir(‘python‘)
39 os.mkdir(‘test‘)  # 创建一个文件夹
40 os.system(‘dir‘)

 

 1 """
 2 函数的参数
 3 - 位置参数
 4 - 默认参数
 5 - 可变参数
 6 - 关键字参数
 7 - 命名关键字参数
 8 
 9 Date:2018-09-24
10 """
11 
12 
13 #位置参数和默认参数
14 def f1(a, b=5, c=10):
15     return a + b * 2 + c * 3
16 
17 
18 print(f1(1, 2, 3))
19 print(f1(100, 200))
20 print(f1(100))
21 print(f1(c=2, b=3, a=1))
22 
23 
24 # 可变参数
25 def f2(*args):
26     sum = 0
27     for num in args:
28         sum += num
29     return sum
30 
31 
32 print(f2(1, 2, 3))
33 print(f2(1, 2, 3, 4, 5))
34 print(f2())
35 
36 
37 # 关键字参数
38 def f3(**kw):
39     if ‘name‘ in kw:
40         print(‘欢迎您{}!‘.format(kw[‘name‘]))
41     elif ‘tel‘ in kw:
42         print(‘您的联系电话是:{}!‘.format(kw[‘tel‘]))
43     else:
44         print(‘没有您的个人信息!‘)
45 
46 
47 pram = {‘name‘: ‘jason‘, ‘age‘: 38}
48 f3(**pram)
49 f3(name=‘jason‘, age=38, tel=‘1377‘)
50 f3(user=‘jason‘, age=38, tel=‘1377‘)
51 f3(user=‘jason‘, age=38, mobile=‘1377‘)

 

 1 """
 2 作用域问题
 3 
 4 Date:2018-09-24
 5 """
 6 
 7 b = 10
 8 
 9 
10 def foo3():
11     b = 100
12     print(b)
13 
14 
15 foo3()
16 print(b)
17 
18 
19 def foo4():
20     global b
21     b = 200
22     print(b)
23 
24 
25 foo4()
26 print(b)

 

函数和模块的使用

标签:复制   class   oca   个人   round   each   orange   mys   rgs   

原文地址:https://www.cnblogs.com/2018jason/p/9695346.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!