笨方法学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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 9,942评论 0 5
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,143评论 0 13
  • Python语言特性 1 Python的函数参数传递 看两个如下例子,分析运行结果: 代码一: a = 1 def...
    伊森H阅读 8,189评论 0 15
  • 在ex4中,已经知道了什么是变量以及如何定义及使用变量,在接下来的这些习题中,一直到ex10,都会不断完善你对于变...
    Python探索之路阅读 525评论 0 0
  • JDK的线程池有三个重要的参数,corePoolSize,maximumPoolSize和一个阻塞队列workQu...
    是我_7b3f阅读 3,931评论 0 0