国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

詳解Python中的變量及其命名和打印

2020-01-04 17:38:24
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Python中的變量及其命名和打印,是Python入門學習中的基礎知識,需要的朋友可以參考下
 

在程序中,變量就是一個名稱,讓我們更加方便記憶。

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# 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临邑县| 北碚区| 乌什县| 玛纳斯县| 临湘市| 威信县| 罗甸县| 景洪市| 庆阳市| 化隆| 高唐县| 闸北区| 遂平县| 临泉县| 运城市| 五原县| 庆城县| 阿拉善左旗| 屏东市| 海南省| 昆山市| 永善县| 房山区| 夏邑县| 盐亭县| 乳源| 南安市| 宁陵县| 南昌市| 石景山区| 专栏| 禄丰县| 黎川县| 哈尔滨市| 锡林郭勒盟| 定州市| 旌德县| 汪清县| 嵊州市| 东明县| 莲花县|