笨方法学python-练习5-更多变量和打印

练习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)

运行结果

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'表示输出对应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'表示有符号整数

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表示浮点数,%m.nf',m表示位宽,n表示小数点后保留多少位,-表示对齐

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 表示科学计数法

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 根据值大小来使用%e或者%f

执行结果

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

你可能感兴趣的:(笨方法学python-练习5-更多变量和打印)