Pythonの変数とその名前と印刷の詳細

2830 ワード

プログラムでは、変数は名前であり、記憶をより便利にします.

cars = 100 
space_in_a_car = 4.0 
drivers = 30 
passengers = 90 
cars_not_driven = cars - drivers 
cars_driven = drivers 
carpool_capacity = cars_driven * space_in_a_car 
average_passengers_per_car = passengers / cars_driven 

  

print "There are", cars, "cars available." 
print "There are only", drivers, "drivers available." 
print "There will be", cars_not_driven, "empty cars today." 
print "We can transport", carpool_capacity, "people today." 
print "We have", passengers, "to carpool today." 
print "We need to put about", average_passengers_per_car, "in each car." 

ヒント:下線は、一般的に変数名に仮想スペースを表すために使用されます.変数名の可読性を高めます.
実行結果:

root@he-desktop:~/mystuff# python ex4.py 

There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.
root@he-desktop:~/mystuff# 


より多くの変数と印刷では、より多くの変数を入力して印刷します.通常、文字列と呼ばれています.
文字列はかなり便利で、練習では変数を含む文字列を作成する方法を学びます.特殊な方法で変数を文字列に挿入するのは、Pythonに「おい、これはフォーマット文字列だから、変数を入れなさい」と伝えることに相当します.
次のプログラムを入力します.

# -- coding: utf-8 -- 
my_name = 'Zed A. Shaw' 
my_age = 35 #      
my_height = 74 #    
my_weight = 180 #   
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 

#         ,     。 
print "If I add %d, %d, and %d I get %d." % ( 
  my_age, my_height, my_weight, my_age + my_height + my_weight) 

ヒント:エンコードの問題がある場合は、最初の文を入力してください.
実行結果:

root@he-desktop:~/mystuff# 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.
root@he-desktop:~/mystuff#