笨方法学Python3(21-44)

接着前天的总结

习题21:函数可以返回某些东西

    定义函数的加减乘除,以及嵌套使用

习题22:回顾以前学的知识

习题23:字符串、字节串和字符编码

综合运用字符串、函数、文件读取等知识。详情如下:

from sys import argv
script, encoding, error = argv    # 命令行参数处理

def main(language_file, encoding,errors):
    line = language_file.readline()            # 就是用readline()处理文本
    if line:                                   # 防止函数永远循环下去
        print_line(line, encoding, errors)
        return main(language_file, encoding, errors)

def print_line(line, encoding, errors):     # 对languages.txt中的每一行进行编码
    next_lang = line.strip()     # 移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
    raw_bytes = next_lang.encode(encoding,errors = errors)
    cooked_string =  raw_bytes.decode(encoding, errors = errors)

    print(raw_bytes, "<===>",cooked_string)

languages = open("language.txt",encoding = "utf-8")

main(languages, encoding, error)

  

习题24: 更多的练习

部分代码如下:

start_point = start_point / 10

print("We can also do that this way:")
formula = secret_formula(start_point)
# this is an easy way to apply a list to a format string
print("we‘d have {} beans,{} jars, and {} crates.".format(*formula))

  

习题25:定义函数

习题26:回顾以前知识

习题27:

这一节 就是讲解逻辑关系  逻辑术语的
and  or  not  !=  ==  >=  <= True False
以及真值表详见P83

  

习题28:布尔表达式(跟C语言类似)

习题29:if语句

习题30:  else 和 if 嵌套

# else  if
people = 30
cars = 40
trucks = 15

if cars > people:
    print("we should take the cars.")
elif cars < people:
    print("We should not take the cars.")
else:
    print("We can‘t decide.")

if trucks > cars:
    print("That‘s too many trucks.")
elif trucks < cars:
    print("Maybe we could take the trucks.")
else:
    print("We still can‘t decide.")

if people > trucks:
    print("Alright,let‘s just take the trucks.")
else:
    print("Fine,let‘s stay home then.")

  

习题31:采用if-else语句编写问答式“小游戏”

练习使用嵌套判断语句

习题32:循环和列表

习题33:while循环

习题34:访问列表的元素

习题35:综合小练习,巩固前面所学知识

习题36:设计和调试

注:debug最佳方式就是 使用print在各个想要检查的关键点将变量打印出来,从而检查哪里是否有错!

习题37:复习各种符号

关键字 描述 示例
and   逻辑与  True and False == False
as with-as 语句的某一部分 with X as Y: pass
assert 断言(确保)某东西为真 assert False, "Error!"
break 立即停止循环 while True: break
class 定义类 class Person(object)
continue 停止当前循环的后续步骤,再做一次循环 while True: continue
def 定义函数 def X(): pass
del   从字典中删除 del X[Y]
elif else if条件 if: X; elif: Y; else: J
else else条件 if: X; elif: Y; else: J
except 如果发生异常,运行此处代码 except ValueError, e: print(e)
exec 将字符串作为Python脚本运行 exec ‘print("hello")‘
finally 不管是否发生异常,都运行此处代码 finally: pass
for 针对物件集合执行循环 for X in Y: pass
from 从模块中导入特定部分 from X import Y
global 声明全局变量 global X
if if 条件    if: X; elif: Y; /else: J
import  将模块导入当前文件以供使用 import os
in for循环的一步分,也可以是否在Y中的条件判断 for X in Y: pass   以及  1 in [1] == True
is 类似于==,判断是否一样 1 is 1 == True
lambda 船舰短匿名函数 s = lambda y: y ** y; s(3)
not 逻辑非 not True == False
or 逻辑或 True or False == True
pass 表示空代码块 def empty(): pass
print 打印字符串 print(‘this string‘)
raise 出错后引发异常 raise ValueError("No")
return 返回值并推出函数 def X(): return Y
try 尝试执行代码,出错后转到except try: pass
while while循环 while X: pass
with 将表达式作为一个变量,然后执行代码块 with X as Y: pass
yield 暂停函数返回到调用函数的代码中 def X(): yield Y; X().next()

数据类型  和  字符串转义序列 等详见P110--113

未完待续。。。

原文地址:https://www.cnblogs.com/guohaoblog/p/11296636.html

时间: 2024-08-28 05:44:52

笨方法学Python3(21-44)的相关文章

笨方法学python3

阅读<笨方法学python3>,归纳的知识点 习题1:安装环境+练习  print函数使用  主要区别双引号和单引号的区别 习题2:注释符号# 习题3:运算符优先级,跟C/C++, Java类似 以下运算符优先级:从下往下以此递增,同行为相同优先级 Lambda #运算优先级最低 逻辑运算符: or 逻辑运算符: and 逻辑运算符:not 成员测试: in, not in 同一性测试: is, is not 比较: <,<=,>,>=,!=,== 按位或: | 按位异

Day 2 笨方法学Python

手打第25个练习,出错的地方有: def 定义后indent 4个空格,第一行空了以后,直接换行是跟上面对其的,但是运行时是错误的,我的解决方法是,重新手动空格4个: 还发现一个问题就是,中文解释,以前老是出错 # -*- coding : utf-8 -*- 网上看到的加上这个就可以了,但是我的还是出错.今天偶然在削微寒的博客http://www.cnblogs.com/xueweihan/的GIthub上找到了答案 #coding:utf-8 换成这个语句就可以了.以后,尽量每句都加上注释,

笨方法学python(5)加分题

这篇对应的是习题17,更多文件操作 # -*- coding: utf-8 -*- #对文件更多操作复制A文件的内容到B文件 #from sys import argv from os.path import exists prompt = "> " from_file = raw_input("please input the filename where you want to copy from: >") #in_file = open(from_

笨方法学python(6)加分题--列表与字典的区别

he string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIG

笨方法学python(4)加分题

自己在看笨方法学python这本书,把自己觉得有学到东西的记下来,并不是每个习题都有记录 这次对应的是:习题 6: 字符串(string)和文本 这次只要是想说明一下,在print语句中,只要带有格式化字符的,会当作格式化来处理 脚本1: 结果1: 打出的结果没有%r,那是因为当作格式化处理了 脚本2: 结果2: 会报错,因为print joke_evaluation %hilarious 是格式化的标识 脚本3: 结果3:

[IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果你觉得英文版看着累,当当网有中文版,也有电子版可以选择. 我试着将其中的代码更新到Python 3.同时附上一些自己的初学体会,希望会对你有帮助. 中文版有人把书名翻译为<笨办法学python>,其实我觉得叫做<学Python,不走寻常路>更有意思些. 作者的意思你可以在序言中详细了解

python3 _笨方法学Python_日记_DAY5

Day5 习题  24:  更多练习 1 print("Let's practice everything.") 2 print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.') 3 4 poem = """ 5 \tThe lovely world 6 with logic so firmly planted 7 cannot discern \n

python3 _笨方法学Python_日记_DAY4

Day4 习题  19:  函数和变量 1 def cheese_and_crackers(cheese_count, boxes_of_crackers): 2 print("You have %d cheeses!" % cheese_count) 3 print("You have %d boxes of crackers!" % boxes_of_crackers) 4 print("Man that's enough for a party!&q

python3 _笨方法学Python_日记_DAY6

Day6 习题  36:  设计和调试 If 语句的规则1. 每一个"if 语句"必须包含一个 else.2. 如果这个 else 永远都不应该被执行到,因为它本身没有任何意义,那你必须在else 语句后面使用一个叫做 die 的函数,让它打印出错误信息并且死给你看,这和上一节的习题类似,这样你可以找到很多的错误.3. "if 语句"的嵌套不要超过 2 层,最好尽量保持只有 1 层. 这意味着如果你在 if 里边又有了一个 if,那你就需要把第二个 if 移到另一个