![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
笨方法学Python
45岁搬砖人开始学Python
这个作者很懒,什么都没留下…
展开
-
习题20 函数和文件
print_a_line(current_line, current_file) # readline每次读取到换行之后,指针被readlin记录到下一行的开始。def print_a_line(line_count, f): # 定义函数 变量line_count, f 功能打印变量f中的第line_count行。current_file = open(input_file) # 打开input_file文件。原创 2024-03-05 15:15:14 · 343 阅读 · 0 评论 -
习题19 函数和变量
【代码】习题19 函数和变量。原创 2024-03-05 15:14:19 · 378 阅读 · 0 评论 -
习题18 命名,变量,代码和函数
def print_two(*args): # 黄色的print_two 是函数名,随便命名,但最好能体现出函数的功能。def print_two(*args): 这里的*号是告诉python把所有参数都接受尽力爱,然后放倒名叫args的列表中去。4.读10遍:运行函数、调用含税、使用函数 是同一个意思!3.利用上面的1和2,创建“迷你脚本” 或者 “小命令”原创 2024-03-05 15:13:27 · 349 阅读 · 0 评论 -
习题17 更多的文件操作
{exists(to_file)}") # exists(),判断文件存在的话,它将返回Ture,否则返回False。print(f"The input file is {len(indata)} bytes long") # len() 长度。print(f"Copying from {from_file} to {to_file}") # 打印。from_file = input("请输入您要拷贝的文件名: ")原创 2024-03-05 15:12:37 · 324 阅读 · 0 评论 -
习题16 读写文件
target.truncate() # 清空文件。filename = input("请输入您要操作的文件名: ") # 还是用输入替代。a追加模式(若文件不存在则创建,已存在的文件内容会被附加到尾部)x独占模式(若文件不存在则创建,已存在文件会抛出异常)w写(若文件不存在则创建,已存在文件会被清空)example.txt 文件名。r只读 若不带参数,默认只读。原创 2024-03-05 15:11:40 · 334 阅读 · 0 评论 -
习题15 读取文件
txt = open(filename) # txt是一个可以尽情浏览filename文件内容的对象。file_again = input("> ") # 两种方式,现在剩下一种了。txt_again = open(file_again) # 注意,先打开,后读取。print(f"Here's your file {filename}:") # 打印 很熟悉了。script = "习题15 读取文件.py"原创 2024-03-05 15:10:40 · 325 阅读 · 0 评论 -
习题14 提示和传递
多行字符串与{}格式化工具结合起来,中间不能用#注释,啥?user_name = input("请输入您的名字: ") # 因为用的Pytharm,这里再输入一下。prompt = '如果舒服错误就要罚跑步哦,3000米,试试看哈: 'script = "习题14 提示和传递"原创 2024-03-05 15:09:47 · 319 阅读 · 0 评论 -
习题13 参数、解包和变量
3、第三行 将argv解包,与其将所有参数放倒同一个变量下面,不如将其赋值给4个变量:script, first, second, third。4、解包的含义很简单:“把argv中的东西取出,解包,将所有的参数依次赋值给左边的这些变量”2、argv为参数变量,这个变量保存着你运行Python脚本时传递给Python脚本的参数。1、import是将Python的特性引入脚本的方法。input():脚本运行过程中输入。argv:执行命令的时候就要输入。原创 2024-03-05 15:08:42 · 368 阅读 · 0 评论 -
习题12 提示别人
") # 这里需要注意的是?后面的空格,可以在输入的时候显得不是那么局促。原创 2024-03-05 15:07:35 · 298 阅读 · 0 评论 -
习题11 提问
",) # 强迫症犯了,为什么有下划线,为什么抄一遍就没有。print(age+height+weight) # 所以,输入42、160、80运行结果为4216080,是字符串相加。print(float(age)+float(height)+float(weight)) # 这个就没有问题了。4、输入类型必为str,数字需要转换类型int or float。2、函数好用,赋值第一,必须赋值。3、input的提示信息也是输出。1、有问有答,终端区输入。原创 2024-03-05 15:06:08 · 306 阅读 · 0 评论 -
习题10 那是什么
print('I am 6\'2" tall.') # 这两个结果一样一样的,用\告诉Python,里面的单双引号不是真正的单双引号。# on a line. 注意后面的句号。原创 2024-03-01 11:20:25 · 311 阅读 · 0 评论 -
习题9 打印,打印,打印
months = "\nJan\nFeb\nMar\nApr,\nMay\n Jun\nJul\nAug" # 只有放在开始才有用 因为months是一个字符串,所以,也是,当然空格也是。months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" # \n换行是放在开始还是放在结尾?原创 2024-03-01 10:53:06 · 313 阅读 · 0 评论 -
习题8 打印,打印
print(formatter.format(formatter, formatter, formatter, formatter)) # 现在的结果为 问题 问题 {} {} {} 问题 {} {} {} 问题 {} {} {} 哈哈 研究一下?print(formatter.format(formatter, formatter, formatter, formatter)) # 哈哈 看来是把formatter装到了formatter的盒子里,原来如此 看来你已经掌握这个小问题了。原创 2024-03-01 10:25:15 · 312 阅读 · 0 评论 -
习题7 更多打印
print("It's fleece was white as {}.".format("snow")) # It's fleece was white as snow. # snow 是一个字符串。print(end7 + end8 + end9 + end10 + end11 + end12) # Burrer 注意错误end='' 输出不换行。原创 2024-03-01 09:30:33 · 317 阅读 · 0 评论 -
习题6 字符串和文本
print(f"I also said: '{y}'") # I slso said: those who know binary and those who don't. 第三处。x = f"There are {types_of_people} types of people." # 将字符串赋值给X了。主要格式:print('{ } { }'.format('字符串1', '字符串2'))# A.format{B} 将B赋值给A中的{}位置。原创 2024-03-01 09:29:00 · 335 阅读 · 0 评论 -
习题5 更多的变量和打印
去掉所有的my_原创 2024-03-01 09:25:51 · 274 阅读 · 0 评论 -
习题4 变量和命名
=是赋值 == 是判断。原创 2024-03-01 09:24:37 · 341 阅读 · 0 评论 -
习题3 数字和数学计算
print(3 + 2 < 5 - 7) # -4 注意错误,正确答案为 False,小于号优先级比+-号低,正确的优先级为1括号2指数3乘除4加减。print("hens", 25 + 30 / 6) # 30 注意 正确答案为30.0 除法的数都是float 结果不是int。print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6) # 8.75 注意错误,正确为6.75.原创 2024-03-01 09:21:33 · 306 阅读 · 0 评论 -
习题2 注释和#号
【代码】习题2 注释和#号。原创 2024-03-01 09:18:51 · 332 阅读 · 0 评论 -
习题1 第一个程序
【代码】习题1 第一个程序。原创 2024-03-01 09:17:13 · 308 阅读 · 0 评论