笨办法学Python(learn python the hard way)--练习程序1-10
下面是当初看这本书时按照书中的代码做的练习,一行一行敲下来的,都已经试运行过,没有错误(基于python3),练习1-练习10
#ex1.py
1 #print("Hello world!")
2 print("Hello again")
3 print("I like typing this.")
4 print("This is fun.")
5 print('Yay!Printing.')
6 print("I'd much rather you 'not'.")
7 print('I "said" do not touch this.')
8 print('')
#ex2.py
1 # A comment, this is so you can read your program later.
# Anything after the # is ignored by python. print("I could have code like this.") # and the comment after is ignored) # You can also use a comment to "disable"or comment out a piece of code:
# print("This won't run.") print("This will run.")
#ex3.py
1 print("I will now count my chickens:") print("Hens",25+30/6)
print("Poosters",100-25*3%4) print("Now I will count the eggs:") print(3+2+1-5+4%2-1//4+6) print("Is it true that 3+2<5-7?") print(3+2<5-7) print("What is 3+2?",3+2)
print("What is 5-7?",5-7) print("Oh,that's why it's False.") print("How about some more.") print("Is it greater?",5 > -2)
print("Is it greater or equal?",5 >= -2)
print("Is it less or equal?",5 <= -2)
#ex4.py
1 #给变量cars赋值为100
cars = 100
#给变量space_in_a_car赋值为4.0
space_in_a_car = 4
#给变量drivers赋值为30
drivers = 30
#给变量passengers赋值为90
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers // cars_driven print("There are",cars,"cars available.")
print("There are only",drivers,"drivers available.")
print("There will be",cars_not_driven,"empty cars today.")
print("We can transport",carpool_capacity,"people today.")
print("We have",passengers,"to carpool today.")
print("We need to put about",average_passengers_per_car,"in each car.")
#ex5.py
1 my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 #lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown' print("Let's talk about %s."% my_name)
print("He's %d inches tall."% my_height)
print("He's %d pounds heavy."% my_weight)
print("Actually that's not too heavy.")
print("His teeth are usually %s depending on the coffee."% my_teeth) # this line is tricky,try to get it exactly right
print("If I add %d,%d,and %d I get %d."%(my_age,my_height,my_weight,my_age+my_height+my_weight))
#ex6.py
1 x = "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) hilarious = False
joke_evaluation = "Isn't that joke so funny?!%r" print(joke_evaluation % hilarious) w = "This is the left side of..."
e = "a string with a right side." print(w+e)
#ex7.py
1 print("Mary had a little lamb.")
print("Its fleece was white as %s."%'snow')
print("And everywhere that Mary went.")
print("."*10) # what'd that do? end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r" # watch that comma at the end. try removing it to see what happens print(end1+end2+end3+end4+end5+end6,end=" ")
print(end7+end8+end9+end10+end11+end12)
#ex8.py
1 formatter = "%r %r %r %r" print(formatter %(1,2,3,4))
print(formatter %("one","two","three","four"))
print(formatter %(True,False,False,True))
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."
))
#ex9.py
1 # Here's some new strange stuff,remember type it exactly. days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" print("Here are the days: ",days)
print("Here are the months: ",months) print("""
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want,or 5,or 6.
""")
#ex10.py
1 tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\a\\ cat." fat_cat = '''
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
''' print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)
笨办法学Python(learn python the hard way)--练习程序1-10的更多相关文章
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本
黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- 笨办法学Python - 习题1: A Good First Program
在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...
- 笨办法学python 13题:pycharm 运行
笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...
- 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘
笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln 怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...
- 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍
点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...
- 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程
<笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...
- 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码
笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...
- 《笨办法学Python 3》python入门书籍推荐|附下载方式
<笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
随机推荐
- 建立 Active Directory域 ----学习笔记
第五章 建立 Active Directory域 1.工作组和域的理解 a.工作组是一种平等身份环境,各个计算机之间各个为一个独立体,不方便管理和资源共享. b.域环境一般情况下满足两类需求, ...
- BitMap的原理和实现
相关概念 基础类型 在java中: byte -> 8 bits -->1字节 char -> 16 bit -->2字节 short -> 16 bits --> ...
- js去掉输入框的前后空格及一些常用正则表达式
去掉TextBox输入框两头的前后空格:onblur="this.value=this.value.replace(/^\s+|\s+$/g,'');" str为要去除空格的字符串 ...
- 深入浅出Oracle数据读取一致性和事务表
保证Oracle数据库读取一致性的关键是SCN.每一个数据块头都会记录一个事务提交的SCN.同时每一数据块头都包含一个事务表(ITL),事务必须获得一个ITL事务表才能进行数据修改.该事务表用来确定当 ...
- asp.net 获取表单中控件的值
原文:https://blog.csdn.net/happymagic/article/details/8480235 C# 后台获取前台 input 文本框值.(都是以控件的Name来获取) s ...
- Mysql日期和字符的相互转换
今天从网上查到了一些关于MySQL数据库的日期转换函数的转换的用法,在这里记录一下: mysql日期和字符相互转换 date_format(date,'%Y-%m-%d') ------------- ...
- 2018-8-27-C#-powshell-调用
title author date CreateTime categories C# powshell 调用 lindexi 2018-8-27 16:20:4 +0800 2018-06-18 20 ...
- CentOS6.5系统解决中文乱码问题
一. 问题详情 Windows的默认编码为GBK,Linux的默认编码为UTF-8.在Windows下编辑的中文,在Linux下显示为乱码.为了解决此问题,修改Linux的默认编码为GBK. ...
- 03机器学习实战之决策树scikit-learn实现
sklearn.tree.DecisionTreeClassifier 基于 scikit-learn 的决策树分类模型 DecisionTreeClassifier 进行的分类运算 http://s ...
- rabbit-c编译 vs2013
需要openssl的库