python練習

sktj發表於2018-09-25

!/usr/bin/python

– coding: UTF-8 –

for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if( i != k ) and (i != j) and (j != k):
print i,j,k

!/usr/bin/python

coding=utf-8

year=int(raw_input(`year:
`))
month = int(raw_input(`month:
`))
day = int(raw_input(`day:
`))

!/usr/bin/python

– coding: UTF-8 –

year = int(raw_input(`year:
`))
month = int(raw_input(`month:
`))
day = int(raw_input(`day:
`))

months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 < month <= 12:
sum = months[month – 1]
else:
print `data error`
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
print `it is the %dth day.` % sum

!/usr/bin/python

– coding: UTF-8 –

l = []
for i in range(3):
x = int(raw_input(`integer:
`))
l.append(x)
l.sort()
print l

!/usr/bin/python

– coding: UTF-8 –

def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
return a

輸出了第10個斐波那契數列

print fib(10)

列表複製

!/usr/bin/python

– coding: UTF-8 –

a = [1, 2, 3]
b = a[:]
print b

!/usr/bin/python

– coding: UTF-8 –

for i in range(1, 10):
print
for j in range(1, i+1):
print “%d%d=%d” % (i, j, ij),

time.strftime(`%Y%m%d%H%M%S`,time.localtime(time.time()))

!/usr/bin/python

– coding: UTF-8 –

for n in range(100,1000):
i = n / 100
j = n / 10 % 10
k = n % 10
if n == i ** 3 + j ** 3 + k ** 3:
print n

!/usr/bin/python

– coding: UTF-8 –

import datetime

if name == `main`:

# 輸出今日日期,格式為 dd/mm/yyyy。更多選項可以檢視 strftime() 方法
print(datetime.date.today().strftime(`%d/%m/%Y`))

# 建立日期物件
miyazakiBirthDate = datetime.date(1941, 1, 5)

print(miyazakiBirthDate.strftime(`%d/%m/%Y`))

# 日期算術運算
miyazakiBirthNextDay = miyazakiBirthDate + datetime.timedelta(days=1)

print(miyazakiBirthNextDay.strftime(`%d/%m/%Y`))

# 日期替換
miyazakiFirstBirthday = miyazakiBirthDate.replace(year=miyazakiBirthDate.year + 1)

print(miyazakiFirstBirthday.strftime(`%d/%m/%Y`))


相關文章