
Python
Python
gqltt
这个作者很懒,什么都没留下…
展开
-
Python编程从入门到实践~JSON
import json#AttributeError: module ‘json’ has no attribute ‘dump’ #模块的名字被我命名成了json.py,名称冲突#使用json.dump()和json.load()numbers = [2, 3, 4, 5, 66, 12]filename = './data/number.json'with open(filename,'w') as file: json.dump(numbers,file)with ope.原创 2021-11-03 15:31:49 · 131 阅读 · 0 评论 -
Python编程从入门到实践~异常
#异常try: print(5/0)except ZeroDivisionError: print("You can't divide by zero!")#else 代码块try: answer = print(5/0.99)except ZeroDivisionError: print("You can't divide by zero!")else: print(answer)#处理FileNotFoundErrorfilename = 'alice.txt'tr.原创 2021-11-03 15:09:47 · 86 阅读 · 0 评论 -
Python编程从入门到实践~文件写入
#写入文件filename = './data/programming.log'with open(filename, 'w') as file_object: file_object.write('I love programming.\n') file_object.write('I love creating new games.\n')#附加到文件filename = './data/programming.log'with open(filename, 'a') as file.原创 2021-11-03 14:58:16 · 100 阅读 · 0 评论 -
Python编程从入门到实践~文件读取
#读取整个文件filename = './data/read.log'with open(filename, 'r', encoding='utf-8') as file_object: contents = file_object.read()print(contents)#逐行读取filename = './data/read.log'with open(filename, 'r', encoding='utf-8') as file_object: for line in fi.原创 2021-11-03 14:20:52 · 247 阅读 · 0 评论 -
Python编程从入门到实践~类
创建Dog 类方法__init__(),Python 自动调用,约定!!!#创建Dog 类class Dog: def __init__(self, name, age): self.name = name self.age = age def sit(self): print(f"{self.name} is now sitting.") def roll_over(self): print(f"{self.name} rolled over.")原创 2021-10-29 10:37:26 · 131 阅读 · 0 评论 -
Python编程从入门到实践~函数
定义函数#定义函数def greet_user(): print("Hello~")#调用函数greet_user()#向函数传递信息def print_user(username): print(f"Hello~{username}")print_user('Jesse')原创 2021-10-28 19:49:27 · 153 阅读 · 0 评论 -
Python编程从入门到实践~字典
使用字典#一个简单的字典alien_0 = {'color': 'green', 'points': 5}#访问字典中的值print(alien_0['color'])#添加键值对alien_0['x_position'] = 4alien_0['y_position'] = 33print(alien_0)#修改字典中的值alien_0['color'] = 'red'print(alien_0)#删除键值对del alien_0['points']prin原创 2021-10-28 19:23:57 · 123 阅读 · 0 评论 -
Python编程从入门到实践~if语句
#检查是否相等cars = ["bmw", "audi", "toyota", "subaru"]for car in cars: if car == 'bmw': print(car.upper()) else: print(car.title())#忽略大小写是否相等car = "Audi"if (car.lower() == "audi"): print(True)#检查是否不相等request_topping = 'mushrooms'if request_t.原创 2021-10-28 19:12:21 · 121 阅读 · 0 评论 -
Python编程从入门到实践~操作列表
列表是什么#列表是什么bicycles = ["trek", "cannodale", "redline","specialized"]print(bicycles)#访问列表元素print(bicycles[0])#使用列表中的各个值message = f"My first bicycle was a {bicycles[0].title()}"print(message)...原创 2021-10-28 11:13:50 · 134 阅读 · 0 评论