《笨方法学python》--习题5

习题5 更多的变量和打印

#!usr/bin/python
#--coding:utf-8--

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 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.

其他:

  1. 格式化字符串(format string):
    %d 输出十进制有符号整数
    %u 输出十进制无符号整数
    %o 输出八进制无符号整数
    %x 输出十六进制无符号整数
    %f 输出小数点形式的浮点数
    %e 输出科学计数法形式的浮点数
    %g 输出浮点数(根据数值大小决定输出形式)
    %s 输出字符串
    %r 不管什么都打印出来
    %% 百分号标记,输出一个百分号
    %c 输出字符及其ASCII码
    %p 用于指针,用十六进制打印数值的内存地址
    %n 存储输出字符的数量放进参数列表的下一个变量中
  2. 将字符串定义为变量时,字符串要用单引号引起来;
    变量名下划线后不能有空格;
    变量可进行数学运算;
    多个变量可以同时输出;

猜你喜欢

转载自blog.csdn.net/jingjingliang1995/article/details/81191294
今日推荐