天天看點

python日常工作

分析nginx日志,得出使用者ip及使用者相關資訊、(通路次,流量量大,相應時間)

logip =  {'1.1.1.1':[200,20M,1S],}

’please‘  enter  your want ip  info:’

題: 采集使用者的輸入,輸入ip,并采集相應ip的通路次數,流量,響應時間。至少采集三組資料

指令一:

python日常工作

logip = {}

ip = input('please enter your ip: ')

count = input('please enter {} counts:' .format(ip))

size = input('{} size: '.format(ip))

time = input('{} time: '.format(ip))

logip[ip] = [count.strip(),size.strip(),time.strip()]

print(logip)

python日常工作

方法二:(自認為這種簡單)

python日常工作

logip = {}

ip = input('please enter your ip: ')

count = input('please enter your counts: ')

size = input('please enter your size: ')

time = input('time: ')

logip[ip] = [count,size,time]

python日常工作

練習: 猜數遊戲,程式随機生成一個1-100之間的整數,然後和使用者互動讓使用者猜生成的 數是多少。如果數猜大了,則輸出"too big",再次等待使用者輸入猜數。如果使用者猜小了,則 輸出" too small" ,再次進入使用者輸入 猜數。如果使用者猜對了,剛輸出"Surprise! you are right!"并退出程式。

python日常工作

import random

randnum = random.randint(1,100)

while True:

num = input('please enter your guests: ')

if not num.isdigit():

print(' {}error'.format(num))

continue

num = int(num)

if num > randnum:

print(' {}is the big'.format(num))

elif num < randnum:

print(' {}is the small'.format(num))

else:

print('good job')

将一個清單中的偶數分開:

python日常工作

number = ['25','36','56','78','62','36']

L = []

while number:

tmp =number.pop(0)

if tmp.isdigit():

tmp = int(tmp)

if tmp % 2 ==0:

L.append(tmp)

print(L)

是否有質數:

python日常工作

num = 5

a = 2

while a < 5:

if num % a ==0:

print(' {}is not a zhishu'.format(num))

break

a += 1

else:

print(' {}is a zhishu'.format(num))

将數字從大到小排序:

python日常工作

a = [23,25,1,26,2,6,7]

yuner = []

while len(a) > 1:

tmp = a.pop(0)

for i in range(len(a)):

if tmp > a[i]:

a[i],tmp = tmp,a[i]

yuner.append(tmp)

else:

yuner.append(a[0])

print(yuner)

 查找/etc/passwd 結尾以/bash結尾的使用者:

python日常工作

with open('/etc/passwd','r') as fp:

for line in fp:

line = line.strip()

if line.endswith('bash'):

name = line.split(':')[0]

print(name)

列印出菱形:

python日常工作

def lx(row=7):

ret = list(range(1,row+1,2))

tmp =ret[:-1][::-1]

ret.extend(tmp)

return ret

row = 7

ret = lx(row)

for line in ret:

a = '*' * line

print(a.center(row))

 列印心形圖案:

python日常工作

print("\n".join([''.join(['*'*((x-y)%3)

if((x*0.05)**2+(y*0.1)**2 -1)**3-(x*0.05)**2*(y*0.1)**3 <= 0

else ' '

for x in range(-30,30)])

for y in range(15,-15,-1)]))

要求,正數在前負數在後 2、整數從小到大 3、負數從小到大

python日常工作

a =[1,36,56,4,6,-1,0,-5]

yuner = sorted(a,key=lambda x:(x<0,abs(x)))

print(yuner)

求出整數的倍數:

python日常工作

斐波那契數列,fib = [1,1] 要求輸出一個互動界面,當使用者輸入數字時就顯示相應位的斐波那契數,如果使用者輸入 exit或quit則退出程式,如果用 戶輸入的是一個非自然數則提示使用者輸入錯誤,并告訴使用者如何使用。

python日常工作

 清單去重

python日常工作

5、執行程式,使用者輸入一下年份,列印此年是平年還是閏年。 ①、某年能被4整除且不能被100整除的為閏年(2004是閏年,1901年不是閏年) ②、某年能被400整除的是閏年。(如2000年是閏年,1900年不是閏年) ③、對于數值很大的年份(不小于3200),這年如果能整除3200,并且能整除172800則是閏年。如172800年是閏年,3200、86400年不是閏年(因為雖然 能整除3200,但不能整除172800)

python日常工作

猜數遊戲,程式随機生成一個1-100之間的整數,然後和使用者互動讓使用者猜生成的數是多少。如果數猜大了,則輸出"too big",再次等待使用者輸入猜 數。如果使用者猜小了,則輸出"too small",再次進入使用者輸入 猜數。如果使用者猜對了,剛輸出"Surprise! you areright!"并退出程式。

python日常工作

在網頁上随便擷取一個圖檔:

python日常工作

##############################################################3

斐波那契數列,fib = [1,1] 要求輸出一個互動界面,當使用者輸入數字時就顯示相應位的斐 波那契數,如果使用者輸入 exit或quit則退出程式,如果使用者輸入的是一個非自然數則提示用 戶輸入錯誤,并告訴使用者如何使用。

def getfib(n):

fib = [1,1]

while len(fib) < n:

tmp = fib[-1] + fib[-2]

fib.append(tmp)

return fib[n-1]

if __name__ == '__main__':

while True:

num = input('please enter a number:')

num = num.strip().lower()

try:

num = int(num)

ret = getfib(num)

print(ret)

except:

if num == 'exit' or num == 'quit': break else: print('{} sorry is not shuzi'.format(num))

轉載于:https://www.cnblogs.com/ljl1366136/p/9260377.html