ex46中,创建自己的python,  当你激活环境时 .\.venvs\lpthw\ Scripts\activate 会报一个错误 此时需要以管理员身份运行PowerShell,(当前的PS不用关,重新开一个PS,方法是开始菜单里搜索PS右键以管理员身份运行) 然后输入命令 set-executionpolicy remotesigned 选择Y.现在再回到刚才的PS窗口, 应该可以了…
11.1 测试函数 要学习测试,得有要测试的代码.下面是一个简单的函数,它接受名和姓并返回整洁的姓名: def get_formatted_name(first, last): """Generate a neatly formatted full name.""" full_name = first + ' ' + last return full_name.title() 为核实get_formatted_name() 像期望的那样工作,我们…
#!/usr/bin/env python# -*- coding: utf-8 -*- class student: def __init__(self, name_list): self.student_name_list = name_list def __getitem__(self, item): return self.student_name_list[item] stu = student(['tom', 'bob', 'jane', ])stu = stu[:2]l = len…
10.1 从文件中读取数据  10.1.1 读取整个文件 with open(~) as object: contents=object.read() with open('C:/Users/jou/Desktop/input.txt') as file_object: contents=file_object.read() print(contents.rstrip())   10.1.2 文件路径 #文件路径读取方式1 filepath='C:/Users/jou/Desktop/input…
9.1 创建和使用类 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想. OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 把计算机程序视为一组对象的集合,而每个对象都可以接收其他对象发过来的消息,并处理这些消息,计算机程序的执行就是一系列消息在各个对象之间传递. 编写类时,你定义一大类对象都有的通用行为. 基于类创建对象 时,每个对象都自动具备这种通用行为,然后可根据需要赋予每个对象独特的个性. 根据类来创建对象被称为实…
8.1 定义函数 def greet_user(): # def 来告诉Python你要定义一个函数.这是函数定义 """Hello World""" # 文档字符串 (docstring)Python使用它们来生成有关程序中函数的文档. print("Hello!") greet_user() # 函数调用   8.1.1 向函数传递信息 def greet_user(username): """…
7.1 函数input()的工作原理 函数input() 让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. 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: hello world hello world…
6.1 一个简单的字典 alien_0 = {'color': 'green', 'points': 5} print(alien_0['color']) print(alien_0['points']) 6.2 使用字典 字典 是一系列键—值对 .每个键 都与一个值相关联,你可以使用键来访问与之相关联的值. 与键相关联的值可以是数字.字符串.列表乃至字典.事实上,可将任何Python对象用作字典中的值. 在Python中,字典用放在花括号 {}  中的一系列键—值对表示 键和值之间用冒号分隔,…
5.1 一个简单示例 cars = ['audi', 'bmw', 'subaru', 'toyota'] for car in cars: if car == 'bmw': print(car.upper()) else: print(car.title()) Audi BMW Subaru Toyota 5.2 条件测试 每条if 语句的核心都是一个值为True 或False 的表达式,这种表达式被称为条件测试 .   5.2.1 检查是否相等 相等运算符 两个等号(== )  5.2.2…
4.1 遍历整个列表   4.1.1 深入地研究循环   4.1.2 在for循环中执行更多的操作   4.1.3 在for循环结束后执行一些操作  例 magicians = ['alice', 'david', 'carolina'] for magician in magicians: print(magician.title() + ", that was a great trick!") print("I can't wait to see your next tr…