python-习题0~5

习题0:准备工作、环境搭建:

Mac OSX(环境)

Windows(环境):……(感觉好麻烦,弄了老半天没成功,还好慕课网上有相关视频:http://www.imooc.com/video/7724

安装python2.7,运行python(IDLE)编译

Linux

习题1:第一个程序:

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

加分习题:

1.  让你的脚本再多打印一行。

2.  让你的脚本只打印一行。

3.  在一行的起始位置放一个‘#’ (octothorpe)符号,它的作用是什么?

习题 2:注释和井号

注释的作用:用自然语言来告诉你某段代码的功能是什么,还可以临时禁用代码。

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

print "This will run."

加分题:

 

习题 3:数字和数学计算

边写边念下列符号:

-*- coding: cp936 -*-
+ plus 加号
-minus 减号
/slash 斜杠
*asterisk 星号
%percent 百分号
<less-than 小于号
> greater-than 大于号
<=less-than-equal 小于等于号
>=greater-than-equal 大于等于号

每个符号的作用:

print "I will now count my chicken:"

print "Hens",25+30/6
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

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 greather?",5>=-2
print "Is less or equal?",5<=-2

 

习题   4:变量(variable)和命名

cars = 100
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,"people 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."

运行结果:

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

练习5:更多的变量和打印

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)

运行结果:

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.

Warning:如果使用了非ASC||字符而且碰到了编码错误,记得在最顶端加一行  #  -- coding:utf-8 --

加分习题:

1.  修改所有的变量名,把他们前面的“my_”去掉。确认将每一个地方的都改掉,不只是你使用“=”赋值过的地方。

name = 'Zed A.Shaw'
age = 35 #not a lie
height = 74 #inches
weight = 180 #lbs
eyes = 'Blue'
teeth = 'White'
hair ='Brown'

print "Let's talk about %s."%name
print "He's %d inches tall."%height
print "He's %d pounds heavy."%weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair."%(eyes, hair)
print "His teeth are usually %s depending on the coffee."% teeth

# this line is tricky, try to get it exactly right
print "If I add %d,%d,and %d I get %d." %(
    age,height,weight,age+height+weight)

运行结果:

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.

结果一样。

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

eg:python格式化字符串%s和%r区别:

test = ""Hello,\nworld""
print 'this is test1:%s' %test
print 'this is test2:%r' %test

输出:

this is test1:Hello,
world
this is test2:'Hello,\nworld'

3.  所有python格式化字符:

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

        
%格式化符也可用于字典,可用%(name)引用字典中的元素进行格式化输出。

        
负号指时数字应该是左对齐的,“0”告诉Python用前导0填充数字,正号指时数字总是显示它的正负(+,-)符号,
即使数字是正数也不例外。

        
可指定最小的字段宽度,如:"%5d" % 2。也可用句点符指定附加的精度,如:"%.3d" % 3。

e.g.
# 例:数字格式化
nYear = 2018
nMonth = 8
nDay = 18
# 格式化日期 %02d数字转成两位整型缺位填0	
print '%04d-%02d-%02d'%(nYear,nMonth,nDay)	
>> 2018-08-18	# 输出结果

fValue = 8.123
print '%06.2f'%fValue	# 保留宽度为6的2位小数浮点型
>> 008.12	# 输出

print '%d'%10	# 输出十进制
>> 10
print '%o'%10	# 输出八进制
>> 12
print '%02x'%10	# 输出两位十六进制,字母小写空缺补零
>> 0a
print '%04X'%10	# 输出四位十六进制,字母大写空缺补零
>> 000A
print '%.2e'%1.2888	# 以科学计数法输出浮点型保留2位小数
>> 1.29e+00

 格式化操作符辅助指令
符号 作用
* 定义宽度或者小数点精度
- 用做左对齐
+ 在正数前面显示加号( + )
<sp> 在正数前面显示空格
# 在八进制数前面显示零('0'),在十六进制前面显示'0x'或者'0X'(取决于
          用的是'x'还是'X')
0 显示的数字前面填充‘0’而不是默认的空格
% '%%'输出一个单一的'%'
(var) 映射变量(字典参数)
m.n m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话)

以上内容来自:

http://www.tsnc.edu.cn/tsnc_wgrj/doc/python/basic.htm#id2809192

&

http://www.17jo.com/program/python/base/StringFormat.html

&

http://blog.csdn.net/sding/article/details/4712651

4.  试着用变量将英和寸和镑转换成厘米和千克。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/8824/blog/785888

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值