您的位置:首页 > 编程语言 > Python开发

PYTHON 学习笔记10-30-2017

2017-10-30 09:57 656 查看
PYTHON 学习笔记10-30-2017

1, if-else条件语句

2,while / for循环语句

while 条件:

for i in var:

break 跳出循环

continue 跳出该次循环 执行下一次循环

3 数学类函数



4 随机函数

choice(seq) 序列中随机挑选一个元素

seed([x])

shuffle(lst) 序列所有元素随机排列

uniform(x,y) 均匀分布



保留字符

pi π e 自然常数字

5.转义字符



6.字符串字符





7 日期与时间

(1970.1.1–>)

1970之前我就尴尬了

###1
import time
ticks = time.time()
print (ticks)
###2
localtime = time.localtime(time.time())
print (localtime)

"""local current time: time.struct_time(tm_year = 2017,tm_mon=10,tm_mday =30,tm_hour=9,tm_min=46,tm_sec=32,tm_wday=1,tm_yday=不知道,tm_isdst=0)
"""




但是不方便使用

下面就很好了

localtime = time.asctime(time.localtime(time.time()))
'''
输出类似于这样的Thu, Mar_17 14:46:53 2016
'''


import calendar

cal = calendar.month(2008,1)
print (cal)
'''
将会输出 2008年1月份的月历
'''




.

8 python 自定义函数

#自定义函数
'''
def functionname( parameters ):
"函数_文档字符串"
function_suite
return [expression]
'''

#匿名函数
'''
lambda [arg1 [,arg2,.....argn]]:expression
'''
sum = lambda arg1, arg2: arg1 + arg2;


变量的作用范围:全局变量 局部变量

9 #键盘输入

input() #可以接受python表达式(输入:[x*5 for x in range(2,10,2)]

输出:[10,20,30,40] 2,4,6,8乘以5)

raw_input()

str = raw_input("Please enter:");
print "你输入的内容是: ", str

str = input("Please enter:");
print "你输入的内容是: ", str


10 文件

#打开与关闭文件
# 打开一个文件
fo = open("foo.txt", "wb")
print "文件名: ", fo.name
print "是否已关闭 : ", fo.closed
print "访问模式 : ", fo.mode
print "末尾是否强制加空格 : ", fo.softspace

# 关闭文件
fo.close()

# 写入文件
fo
aadd
= open("foo.txt", "wb")
fo.write( "www.runoob.com!\nVery good site!\n");
fo.close()

# 文件读取
fo = open("foo.txt", "r+")
str = fo.read(10);
print "读取的字符串是 : ", str
# 关闭打开的文件
fo.close()

# 打开一个文件
fo = open("foo.txt", "r+")
str = fo.read(10);
print "读取的字符串是 : ", str

# 查找当前位置
position = fo.tell();
print "当前文件位置 : ", position

# 把指针再次重新定位到文件开头
position = fo.seek(0, 0);
str = fo.read(10);

#如果不重新定位到开头位置  输出com!
#www.runoob.com!\nVery good site!\n  不读取到下一行

print "重新读取字符串 : ", str
# 关闭打开的文件
fo.close()


OS文件重命名 删除等操作

import os

# 重命名文件test1.txt到test2.txt。
os.rename( "test1.txt", "test2.txt" )

import os

# 删除一个已经存在的文件test2.txt
os.remove("test2.txt")


10 异常处理



try-except

#异常处理
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find file or read data"
else:
print "Written content in the file successfully"
fh.close()
#输出:"Written content in the file successfully

try:
fh = open("testfile", "r")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can\'t find file or read data"
else:
print "Written content in the file successfully"

#输出 Error: can\'t find file or read data(文件应为w写入方式)

#try-finally 模式 两部分都会执行,即使try部分error
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
finally:
print "Error: can\'t find file or read data"
#输出:Error: can\'t find file or read data(即使写入成功也打印出来finally部分的)

try:
fh = open("testfile", "w")
try:
fh.write("This is my test file for exception handling!!")
finally:
print "Going to close the file"
fh.close()
except IOError:
print "Error: can\'t find file or read data"
#输出:Going to close the file 如果有错 就继续执行except部分

def temp_convert(var):
try:
return int(var)
except ValueError, Argument:
print "The argument does not contain numbers\n", Argument

# Call above function here.
temp_convert("xyz");
#输出:The argument does not contain numbers\n
#invalid Iiteral for int() with base 10 : 'xyz'
#意思是 要求传整形  结果进来一个字符串


这部分不太懂………

#异常触发
def functionName( level ):
if level < 1:
raise "Invalid level!", level
# The code below to this would not be executed
# if we raise the exception

try:
Business Logic here...
except "Invalid level!":
Exception handling here...
else:
Rest of the code here...

#自定义异常
class Networkerror(RuntimeError):
def __init__(self, arg):
self.args = arg

try:
raise Networkerror("Bad hostname")
except Networkerror,e:
print e.args
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: