python入門篇-1

這兩天學了下基本的python編程知識,做了一個總結。。

#__author__ = 'LENOVO'
s1=(2,1.3,'fuck',False) #tuple,元組
s2=[233,'1.5',False]    #list,表
print (type(s1),s1)
print (type(s2),s2)
#tuple和list的主要區別在於,一旦建立,tuple的各個元素不可再變更,而list的各個元素可以再變更。字符串是一種特殊元組
print (s1[0])
print (s2[2])
#範圍引用: 基本樣式[下限:上限:步長]  注意絕壁不會包含上限元素本身
print (s1[0:3:2])#下限0 上限3 步長2
print (s2[:4])#從開始到下標3,下標4不包括的
print (s2[2:0:-1])#從2到1
s3=[1,[2,3,4]]
print (type(s3),s3,s3[1][2])#下標從0開始
s4=input()
print (type(s4),s4)
i = 1
if i > 0:#if利用縮進來控制下面兩行語句,就是用縮進來標明成塊的代碼。
    print ('positive i')
    i = i + 1
elif i == 0:
    print ('i is 0')
    i = i * 10
else:
    print ('negative i')
    i = i - 1
print ('new i:',i)#判斷i並進行加減;
#for循環需要預先設定好循環的次數(n),然後執行隸屬於for的語句n次。
#基本構造是
#for 元素 in 序列:
#    statement
#這個循環就是每次從表中取出一個元素(回憶:表是一種序列),然後將這個元素賦值給a,之後執行隸屬於for的操作(print)。
for s in [12,'3.5','ss','your motherfucker',False]:
    print (type(s),s)
#利用range函數建表,range表示的是從0開始到小於他的那個數之前依次遞增的所有數,range(5)=[0,1,2,3,4]
idx = list(range(7))#貌似直接運行返回結果是一個range類型的數據(0,7),果然python3 語法和2還是有一定差別的
print (idx)#強制轉化爲list表類型的在輸出即可。
i=1
while i<10:
    print (i)
    i=i+1
for i in list(range(10)):
    if i==2:
        print ('i==2,跳過這個數吧')
        continue
    print (i**2)#除了2以外的數字乘方後輸出
#寫一個函數判斷某年是否爲閏年
def isleap(year):
    if((year%4==0 and year%100!=0) or year%400==0):
        return True
    else:
        return False
print (isleap(2000))

對應運行結果如下:注意,中間有一個input字符串的過程。。我輸入的是gx,。。。應該能看懂,。。。

C:\Python30\python.exe E:/python筆記/hello.py
<class 'tuple'> (2, 1.3, 'fuck', False)
<class 'list'> [233, '1.5', False]
2
False
(2, 'fuck')
[233, '1.5', False]
[False, '1.5']
<class 'list'> [1, [2, 3, 4]] 4
gx
<class 'str'> gx
positive i
new i: 2
<class 'int'> 12
<class 'str'> 3.5
<class 'str'> ss
<class 'str'> your motherfucker
<class 'bool'> False
[0, 1, 2, 3, 4, 5, 6]
1
2
3
4
5
6
7
8
9
0
1
i==2,跳過這個數吧
9
16
25
36
49
64
81
True
Process finished with exit code 0




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章