写一个程序,判断给定年份是否为闰年. 这样定义闰年的:能被4整除但不能被100整除,或者能被400整除都是闰年. while(1): year = input("请输入一个年份,让我判断一下是不是闰年: ") while not year.isdigit(): print("请输入一个整数年份,不要输入其他字符") year = int(year) if year/400 == int(year/400): print("这一年是闰年!!!")
这里用到了三个函数: #判断是否为数字:str.isdigit()#是否为字母:str.isalpha()#是否为空格:str.isspace() def tongji(str): alpha = 0 number = 0 space =0 qt = 0 for i in range(len(str)): #或者for i in str: if str[i].isalpha(): #接上一句改为:i.isalpha() alpha += 1 elif str[i].isdigit(): numb
python查找指定字符 #!/usr/bin/env python import sys import re f = open("log.txt", "rb") info = open("info.txt", "ab") for line in f.readlines(): if re.search(sys.argv[1], line): info.write(line) info.close() f.close() 当形参
最近在学习Python, 现在写一个Python程序和Java程序进行对一下比,以此展示各自不同的特点.这个程序的功能是计算([n, m) )之间的闰年. Python程序如下: def function(n, m): return [y for y in range(n, m) if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0] Java程序如下: public static List<Integer> function(int n,
texts=[[word for word in document.lower().split() if word not in stoplist] for document in documents] 这段代码等同于: texts=[] for document in documents: words=[] for word in document.lower().split(): if word not in stoplist(): words.append(word) texts.appe
1.a**b 表示a的b次方. 2.def something(a,b): 定义函数,注意 python的缩进 . 3.print (a)与print a 的区别,python3中不支持print a . 4. >>> class superList(list):... def __sub__(self,b):... a = self[:]... b = b[:]... while len(b) > 0:...