接触到的python~1

1、接触的python

1.1安装python以及IDE开发环境

点击上面链接,安装python和开发环境

1.2、例题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.3、例题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主要用于把字符串里的值进行运算后取出来,如:

eval('abcd')=abcd

eval('123')=123

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])         #从右向左,依次输出

原文地址:https://www.cnblogs.com/SkyOceanchen/p/11175506.html