接觸到的python~1

一、接觸的python

1.1安裝python以及IDE開發環境

點擊上面連接,安裝python和開發環境python

1.二、例題1(溫度變換)

temp=input("請輸入溫度")
if temp[-1:]=='C':
    F=eval(temp[:-1])*1.8+32
    printf(str(F)+'F')
elif temp[-1:]=='F':
    C=(eval(temp[:-1])-32)/1.8
    printf(C)

1.三、例題2(分鐘和天數的相互轉換)

T = input("輸入時間")
if T[-3:] == 'min':
    d = eval(T[:-3]) / 60 / 24
    print(str(d) + 'd')
elif T[-1:] == 'd':
    min = eval(T[:-1]) * 60 * 24
    print(str(int(min)) + 'min')
elif T[-4:] == 'week':
    d = eval(T[:-4]) *7
    print(str(int(d)) + 'd')
else :
    print("輸入錯誤,請從新輸入")


1.4 講解eval

eval主要用於把字符串裏的值進行運算後取出來,如:ide

eval('abcd')=abcdspa

eval('123')=123code

1.5 講解切片(變量)[:]

temp = '456123789'
print(temp[:])
print(temp[-1])
print(temp[-3:-1])
print(temp[-3:])
print(temp[:-1])
print(temp[-7:-2])

結果

s='nick handsome'
print(s[:8:1]) # nick han  #從前日後,輸出前八個
print(s[:8:2]) # nc a      #從前日後,跳一個輸出
print(s[:8:-1]) # emos  # 從右向左,從後往前輸出,第八個字符以後的
print(s[-4::-1])         #從右向左,依次輸出

相關文章
相關標籤/搜索