Learn Python The Hard Way-Ecercise1~5

Exercise1:第一个程序


print "Hello World!"
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.'

第一个练习就是基本的print输出,没有C语言恼人的printf和括号分号,Python是如此的简洁。


输出结果:

$ python ex1.py

Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing.
I'd much rather you 'not'.
I "said" do not touch this.


Exercise2:注释和#号


#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 acomment to "disable" or comment out a piece of code:
#print "This won't run."

print"This will run."


Python用#来作为注释符号,#后面的内容会全部被注释掉,且只能作用于一行。


输出结果:

$ python ex2.py
I could have code like this.
This will run.


Exercise3:数字和数学计算


print "I will now count my chickens:"

print "Hens", 25 + 30 / 6 #双引号表示输出字符串,运算结果接在Hens后面输出
print "Roosters", 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 #比较运算的输出结果为True/False

print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7

print "Oh,that's why it's False."

print "How abou some more."

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2


print可以直接把式子的运算结果直接输出,比较运算则输出True/False。


输出结果:
$ python ex3.py
I will now count my chickens:
Hens 30
Roosters 97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5
What is 5 - 7? -2
Oh,that's why it's False.
How abou some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False



Exercise4:变量和命名


cars = 100 #变量名在左,右边赋值给左边,习惯和C语言大体相同
space_in_a_car = 4.0
drivers = 30
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."


赋值符号为‘=’,右边的值赋值给左边的变量,习惯和C语言大体相同。


输出结果:

$ python ex4.py
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.



Exercise5:更多的变量和输出


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 "He's got %s eyes and %s hair." %(my_eyes, my_hair) #当出现多个变量时,应把他们都放在一个括号里,形如%( , , )…
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)


这个练习主要包含格式化字符和格式化字符串的使用。


实际使用中有更多的格式化字符。例如 %r 就是是非常有用的一个,它的含义是“不管什么都打印出来”。


更多的格式化字符:


%% 百分号标记 
%c 字符及其ASCII码 
%s 字符串 
%d 有符号整数(十进制) 
%u 无符号整数(十进制) 
%o 无符号整数(八进制) 
%x 无符号整数(十六进制) 
%X 无符号整数(十六进制大写字符) 
%e 浮点数字(科学计数法) 
%E 浮点数字(科学计数法,用E代替e) 
%f 浮点数字(用小数点符号) 
%g 浮点数字(根据值的大小采用%e或%f) 
%G 浮点数字(类似于%g) 
%p 指针(用十六进制打印值的内存地址) 
%n 存储输出字符的数量放进参数列表的下一个变量中 


输出结果:

$ python ex5.py
Let's talk about Zed A.Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值