日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python编程入门与实践_Python编程入门到实践(二)

發(fā)布時間:2024/9/19 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python编程入门与实践_Python编程入门到实践(二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.用戶輸入和while循環(huán)

python2.7使用raw_input()來提示用戶輸入與python3中的input()一樣,也將解讀為字符串。

name=input("please enter your name:")print("Hello,"+name+"!")

please enter your name: Eric

Hello, Eric!

2.函數(shù)

(1)傳遞任意數(shù)量的實參

def make_pizza(*toppings):print(toppings)

make_pizza('pepperoni')

make_pizza('mushrooms','green peppers','extra cheese')

('pepperoni',)

('mushrooms', 'green peppers', 'extra cheese')

形參名*toppings中的星號讓Python創(chuàng)建一個名為toppings的空元組,并將所有收到的值封裝到這個元組中。

(2)使用任意數(shù)量的關(guān)鍵字實參

def build_profile(first,last,**user_info):

profile={}

profile['first_name']=first

profile['last_name']=lastfor key,value inuser_info.items():

profile[key]=valuereturnprofile

user_profile=build_profile('albert','einstein',location='princeton',field='physics')print(user_profile)

{'first_name': 'albert', 'location': 'princeton', 'field': 'physics', 'last_name': 'einstein'}

形參**user_info中的兩個星號讓Python創(chuàng)建了一個名為user_info的空字典,接受任意數(shù)量的關(guān)鍵字實參。

(3)繼承

classCar():def __init__(self,make,model,year):

self.make=make

self.model=model

self.year=year

self.odometer_reading=0defget_descriptive_name(self):

long_name=str(self.year)+' '+self.make+' '+self.modelreturnlong_name.title()defread_odometer(self):print("This car has"+str(self.odometer_reading)+"miles on it.")defupdate_odometer(self,mileage):if mileage >=self.odometer_reading:

self.odometer_reading=mileageelse:print("You can't roll back an odometer!")defincrement_odometer(self,miles):

self.ofometer_reading+=milesclassBattery():def __init__(self,battery_size=70):

self.battery_size=battery_sizedefdescribe_battery(self):print("This car has a"+str(self.battery_size)+"-kWh battery.")classElectricCar(Car):def __init__(self,make,model,year):'''初始化父類的屬性,再初始化電動汽車特有的屬性,這里創(chuàng)建一個新的Battery實例,并將該實例存儲在屬性self.battery中'''super().__init__(make,model,year)

self.battery=Battery()

my_tesla=ElectricCar('tesla','model s',2016)print(my_tesla.get_descriptive_name())

my_tesla.battery.describe_battery()

2016 Tesla Model S

This car has a 70-kWh battery.

(4)類編碼風(fēng)格

類名應(yīng)采用駝峰命名法,即將類名中的每個單詞的首字母都大寫,而不使用下劃線。實例名和模塊名都采用小寫格式,并在單詞之間加上下劃線。

對于每個類,都應(yīng)緊跟在類定義后面包含一個文檔字符串,這種文檔字符串簡要地描述類的功能,并遵循編寫函數(shù)的文檔字符串時采用的格式約定。

每個模塊也都應(yīng)包含一個文檔字符串,對其中的類可用于做什么進行描述。

在模塊中可用兩個空行來分割類。

需要同時導(dǎo)入標準庫的模塊和自己編寫的模塊時,先編寫導(dǎo)入標準庫模塊的import語句,再添加一個空行,然后編寫導(dǎo)入你自己編寫的模塊的import語句。

3.文件讀寫

(1)從文件讀取

filename='/home/shawnee/Geany_work/pcc-master/chapter_10/pi_million_digits.txt'with open(filename) as file_object:

lines=file_object.readlines()

pi_string=''

for line inlines:

pi_string+=line.strip()print(pi_string[:52]+'...')print(len(pi_string))

3.14159265358979323846264338327950288419716939937510...

1000002

使用關(guān)鍵字with時,open()返回的文件對象只在with代碼塊內(nèi)可用。

如果要在with代碼塊外訪問文件的內(nèi)容,可在代碼塊內(nèi)將文件的各行存儲在一個列表中,并在with代碼塊外使用該列表,可以立即處理也可推遲處理。

(2)寫入空文件

filename='programming.txt'with open(filename,'w') as file_object:

file_object.write("I love programming.\n")

file_object.write("number input test:"+str(12345)+"\n")

'w'模式下:文件不存在,open()將自動創(chuàng)建它;文件存在,python將在返回文件對象前清空該文件。

(3)附加到文件

打開文件方式指定附加模式(‘a(chǎn)’).

filename='programming.txt'with open(filename,'a') as file_object:

file_object.write("I also love finding meaning in large datasets.\n")

file_object.write("I love creating apps that can run in a browser.\n")

總結(jié)

以上是生活随笔為你收集整理的python编程入门与实践_Python编程入门到实践(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。