笨方法學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)

運行結(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,715評論 0 5
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,917評論 0 13
  • Python語言特性 1 Python的函數(shù)參數(shù)傳遞 看兩個如下例子,分析運行結(jié)果: 代碼一: a = 1 def...
    伊森H閱讀 3,177評論 0 15
  • 在ex4中,已經(jīng)知道了什么是變量以及如何定義及使用變量,在接下來的這些習題中,一直到ex10,都會不斷完善你對于變...
    Python探索之路閱讀 81評論 0 0
  • JDK的線程池有三個重要的參數(shù),corePoolSize,maximumPoolSize和一個阻塞隊列workQu...
    是我_7b3f閱讀 733評論 0 0

友情鏈接更多精彩內(nèi)容