詳解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)建包含變量的字符串。有專門的方法將變量插入到字符串中,相當(dāng)于告訴Python:“嘿,這是一個(gè)格式化字符串,把變量放進(jìn)來吧?!?/p>
輸入下面的程序:
# -- 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
# 下面這行比較復(fù)雜,嘗試寫對(duì)它。 print "If I add %d, %d, and %d I get %d." % ( my_age, my_height, my_weight, my_age + my_height + my_weight)
提示:如果有編碼問題,記得輸入第一句。
運(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進(jìn)行TCP網(wǎng)絡(luò)編程的教程
這篇文章主要介紹了用Python進(jìn)行TCP網(wǎng)絡(luò)編程的教程,是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識(shí),代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
Python迅速掌握語音識(shí)別之知識(shí)儲(chǔ)備篇
語音識(shí)別是一門交叉學(xué)科。近二十年來,語音識(shí)別技術(shù)取得顯著進(jìn)步,開始從實(shí)驗(yàn)室走向市場(chǎng)。人們預(yù)計(jì),未來10年內(nèi),語音識(shí)別技術(shù)將進(jìn)入工業(yè)、家電、通信、汽車電子、醫(yī)療、家庭服務(wù)、消費(fèi)電子產(chǎn)品等各個(gè)領(lǐng)域2021-11-11
對(duì)Pytorch 中的contiguous理解說明
這篇文章主要介紹了對(duì)Pytorch 中的contiguous理解說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
opencv實(shí)現(xiàn)靜態(tài)手勢(shì)識(shí)別 opencv實(shí)現(xiàn)剪刀石頭布游戲
這篇文章主要為大家詳細(xì)介紹了opencv實(shí)現(xiàn)靜態(tài)手勢(shì)識(shí)別,opencv實(shí)現(xiàn)剪刀石頭布游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Python實(shí)現(xiàn)繪制M2貨幣供應(yīng)率曲線
這篇文章主要為大家詳細(xì)介紹了如何利用Python語言實(shí)現(xiàn)繪制M2貨幣供應(yīng)率曲線,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06
Python生成隨機(jī)驗(yàn)證碼代碼實(shí)例解析
這篇文章主要介紹了Python生成隨機(jī)驗(yàn)證碼代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Python開發(fā)之身份證驗(yàn)證庫id_validator驗(yàn)證身份證號(hào)合法性及根據(jù)身份證號(hào)返回住址年齡等信息
這篇文章主要介紹了Python開發(fā)之身份證驗(yàn)證庫id_validator驗(yàn)證身份證號(hào)合法性及Python解析身份證號(hào),根據(jù)身份證號(hào)返回住址年齡等信息,需要的朋友可以參考下2020-03-03
python數(shù)據(jù)分析之如何刪除value=0的行
這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)分析之如何刪除value=0的行的相關(guān)資料,文中通過實(shí)例代碼以及圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-12-12
Python中移除List重復(fù)項(xiàng)的五種方法
本文列些處幾種去除在Python 列表中(list)可能存在的重復(fù)項(xiàng),這在很多應(yīng)用程序中都會(huì)遇到的需求,本文介紹幾種方法,感興趣的可以了解一下2021-05-05
同時(shí)安裝Python2 & Python3 cmd下版本自由選擇的方法
下面小編就為大家分享一篇同時(shí)安裝Python2 & Python3 cmd下版本自由選擇的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12

