python初体验

闲来无事,学学python

import random
print("---begin---")
secret = random.randint(0,10)
print(secret)
print(10/8)#1.25
print(10//8)#1
print(10.0//8)#1.0
print(10/3)#3.3333333333333335  16位float
print(10**3)#1000 幂函数
print(not 10)#False 幂函数
#===运算符优先级:幂运算》正负号》算数操作符》比较操作符》逻辑运算符(not and or)===

x,y=4,5
small = x if x<y else y
print(small)

print(type(secret))#数据的type
print(isinstance(secret,int))#是否int类型

#for循环
flowers = 'abcdef'
for i in flowers:
    print(i)

member=['hello','world']
for each in member:
    print(each,len(each))
    
#range函数
r = range(5,10,2)    #range([start,]stop[,step]) start默认0 step默认1
for each in r:
    print(each)
l = list(r)
print(l)    #[5, 7, 9]


#list
empty = []
empty.append('这是第一个元素')
empty.append(1)
print(empty)
empty.extend(['张三丰','朱元璋'])
print(empty)
empty.insert(1,'无极')    #下标,元素
print(empty)
empty.remove(1)
print(empty)
del empty[1]
print(empty)
empty.pop()
print(empty)


#function
def addNum(num1,num2):
    print('num1:',num1)
    print('num2:',num2)
    print(num1+num2)
print("调用函数")
addNum(1,2)

addNum(num2=7,num1=1)


#lamada
list1 = [3,5,-4,-1,0,-2,-6]
print(sorted(list1, key=lambda x: abs(x)))
print(list1)

#递归
def factorial(n):
    if n==1:
        return 1
    else:
        return n*factorial(n-1)
num = int(input("计算阶乘,请输入:"))
result = factorial(num)
print("%d 的阶乘是%d"%(num,result))
        

temp = input("guess it,please:")
guess = int(temp)
flag = True
count = 1
while flag:
    if guess == 1:
        print("oh my god! u're the ascarid in my stomach.")
        break
    elif guess == 0:
        print("zero!")
        flag = False
    else:
        print("Wrong!they say nothing was forever")
        if count == 3:
            print("u have no chance")
            flag = False
        else:
            count+=1
            print("try again")
            temp = input("guess it again,please:")
            guess = int(temp)
print("---the end ---")

import urllib.request
import json
import pickle


#pickle_file = open('city_data.pkl','rb')
#pickle.load(pickle_file)
#password = input('请输入城市')
#name1 = city[password]
file1 = urllib.request.urlopen("http://www.weather.com.cn/data/sk/101110101.html")
weatherHtml = file1.read().decode('utf-8')
weatherJson = json.JSONDecoder().decode(weatherHtml)
wInfo = weatherJson['weatherinfo']
print('城市:',wInfo['city'])
print('城市ID:',wInfo['cityid'])
print('温度:',wInfo['temp'])
print('风向:',wInfo['WD'])
print('级别:',wInfo['WS'])
print('湿度:',wInfo['SD'])
print('气压:',wInfo['AP'])
print('njd:',wInfo['njd'])
print('WSE:',wInfo['WSE'])
print('sm:',wInfo['sm'])
print('isRadar:',wInfo['isRadar'])
print('Radar:',wInfo['Radar'])
print('时间:',wInfo['time'])




#exception
try:
    sum = 1+'1'
    f = open("thisisafile.txt")
    print(f.read())
    f.close()
except OSError as reason:
    print("Error,the reason is:"+str(reason))
except TypeError as reason:
    print("Error,the reason is:"+str(reason))
except FileNotFoundError as reason:
    print("Error,the reason is:"+str(reason))
finally:
    print('---the end---')



#max factor
def calMaxFactor(num):
    count = num//2
    while count>1:
        if num % count == 0:
            print("%d的最大公约数是%d"%(num,count))
            break
        else:
            count -= 1
    else:
        print("%d是一个素数"%(num))

num = int(input("最大公约数计算,请输入一个数:"))
calMaxFactor(num)



#easygui
import easygui
easygui.msgbox('hello')
    

猜你喜欢

转载自www.cnblogs.com/zsxiang/p/9577907.html