python入门到实践笔记
热爱学习的小鲁同学
记录学习笔记
展开
-
第11章节 测试代码
11.1测试代码from name_function import name_function_nameprint("Enter 'q' at any time to quit.")while True: first=input("\nPlease gice me as a first name:") if first =='q': break last=input("\nPlease gice me as a last name:")原创 2022-04-13 08:35:57 · 384 阅读 · 0 评论 -
第六章 字典
#6.1一个简单的字典#创建字典:{键:值}alien_0={'color':'green','points':5}print(alien_0['color'])print(alien_0['points'])green5##6.2使用字典#字典用放在花括号{}中的一系列键—值对表示#键—值对是两个相关联的值。指定键时,Python将返回与之相关联的值。键和值之间用冒号分隔,而键—值对之间用逗号分隔。alien_0={'color':'green','points':5}原创 2022-04-10 18:50:40 · 348 阅读 · 0 评论 -
第十章 文件和异常
10.1从文件中读取数据读取整个文件with open('pi_digits.txt') as file_object: contents=file_object.read()#read读取后,会返回一个空行 print(contents)with可以自动关闭文件,比colse()好用,建议使用withprint(contents.rstrip())文件路径windows系统在相对路径打开文件:with open(‘text_files\filename.txt’) as f原创 2022-04-10 19:47:10 · 558 阅读 · 0 评论 -
第九章 类
9.1创建和使用类9.1.1创建dog类class Dog():#calss定义类 def __init__(self,name,age):#形参self不能少,self为前缀的变量可供类中的函数(方法)使用 self.name=name self.age=age#age,name这些可以被访问的成为属性 def sit(self):#类中:方法=函数 print(self.name.title()+" is n原创 2022-04-10 19:46:38 · 79 阅读 · 0 评论 -
第八章 函数
8.1 定义函数def greet_user():#def 函数名(函数完成工作所需要的输入): """显示简单的问候语"""#“““文档字符串””” print("Hello")# 执行的任务greet_user()Hellodef:定义函数+函数名+括号(括号可以有内容)+:8.1.1 向函数传递信息def greet_user(username): print("Hello "+username.title()+".")greet_user('lu')原创 2022-04-10 19:46:04 · 385 阅读 · 0 评论 -
第七章 用户输入和while循环
#第七章用户输入和while循环#7.1函数input的工作原#input(x),先打印x之后用户输入一个值message = input("Tell me something, and I will repeat it back to you: ")print(message)Tell me something, and I will repeat it back to you: ddddname=input("please enter your name:")print("hello原创 2022-04-10 19:45:26 · 69 阅读 · 0 评论 -
第五章 if语句
#第五章if语句#5.1简单实例cars=['audi','bmw','subaru','toyota']for car in cars: if car=='bmw': print(car.upper()) else: print(car.title()) AudiBMWSubaruToyota#条件测试#“==”根据条件测试的值为true或者false#5.2.1检查是否相等car='bmw'car=='bmw'原创 2022-04-10 18:47:34 · 250 阅读 · 0 评论 -
第四章 操作列表
#第四章 操作列表#4.1遍历整个列表magicians=['alice','david','carolina']for magician in magicians:#从列表magicians中取出一个名字,并将其放在magician中 print(magician)#注意缩进,每个缩进的都是循环的一部分alicedavidcarolina#格式:# for 变量名 in 列表:# 进行相关操作#4.1.1深入的研究循环#选择描述单个列表元素的有意义的名称非常有帮原创 2022-04-10 18:46:51 · 73 阅读 · 0 评论 -
第三章 列表简介
#列表是什么——一系列特定顺序排列的元素组成bicycles=['trek','cannondale','redline','specialized']print(bicycles)#用方括号[]来表示列表,并用逗号来分隔其中的元素['trek', 'cannondale', 'redline', 'specialized']#访问列表元素#要访列表元素,可指出列表的名称,再指出元素的索引,并将索引放在括号内#索引:从正面数:0,1,2 倒数:-1,-2bicycles=['trek',原创 2022-04-10 18:46:16 · 57 阅读 · 0 评论 -
第二章 变量和简单数据类型
#第二章#2.1print("hello python world")#打印内容到屏幕上#单引号与双引号:如果字符传里面有单引号,要用双引号盖起来hello python world#打印变量message="hello ptyhon world"print(message)hello ptyhon worldmessage="hello ptyhon world"print(message)#重新定义message="hello ptyhon crash course wo原创 2022-04-10 18:41:15 · 199 阅读 · 0 评论