練習5-更多變量和打印
- 更多變量和打印練習程序
- 附加練習
更多變量和打印練習程序
# -*-coding: utf-8 -*-
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)
運行結(jié)果
PS F:\python大師\習題> 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.
附加練習
3.Search online for all of the Python format characters.
# -*- coding: utf-8 -*-
print "%c is String&ASCII","%c"%'a'
print "%c is String&ASCII","%c"%'&'
print "%c is String&ASCII","%c"% 65
print "%c is String&ASCII","%c"% 254
#'%c'表示輸出對應(yīng)ASCII碼
print '%s',"%s"%"abc"
print '%s',"%s"%"123"
print '%s:\t|{0:s}'.format('a')
print '%s:\t|{0}'.format('ass')
# '%s'表示字符串
print '%d','%d'%10
print '%d','%d'%-10
print '%d','%d'%-10.1
print '%d:\t|{0}'.format(123)
# '%d'表示有符號整數(shù)
print '%o','%o'%100
print '%o:\t|{0:o}'.format(13)
# '%o' 表示八進制
print '%x','%x'%1119
print '%02x:%02x:%02x:%02x:%02x:%02x'%(10,20,30,40,50,60)
print '%x:\t|{1:02x}:{0:012x}'.format(10,1)
#'%x'表示16進制
print '%X','%X'%1119
print '%02X:%02X:%02X:%02X:%02X:%02X'%(10,20,30,40,50,60)
print '%X:\t|{1:02X}:{0:012X}'.format(10,1)
#'%x'表示16進制大寫
print '%f','%f'%18.1
print "%f:\t|{0:f}".format(4)
print '%f','%-33.5fjj'%18.1
print "%f:\t|{0:5.2f}jj".format(4)
#'%f表示浮點數(shù),%m.nf',m表示位寬,n表示小數(shù)點后保留多少位,-表示對齊
print '%e','%e'%18.1
print "%e:\t|{0:e}".format(4)
print '%e','%-33.5ejj'%18.1
print "%e:\t|{0:25.2e}jj".format(4)
#'%e 表示科學計數(shù)法
print '%g','%g'%1000008.1
print "%g:\t|{0:g}".format(4)
print '%g','%-33.5gjj'%18.1
print "%g:\t|{0:25.2g}jj".format(4)
#'%g 根據(jù)值大小來使用%e或者%f
執(zhí)行結(jié)果
PS F:\python大師\習題> python .\ex5_extend.py
%c is String&ASCII a
%c is String&ASCII &
%c is String&ASCII A
%c is String&ASCII ?
%s abc
%s 123
%s: |a
%s: |ass
%d 10
%d -10
%d -10
%d: |123
%o 144
%o: |15
%x 45f
0a:14:1e:28:32:3c
%x: |01:00000000000a
%X 45F
0A:14:1E:28:32:3C
%X: |01:00000000000A
%f 18.100000
%f: |4.000000
%f 18.10000 jj
%f: | 4.00jj
%e 1.810000e+01
%e: |4.000000e+00
%e 1.81000e+01 jj
%e: | 4.00e+00jj
%g 1.00001e+06
%g: |4
%g 18.1 jj
%g: | 4jj