for循環(huán)、if 塔沃、乘方 、continue 阳谍、除法
for i in range(100,1000):
a = i//100 # 取百分位
b = i//10%10 # 取十分位
c = i%100%10 # 取個位數(shù)
if i == a**3 +b**3 + c**3:
print(i)
else:
continue
輸入輸出蛀柴、while循環(huán)
import sys
n = int(input("input number:\n"))
print("n = %d" % n)
for i in range(2,n+1):
while n != i:
if n % i == 0:
sys.stdout.write(str(i))
sys.stdout.write("*")
n = n//i
else:
break
print("%d" %n)
日期操作
import datetime
# 輸出今日日期,格式為 yyyy/mm/dd矫夯。更多選項可以查看 strftime() 方法
print(datetime.date.today().strftime("%Y-%m-%d"))
print(datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
# 創(chuàng)建日期對象
miyazakiBirthDate = datetime.date(2015,5,31)
print(miyazakiBirthDate.strftime("%d/%m/%Y"))
miyazakiBirthNextDay = miyazakiBirthDate + datetime.timedelta(days=2)
print(miyazakiBirthNextDay)
批量打印
from sys import stdout
for i in range(4):
stdout.write("*")
print()
函數(shù)定義鸽疾,遞歸
# 階層計算
def fact(j):
if j == 0:
sum = 1
else:
sum = j * fact(j-1)
return sum
for i in range(6):
print("%d!=%d"%(i,fact(i)))
字符串操作
from sys import stdout
s = input("Input a string:")
l = len(s)
stdout.write(s[l-1])
print(l)
數(shù)組foreach
def geArr(i):
h = [];
for j in range(i):
h.append(j)
return h;
l = geArr(10);
for each in l:
print(each)
if elif
m = 2;
if 1>m :
print(1)
elif 3>m:
print(2)