詳解Python中的變量及其命名和打印
在程序中,變量就是一個(gè)名稱,讓我們更加方便記憶。
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."
提示:下劃線一般用在變量名中表示假想的空格。讓變量名的可讀性高一點(diǎn)。
運(yùn)行結(jié)果:
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#
更多的變量和打印
現(xiàn)在我們輸入更多的變量并打印他們,通常我們用""引住的叫字符串。
字符串是相當(dāng)方便的,在練習(xí)中我們將學(xué)習(xí)怎么創(chuàng)建包含變量的字符串。有專門(mén)的方法將變量插入到字符串中,相當(dāng)于告訴Python:“嘿,這是一個(gè)格式化字符串,把變量放進(jìn)來(lái)吧。”
輸入下面的程序:
# -- coding: utf-8 -- my_name = 'Zed A. Shaw' my_age = 35 # 沒(méi)撒謊哦 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
# 下面這行比較復(fù)雜,嘗試寫(xiě)對(duì)它。 print "If I add %d, %d, and %d I get %d." % ( my_age, my_height, my_weight, my_age + my_height + my_weight)
提示:如果有編碼問(wèn)題,記得輸入第一句。
運(yùn)行結(jié)果:
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#
相關(guān)文章
利用Python實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的示例代碼
最近學(xué)習(xí)了字符串,運(yùn)算符,條件語(yǔ)句,循環(huán)語(yǔ)句,我在想可以用我最近學(xué)的東西做什么? 看到運(yùn)算我就想到了可以做一個(gè)簡(jiǎn)易的計(jì)算器,感興趣的可以了解一下2022-11-11
flask + pymysql操作Mysql數(shù)據(jù)庫(kù)的實(shí)例
下面小編就為大家?guī)?lái)一篇flask + pymysql操作Mysql數(shù)據(jù)庫(kù)的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Python使用PyQt5/PySide2編寫(xiě)一個(gè)極簡(jiǎn)的音樂(lè)播放器功能
這篇文章主要介紹了Python中使用PyQt5/PySide2編寫(xiě)一個(gè)極簡(jiǎn)的音樂(lè)播放器功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
window7下的python2.7版本和python3.5版本的opencv-python安裝過(guò)程
這篇文章主要介紹了window7下的python2.7版本和python3.5版本的opencv-python安裝過(guò)程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
python編程簡(jiǎn)單幾行代碼實(shí)現(xiàn)視頻轉(zhuǎn)換Gif示例
這篇文章主要為大家介紹了簡(jiǎn)單使用幾行python代碼就可以實(shí)現(xiàn)將視頻轉(zhuǎn)換Gif的示例過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
python txt中的文件,逐行讀取并且每行賦值給變量問(wèn)題
這篇文章主要介紹了python txt中的文件,逐行讀取并且每行賦值給變量問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-02-02
pytorch中forwod函數(shù)在父類(lèi)中的調(diào)用方式解讀
這篇文章主要介紹了pytorch中forwod函數(shù)在父類(lèi)中的調(diào)用方式解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

