笨方法学Python3电子版中文,笨方法学Python进阶篇

大家好,小编为大家解答笨方法学Python读后感1500字的问题。很多人还不知道笨方法学Python3电子版中文,现在让我们一起来看看吧!

这篇文章主要介绍了learn python the hard way (笨办法学python),具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下python动态爱心代码

习题1.

在书本中看到的是这个样子的


rint('hello word !')#你好世界
print('hello again')#再次见到你
print('I like typing this.')#我想试试这个
print('this is fun.')#这个很有趣
print('Yay!printing')#打印
print(' I`d much rather you `not`.')#我控制不了
print('I "said" do  not touch this') #我说不要触摸这个

习题2.#注释,

在Windows的pycharm下应该看到图所示的这个样子

习题3.数字和运算


print('I will now count my chickens')#我要统计我的鸡肉
print("Hens", 25+30/6)#母鸡有30只
print("Roosters",100-25*3%4)#公鸡有100-3=97只,(75除以4=18....3)
print("Now I will count the eggs:")#鸡蛋共有多少个?
print(3+2+1-5+4%2-1/4+6)#6.75。过程是3+2+1-5+0-0.25+6,。4%2的余数是0
print("It is true that 2+3<5-7?")#5<-2可以是真的吗
print(3+2<5-7)#5<-2是假的
print("what is 3+2?",3+2)#3加2等于多少,5
print("what is 5-7?",5-7)#5-7等于多少,-2
print("Oh that`s why it is false")
print('How about some more,')
print("Is it greater or equal?",5-2)#这个数大于等于几吗? 等于3
print("Is is greater or equal?",5>=-2)#这个数字大于等于吗?是的
print("Is it less or equal?",5<=-2)#这个数小于等于吗? 不是

习题4.

求变量vriable 和命名


cars=100#汽车100辆
space_in_car=4 #一个车可以拉4个人
drivers=30#司机30人
passengers=90#乘客90人
cars_not_driven=cars-drivers# 没司机的车
cars_not_driven=drivers#司机
carpool_capacity=cars_not_driven*space_in_car#4个人30个司机
average_passengers_per_car=passengers/cars_not_driven#90个人平均要多少司机

print('there are',cars ,'cars available')
print("there are",cars,"cars available")#车变量
print("there are  only",drivers,"drivers available")#司机变量
print("there will be",cars_not_driven,"empty cars today")#今天空70个车
print("we can transport",carpool_capacity,"people")#今天可以拉120人
print("we have",passengers,"to carpool today")#今天有90个乘客
print("we need to put about",average_passengers_per_car,"in each car")#我们大概需要30辆车

输出结果:

习题5.更多的变量和打印


my_name='mrfri'
my_age = 23
my_height=178
my_weight=60
my_eyes='Brown'#我的眼睛是棕色
my_teeth='white'#我的牙齿是白色
my_hair='Black'#我的头发是黑色
print("Let's talk about %s." %my_name)#我的名字
print('He is %s cm height' % my_height)#我的身高
print("He is %s kgs heavy."%my_weight)#我的体重
print("He's got %s eyes and %s hair,His teeth are %s"%(my_eyes,my_hair,my_teeth))
print("if I add %s,%s and %s,then I get: %s"%(my_age,my_weight,my_height,my_age+my_height+my_weight))
print("If I add %d,%d and %d,then I get: %d."%(my_age,my_weight,my_height,my_age+my_height+my_weight))

格式化字符:

%% 百分号标记 #就是输出一个%

%c 字符及其ASCII码

%s 字符串

%d 有符号整数(十进制)

%u 无符号整数(十进制)

%o 无符号整数(八进制)

%x 无符号整数(十六进制)

%X 无符号整数(十六进制大写字符)

%e 浮点数字(科学计数法)

%E 浮点数字(科学计数法,用E代替e)

%f 浮点数字(用小数点符号)

%g 浮点数字(根据值的大小采用%e或%f)

%G 浮点数字(类似于%g)

%p 指针(用十六进制打印值的内存地址)

%n 存储输出字符的数量放进参数列表的下一个变量中

%r 照原样打印出来,例如字符串会打印出引号

百词斩里面的格式输出

百词斩里面的换行\n,和\t的tab四个空字符

字符串的拼接“+

求余数用“%”100%3等于1.求整数用“//”100 //3等于33

习题6字符串和文本


= "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
print(x)
print(y)

print("I said:%r," % x)  # %表示原样输出包括了上面的单引号
print("I also said:'%s' " % y)  # %s输出没有引号了,给%s加上“引号”
hiarious = False#
joke_evaluation = "Isn't that joke so funny?! %r"
print(joke_evaluation % hiarious)
w = "this is the left side of----"
e = "a string with a right side "
print(w + e)

习题7更多的打印


print("Mary had a little lamb,")
print("Its fleece  was white as %s" % 'snow')
print("And everywhere that Mary went,")
print("."*10)#输出10个。
end1 ="C"
end2 = "h"
end3 = "e"
end4 = "s"
end5 = "e"
print(end1+end2+end3+end4+end5)#多个变量用加号连接成一个字符窜

习题8继续打印


formatter = "%r %r %r %r"
print(formatter%(1,2,3,4))
print(formatter% ("one","two","three","four"))
print(formatter%(formatter,formatter,formatter,formatter))
print(formatter % (
    "I had this thing,",
    "That you could type up right.",
    "But it didn't sing.",
    "So I said goodnight."
))
# 怎么不一样捏,第三个在前一种方案出现了双引号
# 原来是"But it didn't sing."这一句中已经出现了单引号,所以在格式化的时候为了避免认错,首尾换成了双引号

python里使用单引号和双引号没有意思上的区别,当句中有单引号的时候会强制使用来双引号区

百词斩里的if条件判断语句

百词斩里的if else 的使用

9,继续打印


days = "Mon Tue Wed Thu Fri Sat Sun"#定义变量days
mouths = "Jan\n feb\n Mar\n "#定义变量mouths用“\n来换行
print("Here are the days:",days)#加“,”号导入变量days
print("Here are the mouths:",mouths)#加“,”导入变量mouths,print打印输出
print(""" there`s something #“”“   ”“”使用三 个引号来保持原样输出
going on here with the three double 
quotes
we`ll be able to type as much as we like 
Even4 lines if we want""")

双引号可以放置多行文字

\n 为转义字符,换行

习题10

习题11,提问

Python3的代码:python2升级为python3后在,2中存在input和raw_input两个类似功能的函数,在3认为这是冗余函数,于是3将raw_input作为垃圾扔掉了。因此我们在运行2的程序前需要将其中的raw_input全部替为input。


age = input("How old are you?")#定义变量age,input里输入提示内容用引号括起来
height = input("How tall are you")#定义变量height加入input提示
weight = input("How much do you weight")#定义变量weight加入input输入提示,
print("So, you are %r old,%r tall and %r heavy,"%(age,height,weight))#打印输出%r格
# 式化原样输出且加入变量age ,height,weight

输入和提问

str = input("请输入:");

print("你输入的内容是%r: ", str)

习题13参数,解包,变量


from sys import argv
#argv = input().split()
yi,er,san,si = argv
print('第一变量是:'yi)
print("第二个变量 是:"er)
print("第三个变量是:"san)
print("第四个变量是:"si)

在pycharm中有错误无法运行,可以在Windows终端允许,但是参数要减掉两个,本来是4个参数

在本题中将了解另一种将变量传递个变量的方法(脚本就是 *.py 的文件)。运行一个 python 脚本我们可以用命令行输入 python ex13.py 其中 python 是要运行的程序,而 ex13.py 就是参数了。

先来准备将被传递参数的脚本


from sys import argv#导入argv

,first, second, third = argv #定义四个变量,一定要在终端执行,
#文件名是第一个,接着第二个是1,第三个是2,第四个是3
print("Your  is called:", )
print("Your third variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)
print("\n")    # 夹

脚本解析:

第 1 行:

import 语句的作用是引用我们(python自带的、其他人写的)已经写好的程序、“功能”,使这些“功能”可以在当前脚本中使用。

sys 就是本次引入的“功能”,我们一般叫它们“模块”或“库”。它提供了一系列有关 Python 运行环境的变量和函数。

import 有两种用法:

引入全部模块: import sys

引入部分模块: from sys import argv

这次用的就是第二种方法从 sys 模块中引入了 argv 个具体的“功能”(获取当前正在执行的命令行参数)

第 3 行:

这一行将 argv 进行了 解包 。解包是个编程专用的名词,作用是把 argv 中包含的多个值依次赋值个左边的几个变量:, first, second, third

第 5-8 行:

我们把被赋值后的几个变量打印了出来。

这个脚本保存后需要在命令行中单独运行:(必须有 3 个参数)

python ex13.py first 2nd 3rd

1

1from sys import argv

23 , first, second, third = argv

45print"The is called:",

6print"Your first variable is:", first

7print"Your second variable is:", second

8print"Your third variable is:", third

我们尝试几个不同的参数:

原文链接:《笨办法学 python3》系列练习计划——13: 参数、解包、变量_哈哈餐馆的博客-CSDN博客

习题14,参数的传递


from sys import argv
argv = input().split()
#传入的参数分配给变量
yi,er,san,si = argv
#输出
print('第一个变量',yi)
print('第二个变量',er)
print('第三个变量',san)
print('第四个变量',si)

习题15


from sys import argv

 ,filename =argv #定义来变量

print ("We 're  going to erase %r ."%filename )#传递变量之前创建号的txt文件
print ("If you don't want that, hit CTRL-C(^C).")#输出打印
print ("If you do want that , hit RETURN.")#输出打印

input("?")#提示符

print ("Opening the file...")#打印输出
target =open (filename,'w')#对文件进行读写

print ("Truncating the file,Goodbye!")#打印输出
target .truncate()#清除之前的文件内容

print ("Now I'm going to ask you for three lines.")#打印输出

line1 =input ("line1:")#定义变量,提示语打印输出
line2 =input ("line2:")
line3=input ("line3:")

print ("I'm going to write these to the file.")#原文打印输出

target.write(line1)#写入第一行文件
target.write("\n")#换行
target.write(line2)#写入第二行文件
target.write("\n")#换行
target.write(line3)
target.write("\n")

print ("And finally ,we close it .")#原样输出提示,要关闭文件
target.close()#关闭文件

习题16,一个文件,txt 复制到另一个文件txt

方法1:


from sys import argv#导入argv
scrip,from_file,to_file = argv#给三个变量,在Windows终端输入from_file和to_file
print("copying form %s to %s"%(from_file,to_file))#字符串格式输出,1.txt和3.txt
raw_input=open(from_file)#把3.txt文件打开
indata= raw_input.read()#读取3.txt文件
print("The input file is %d bytes long"%len(indata))#打印输出3.txt数据的大小字节
print("Ready,hit RETURN to continue,CTRL-c to abort")#打印输出
input()#写入
output=open(to_file,'w')#1.txt文件写入
output.write(indata)#写入数据
print("Alright,all done,")
output.close()
raw_input.close()



方法2:


from sys import argv
argv = input().split()
from_file,to_file = argv
indata = open(to_file,'w').write(open(from_file).read())
a = open(to_file)
print(a.read())
a.close()

argv相当于列表,可以使用下标对参数进行传递

习题15,


#从sys中使用argv列表
from sys import argv
#脚本本身名字,传入脚本的参数
, filename =argv
#打开filename这个参数所表示的文档,并将这个file传给txt
txt = open(filename)
#打印字符串,并代入文件名
print ("Here's your file %r :"%filename)
#打印字符串,并读取文件
print (txt.read())
#打印字串符,
print ("Type the filenanme again:")
#定义file,并使用input输入
file_again = input(">")
#定义txt_again,并打开file传给txt
txt_again =open (file_again)
#打印字符串,并读取文件
print (txt_again.read())

习题14.


from sys import argv#导入argv

 ,user_name = argv#定义两变量
prompt ='~'#输入变量提示 符是~

print ("Hi %s ,I'm the %s  ." %(user_name,))#格式化变量打印
print ("I'd like to ask you a few questions.")#打印我有一个问题
print ("Do you like me %s ?"%user_name)#格式化输出,
likes = input(prompt)#变量

print ("Where do you live %s ?"%user_name)#格式输出
lives = input(prompt)#变量

print("What kind of computer do you have?")#输出
computer =input (prompt)#变量

print ("""
Alright , so you said %r about liking me.
You live in %r .Not sure where that is .
And you have a %r computer  .Nice.
"""%(likes,lives,computer))#三引号自动换行输出

  1. 确认你弄懂了三个引号 “”" 可以定义多行字符串,而 % 是字符串的格式化工具。
    yes

练习题读取一个文件txt的内容


from sys import argv
,filename = argv
txt = open(filename)
print("Here`s you file,%s"%filename)
print(txt.read())

习题18.

函数,我们使用到的命令是 def,也就是“定义(define)”的意思,函数的命名规则,类似变量命名

函数参数的传递,函数也可是小脚本,

这和脚本的 argv(参数的意思)非常相似,参数必须放在圆括号 ()中才能正常工作。

接着我们用冒号 :结束本行,然后开始下一行缩进。

冒号以下,使用 4 个空格缩进的行都是


def print_two(*args):#首行两冒号
    arg1,arg2, = args#缩进四个字符,一个tab
    print("arg1:%r,arg2:%r,"%(arg1,arg2))#缩进四个字符,一个tab。%r原样格式化输出,
def print_two_again(arg1,arg2):
    print("arg1:%r,arg2:%r"%(arg1,arg2))
def print_one(arg1):
    print("arg1:%r"%arg1)
def print_none():
    print("I got nothin.")
print_two("Zed,","shaw")#
print_two_again("Zed","shaw")#演示函数可以接受两个参数,直接用()里的名称来做参数不需要解包,
print_one("Fisrt!")#演示了函数接受一个参数
print_none()#演示了函数不接受任何参数

习题19


def cheese_and_crackers(cheese_count,boxes_of_crackers):
    print("You have %d cheeses!"%cheese_count)
    print("You have %d boxes of crackers! "%boxes_of_crackers)
    print("Man that`s enough for a part")
    print("Get a blanket.\n")
print("We can just give the fution numbers  directly:")#直接使用函数编码
cheese_and_crackers(20,30)
print("OR,we can use varables from our :")#在函数里使用脚本变量
amount_of_cheese=10
amount_of_crackers=50
cheese_and_crackers(amount_of_cheese,amount_of_crackers)
print("we can even do math inside too:")
cheese_and_crackers(10+20,5+6)#直接在函数里面使用数学运算
print("And we can combine the two ,variables and math:")
cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)
#把变量和数学运算相结合。

习题20


from sys import argv#从sys库导入argv
,input_file=argv#解包argv,把参数依次付给左边变量
def print_all(f):#定义函数print_all,定义形参f
    print(f.read())#以只读的方式读取f中的内容
def rewind(f):#定义函数rewind,且设置参数f
    f.seek(0)#移动文件读取指针至f中内容开头的位置
def print_a_line(line_count,f):#定义函数print_a_line.且设置形参t,f
    print(line_count,f.readline())#打印出文件的所有,读取222文件内容打印出来
current_file=open(input_file)#把222文件参数传递给 currtent_fle.读取222文件内容
print("First let`s print the whole file:\n")#打印当前内容换行
print_all(current_file)#,调用前文print_all函数,打印curre_file文件内容
print("Now let`srewind,kinds of like a tape")
rewind(current_file)#调用前文定义的函数rewind,将文件读取指针至开头位置
print("Let`s print three lines:")#打印字符串,打印三行
current_line=1#设置文本行号第一行
print_a_line(current_line,current_file)#打印第一行行号,和当前文件,
current_line=current_line+1#设置文本第二line+,行数增加第二行,,
print_a_line(current_line,current_file)#打印第二行行号和当前文件
current_line=current_line+1#设置文本第三line+1,行数增加读取第三行,
print_a_line(current_line,current_file)#打印第三行行号和当前文件

每次 print_a_line 运行时,你都传递了一个叫 current_line 的变量。在每次调用函数时,打印出 current_line 的至,跟踪一下它在print_a_line 中是怎样变成 line_count 的。

答 这里其实 line_count 要叫做 位置参数,之所以调用时的参数 current_line 成为了函数定义时的 line_count 就是因为它们在定义与被调用时所处的位置是一样的。

如果我们把函数定义时两个参数的位置对调,并保持调用的顺序不变

习题20

return 的功能


def add(a,b):#定义函数add,
    print("ASSING %d +%d"%(a,b))#打印输出ASSING 30+5
    return a+b #计算a+b
def subtract(a,b):#定义函数subtract,
    print("SUBTRACTING %d -%d"%(a,b))#打印输出SUBTRACTING 78-4
    return a-b#计算a减去b
def multiply(a,b):#定义函数multiply,
    print("MULTIPLYING %d *%d"%(a,b))
    return a*b#计算a乘b
def divide(a,b):#定义函数divide,
    print("DIVIDIG %d / %d"%(a,b))
    return a/b#计算a除b
print("Let`s do some math wiht just funtions!")#让我们来做一些数学函数
age = add(30,5)#调用函数add,返回值赋给age
height = subtract(78,4)调用函数subtract,返回值赋给height
weight = multiply(90,2)调用函数multiply,返回值赋给weight
iq = divide(100,2)调用函数divide,返回值赋给iq
print("Age;%d,height:%d,weight:%d,IQ:%d"%(age,height,weight,iq))#格式化打印输出
print("Here is a puzzle")#这是一个难题
what = add(age,subtract(height,multiply(weight,divide(iq,2))))
#函数的返回值用作了另外一个函数的参数,非常难懂得
print("That becomes:",what,"can you do it by hands")#看我们能做出来吗

  • 16
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
笨办法 Python (第三版) 欢迎阅读《笨办法 Python》第三版。本书中译本发布于 https://learn-python-the-hard-way-zh_cn-translation.readthedocs.org 英文原版地址为 http://learnpythonthehardway.org/book/ Contents: 前言:笨办法更简单 习题0:准备工作 习题1:第一个程序 习题2:注释和井号 习题3:数字和数计算 习题4:变量(variable)和命名 习题5:更多的变量和打印 习题6:字符串(string)和文本 习题7:更多打印 习题8:打印,打印 习题9:打印,打印,打印 习题10:那是什么? 习题11:提问 习题12:提示别人 习题13:参数、解包、变量 习题14:提示和传递 习题15:读取文件 习题16:读写文件 习题17:更多文件操作 习题18:命名、变量、代码、函数 习题19:函数和变量 习题20:函数和文件 习题21:函数可以返回东西 习题22:到现在你到了哪些东西? 习题23:读代码 习题24:更多练习 习题25:更多更多的练习 习题26:恭喜你,现在可以考试了! 习题27:记住逻辑关系 习题28:布尔表达式练习 习题29:如果(if) 习题30: Else和If 习题31:作出决定 习题32:循环和列表 习题33: While循环 习题34:访问列表的元素 习题35:分支和函数 习题36:设计和调试 习题37:复习各种符号 习题38:列表的操作 习题39:字典, 可爱的字典 习题40:模块、类、对象 习题41:物以类聚 习题42:对象、类、以及从属关系 习题43:来自Percal 25号行星的哥顿人(Gothons) 习题44:继承 (Inheritance) VS合成(Composition) 习题45:你来制作一个游戏 习题46:一个项目骨架 习题47:自动化测试 习题48:更复杂的用户输入 习题49:创建句子 习题50:你的第一个网站 习题51:从浏览器中获取输入 习题52:创建你的web游戏 下一步 老程序员的建议
本书是一本Python入门书籍,适合对计算机了解不多,没有过编程,但对编程感兴趣的读者习使用。这本书以习题的方式引导读者一步一步习编程,从简单的打印一直讲到完整项目的实现,让初者从基础的编程技术入手,最终体验到软件开发的基本过程。 本书结构非常简单,共包括52个习题,其中26个覆盖了输入/输出、变量和函数三个主题,另外26个覆盖了一些比较高级的话题,如条件判断、循环、类和对象、代码测试及项目的实现等。每一章的格式基本相同,以代码习题开始,按照说明编写代码,运行并检查结果,然后再做附加练习。 Zed Shaw完善了这个堪称世上最好的Python习系统。只要跟着习,你就会和迄今为止数十万Zed教过的初者一样获得成功。 在这本书中,你将通过完成52个精心设计的习题来Python。阅读这些习题,把习题的代码精确地写出来(禁止复制和粘贴!),修正你的错误,观察程序的运行。在这个过程中,你将了解软件是如何工作的,好的程序看起来是什么样子,怎样阅读、编写、思考代码,以及如何用专业程序员的技巧来找出并修正错误。最重要的是,你将到下面这些编写优秀的Python软件必需的初始技能。 这本书会让你的每一分钟投入都有回报。Python是世界上最强大、最受欢迎的编程语言之一,很快你就会成为一名Python程序员。 你还可以看Zed的视频!随书附赠的DVD中包含5个多小时激情挥洒的教内容:一部完整的Python视频教程!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值