1. 程式人生 > ><潭州教育>-Python學習筆記@作業1

<潭州教育>-Python學習筆記@作業1

列表方法 amp 集合 比較 pytho name document 一個 pass

作業:

1.定義講過的每種數值類型,序列類型

數值類型:

整型:int
字符型:float
字符串:str
布爾型: bool

序列類型:

列表: 有序,可變,元素類型沒有固定要求
lst = [1,2,3]

元祖:有序,不能改變,元素類型沒有固定要求
tuple_list = (1,2,3,4,5)

字典: 無序,鍵值對組合,利於檢索
dict = {‘username‘:‘Stone‘,‘passwd‘:‘helloworld‘}

集合: 無序,元素類型沒有固定要求
set_list = {1,2,3,4}

2.python裏面怎麽註釋代碼?

方法1:代碼開頭加‘#‘
  # this is a example


  ##方法2: 三引號包裹註釋部分

方法2: 三引號註釋

‘‘‘
this is two example
this is three example
‘‘‘

拓展部分:註釋代碼應用,構造文檔字符串#文檔字符串,用來解釋功能函數的幫助文檔
#文檔的使用要求:必須在函數的首行,
#查看方式: 交互模式下用help查看函數,文本模式下用function.__doc__查看


def inputxy():
‘‘‘this is a documents file‘‘‘
print ‘hello Stone‘

inputxy()

print inputxy.__doc__

##3.簡述變量的命名規則


‘‘‘
Python變量主要由字母,數字,下劃線組成,跟C語言一樣,數字不能在變量的開頭,為了便於區分識別,定義變量最好簡單易懂,與實際吻合便於理解,變量
命名比較好的是遵循駝峰命名法。

‘‘‘

4.有一個列表 li= [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘],用第一節課的知識,將列表倒序,然後用多種方法# 取出第二個元素

# 不能用列表方法reverse以及函數reversed,方法越多越好。

li = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]
##方法1
li = li[::-1] # 倒序切片
print li

#方法2:
li = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]
li.reverse()
print li

#方法3:
li = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]
l = reversed(li)
new_list = []
for x in l:
# print x,
new_list.append(x)
print new_list

##print list(reversed(li))


#方法四:
li = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]
print sorted(li,reverse = True)

5.有個時間形式是(20180206),通過整除和取余,來得到對應的日,月,年。請用代碼完成。

#時間字符串處理

date = ‘20180206‘

# way 1 利用字符串切片操作
year = date[0:4]
month = date[4:6]
day = date[6:8]
print year,month,day


# way 2 ## 利用除&取余求值
Year = int(date)/10000
Month = (int(date)/100)%100
Day = int(date)%100
print Year
print Month
print Day


#way 3 利用時間數組求值
import time
time_struct = time.strptime(date,‘%Y%m%d‘)
years = time.strftime(‘%Y‘,time_struct)
months = time.strftime(‘%m‘,time_struct)
days = time.strftime(‘%d‘,time_struct)
print years,months,days,

6.有一個半徑為10的圓,求它的面積(math模塊有π)

import math

pi = math.pi
r = 10
s = pi*r*r
print ‘面積是:‘, ‘%.6s‘%s

<潭州教育>-Python學習筆記@作業1