1, the common denominator: python has a good package function obtains the greatest common divisor gcd (a, b), such as:
Import math # Import math library
print (math.gcd (a, b) ) # of Function Solution Maximum Convention number of
print (a * b / math.gcd ( a, b)) # solving the above function using the least common multiple
2, the calendar module: as:
# introducing a calendar module
Import Calendar
# input designated date
yy = int (input ( "Enter the year:"))
mm = int (INPUT ( "enter the month:"))
# generates and displays a corresponding the entire calendar month.
Print (Calendar.MONTH (YY, mm))
# number of days per month:
monthRange = calendar.monthrange (2016,9)
Print (monthRange) # output :( tuple is a first element of the search month corresponding to the first day of the week (0-6), the second element is the number of days of the month).
3, binary conversion:
# get user input decimal
dec = int (input ( "enter the number:") )
Print ( "decimal number:", dec)
Print ( "converted to binary is:", bin (dec))
Print ( "octal as:", the OCT (dec))
Print ( "converted to hexadecimal system is: ", hex (dec))
4, python string size conversion: as:
STR = "www.runoob.com"
Print (str.upper ()) # convert all characters to lowercase to uppercase letter
print (str.lower ()) # all character in uppercase letters are converted to lowercase
print (str.capitalize ()) # the first letter is converted to uppercase, the rest lowercase
print (str.title ()) # the first letter of each word into uppercase, lowercase rest
5, python string is determined: As:
STR = "runoob.com"
print (str.isalnum ()) # Analyzing all the characters are numbers or letters
print (str.isalpha ()) # Analyzing all the alphabetic characters are
print ( str.isdigit ()) # judge all the characters are digital
print (str.islower ()) # judge all characters are lowercase
print (str.isupper ()) # judge all characters are uppercase
print (str.istitle () ) # judge all words are capitalized, like the title
print (str.isspace ()) # judge all the characters are blank character, \ t, \ the n-, \ r
6, get the date you want: as returned yesterday date:
# introducing datetime module
Import datetime
DEF getYesterday ():
Today datetime.date.today = ()
OneDay = the datetime.timedelta (Days =. 1) #timedalte datetime is an object of the returned object is a two the time difference
Yesterday = Today OneDay-
return Yesterday
# output
print (getYesterday ())
7、defaultdict的作用:dictionary中,当key值不存在但被查找时,返回的不是keyerror,而是一个默认值,这个默认值得看是什么类型,如果是list就返回[],如果是str就返回空字符串,set就返回set(),int返回0等等。
8、python提供了一种简单的计算笛卡尔积的方法:itertools.product(A[],B[]).