current_number = 1
while current_number <= 10:="" print('current_number')="" current_number="" +="1" <="" pre="">
46 while循环使用标志的用法:
flag = True
while flag:
message = input(prompt)
47 列表之间移动元素,用法:while list[]:
newlist.append(list[].pop())
48 删除特定的元素,用法:while element in list:
list.remove(element)
49 形参与实参的理解,用法:def method(username): # username形参
method('zhangsan') # zhangsan实参 50 位置参数,用法:def describe(name,age):
describe('zhangsan',22) # 参数位置对应传递
51 关键字实参是传递函数的名称-值对,用法:def describe(name,age):
describe(name='zhangsan',age=22) # 关键字实参
describe(age=22,name='zhangsan') # 关键字实参,位置不重要
52 形参设置默认值,用法:def describe(name=’lisi’,age):
53 返回值,用法:def describe(name='lisi',age):
des = name + str(age)
return des # 可以返回字典、列表等形式
54 列表参数,用法:lists = ['huangsan','lisi','wangjun','denghui']
def cats_name(lists):
for list in lists:
print("'my love is :t'+list".title()) 55 传递任意参数,用法:def cats_name(*cats): # 可以传递多个形参
56 位置实参和任意数量实参:def cats_name(parament1,parament2,*cats): # 可以传递多个形参
cats_name(para1,para2,para3,para4,...) 57 任意实参和关键字实参,用法:(cats.py)def cats_name(parament1,parament2,**cats): # 可以传递多个形参
cats_name(para1,para2,para3,newname=para4,...)
58 导入整个模块,用法:import cats
cats.cats_name(para1,para2,para3,newname=para4,...) 59 导入特定的函数,用法:from nltk import map_tag as mt
60 导入模块所有函数,用法:from nltk import *
61 形参默认时,两边不能为空,用法:def function_name(parament_0,parament_1='default')
62 类的命名是驼峰型即首字母大写。
63 __init__(self,papa1,para2):避免python默认方法跟普通方法名称冲突,self必不可少,必须位于其他形参的前面,指向实例本身。
64 类的继承,用法:# 父类
class Animal():
def __init__(self,name,age):
self.name = name
self.age = age
def animal_call(self):
print('this is '+self.name.title()+' call.')
# 子类
class Cat(Animal):
def __init__(self,name,age,color):
super().__init__(name,age)
self.color =color
def cat_color(self):
my_color = 'the cat is '+self.color
print(my_color)
return my_color
if __name__ == '__main__':
cat = Cat('tom',22,'blue')
cat.animal_call()
strs=cat.cat_color()
65 几种类的导入方式,用法:from cat import Cat # 导入单个类
from cat import Animal,Cat # 导入多个类
from cat # 导入整个模块
from cat import * # 导入所有类
66 读取文本文件,并删除字符串始末空白,用法:my_str = line.strip()
67 opem()自动创建文件路径,若路径不存在时候。
68 异常代码块:try-except
69 split()创建单词列表str = 'this is a string'
str.split()
['this','is','a','string']
70 存储数据json.dump()和json.load()import json
# 父类
class Animal():
def __init__(self,name,age):
self.name = name
self.age = age
def animal_call(self):
print('this is '+self.name.title()+' call.')
# 子类
class Cat(Animal):
def __init__(self,name,age,color):
super().__init__(name,age)
self.color =color
def cat_color(self):
my_color = 'the cat is '+self.color
print(my_color)
return my_color
if __name__ == '__main__':
cat = Cat('tom',22,'blue')
cat.animal_call()
strs=cat.cat_color()
filename = r'../AllProject/V4.0EngInfoExtract/Document/EnPapers_single/test.json'
with open(filename,'w') as f_obj:
json.dump(strs,f_obj)
with open(filename,'r') as f_obj:
strs = json.load(f_obj)
print(strs)
完整代码下载源码请进【机器学习和自然语言QQ群:436303759】文件下载:
作者声明本文版权归作者所有,旨在技术交流使用。未经作者同意禁止转载,转载后需在文章页面明显位置给出原文连接,否则相关责任自行承担。