`
陈三许
  • 浏览: 7550 次
  • 性别: Icon_minigender_1
文章分类
社区版块
存档分类
最新评论
文章列表
####测试代码### #模块unittest (python标准库中) 单元测试 #测试函数 import unittest from name_function import user_name """导入模块unittext和待测函数""" class nameTestCase(unittest.TestCase): #这个类必须继承unittest.TestCase类 """创建用于针对待测函数的单元测试(user_name())的类""" def test_fi ...
###从文件中读取数据 #读取整个文件 with open("1.txt") as file_object:   #在当前执行的文件所在的目录中查找指定的文件(1.txt) contents=file_object.read() print(contents.rstrip())   #方法rstrip()删除字符串末尾的空白 #文件路径 with open("ce/2.txt") as file_object:    #ce与程序文件在同一目录下,2.txt在ce文件夹中#还可以使用绝对路径(精确路径) #在文件路径中时用反斜杠(\),或者斜 ...
######类## ##创建和使用类 #创建Dog类(类中的函数成为方法) class Dog(): """一次模拟小狗的简单测试""" def __init__(self,name,age):   ##init前后两个下划线_ """初始化属性name和age""" self.name=name self.age=age def sit(self): """模拟小狗被命令时蹲下""" pri ...
##关键字def自定义函数 def greet_user(username):  #username 为形参 print('Hello,'+username.title()+'!') greet_user('Curry') #'curry'为实参 ##传递实参##----函数的调用:位置实参,关键字实参和默认值 #位置实参(可根据需要调用函数任意次) def describe ...
###用户输入和while循环#exercise #餐厅订位 customer=input('How many people have dinner,please?') customer=int(customer) if customer>=7: print("Sorry,there aren't empty table and wecome to you next time!") else: print("There are empty table!") #10的整数倍 number=input("Enter the number, ...
###用户输入和while循环 ##函数input()的工作原理:让程序暂停运行,等待用户输入如一些文本,之后 将其存储在一个变量中 message=input('Tell me some thing ,and I will repeat it back to you:') print(message) #用户按照提示输入自己的名字 name=input('please enter your name:') print('Hello,'+name+"!") #运算符 +=  #储存在**中的字符串的末尾添加字符串 prompt=('If you teel us who you ...
#用字典存储一个人的信息 personal_informations={     'first_name':'li',     'last_name':'hua',     'age':'20',     'city':'shanghai',     'constellation':'pisces',     } print(personal_informations) #存储多个人喜欢的数字,并单独打印键和值 favorite_number={     'curry':3,     'mr.chen':8,     'flower':3,     'lihua':5,     'abc':7, ...
###字典:一系列的键-值  a={'键':'值'} ,与键相关联的值:数字,字符串,列表,字典 #存储有关特定的信息 alien_0={'color':'green','points':10} print(alien_0['color']) print(alien_0['points']) #访问字典中的值 alien_0={'color':'green','points':10} a_pionts=alien_0['points'] print('YOU just earned '+str(a_pionts)+' points') #添加键—值对 alien_0={'color':'gre ...
#1-20的列表 a=list(range(1,21)) print(a) #1-10000的列表 squares=[] for value in range(1,10001): square=value squares.append(square) print(squares) #1-10000的列表,核实从1开始10000结束,求1-10000之和 a=list(range(1,10001)) print(a) print(min(a)) print(max(a)) print(sum(a)) #1-20的奇数列表 bs=[value for value in range(1,21,2) ...
####if语句### cars=['audi','bmw','subaru','toyota'] for car in cars: if car=='bmw':   #==检查是否相等  即相等时返回Ture,不相等时返回Flase print(car.upper()) else: print(car.title()) cars=['audi','bmw','subaru','toyota'] for car in cars: if car !="bmw":   #!=检查是否不相等  即不相等时返回Ture,相等时返回Flase print(car. ...
注:笔记2已补全 #首字母大写 .title() #.append()方法:可在列表或元组末尾添加元素                 主要在for循环中常用 #在提列表元素是的索引是从0开始,负数表示倒数,没有索引则默认是第一个元素或者最后一个元素 #列表排序详细在笔记1中 ###for循环##可参考笔记2 #for ** in **:下的代码缩进则表示要循环,没缩进则不循环 #“ :”一定要带上 #创建不同类型的数值列表 a=list(range(2,5,1))# (A,B,c)指的是从 A开始数(包括A)往后不断加C,达到终值为止(一般是B-1) 宁外三种不同方式创建不同类型的数值列表可 ...
操作列表 #列表循环  for循环  (for**in**)   1.注意使用for循环时 print前要缩进 cats=["alice","clear","dell",'moon'] for cat in cats:    print(cat)  #可在for循环中执行更多操作#   2.不使用for循环时,切记print能缩进 cats=["alice","clear","dell",'moon'] for cat in cats:    print(cat) print ...
python: >>>favorite_language='python' >>>favorite_language.strip() 注:rstrip消右空白    right()        lstrip消做空白    left() 大小写:首字母大写 .title()               全小写   .lower()               全大写   .up ...
Global site tag (gtag.js) - Google Analytics