1. 程式人生 > >“笨辦法學python”學習筆記-習題5

“笨辦法學python”學習筆記-習題5

  1. 部分Python格式化字元介紹
    %% 百分號標記 #就是輸出一個%
    %c 字元及其ASCII碼
    %s 字串
    %d 有符號整數(十進位制)
    %u 無符號整數(十進位制)
    %o 無符號整數(八進位制)
    %x 無符號整數(十六進位制)
    %X 無符號整數(十六進位制大寫字元)
    %e 浮點數字(科學計數法)
    %E 浮點數字(科學計數法,用E代替e)
    %f 浮點數字(用小數點符號)
    %g 浮點數字(根據值的大小採用%e或%f)
    %G 浮點數字(類似於%g)
    %p 指標(用十六進位制列印值的記憶體地址)
    %n 儲存輸出字元的數量放進引數列表的下一個變數中
  2. 使用變數將英寸和磅轉換成釐米和千克

    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’
    my_height_cm = my_height * 2.54
    my_weight_kg = my_weight * 0.4536

print “Let’s talk about %s.” % my_name
print “He’s %d cm tall.” % my_height_cm
print “He’s %d kg heavy.” % my_weight_kg
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)