若是你有足夠長時間作某事,必定會更擅長。python
知識點:git
ASCII主要用於顯示現代英語和其餘西歐語言,規定了128個字符的編碼,使用一個字節編碼,不支持中文; GBK編碼是對GB2312的擴展,徹底兼容GB2312。採用雙字節編碼方案,剔出xx7F碼位,共23940個碼位,共收錄漢字和圖形符號21886個; Unicode爲世界上全部字符都分配了一個惟一的數字編號,採用4個字節編碼,意味着一個英文字符原本只須要1個字節,而在Unicode編碼體系下須要4個字節,其他3個字節爲空,這就致使資源的浪費; UTF-8是一種針對Unicode的可變長度字符編碼,又稱萬國碼,用1到6個字節編碼UNICODE字符;
year = int(input('請輸入年份: ')) is_leap = (year % 4 == 0 and year % 100 != 0 or year % 400 == 0) print(is_leap)
百分制成績轉等級製成績
90分以上--> A,80分~89分--> B,70分~79分--> C,60分~69分 --> D,60分如下--> Eapp
score = float(input('請輸入成績: ')) if score >= 90: grade = 'A' elif score >= 80: grade = 'B' elif score >= 70: grade = 'C' elif score >= 60: grade = 'D' else: grade = 'E' print('對應的等級是:', grade)
sum = 0 for x in range(101): sum += x print(sum)
from math import sqrt num = int(input('請輸入一個正整數: ')) end = int(sqrt(num)) is_prime = True for x in range(2, end + 1): if num % x == 0: is_prime = False break if is_prime and num != 1: print('%d是素數' % num) else: print('%d不是素數' % num)
x = int(input('x = ')) y = int(input('y = ')) if x > y: x, y = y, x for factor in range(x, 0, -1): if x % factor == 0 and y % factor == 0: print('%d和%d的最大公約數是%d' % (x, y, factor)) print('%d和%d的最小公倍數是%d' % (x, y, x * y // factor)) break
def gcd(x, y): (x, y) = (y, x) if x > y else (x, y) for factor in range(x, 0, -1): if x % factor == 0 and y % factor == 0: return factor def lcm(x, y): return x * y // gcd(x, y)
def is_palindrome(num): temp = num total = 0 while temp > 0: total = total * 10 + temp % 10 temp //= 10 return total == num
def is_prime(num): for factor in range(2, num): if num % factor == 0: return False return True if num != 1 else False
if __name__ == '__main__': num = int(input('請輸入正整數: ')) if is_palindrome(num) and is_prime(num): print('%d是迴文素數' % num)
import os import time def main(): content = '北京歡迎你爲你開天闢地…………' while True: # 清理屏幕上的輸出 os.system('cls') # os.system('clear') print(content) # 休眠200毫秒 time.sleep(0.2) content = content[1:] + content[0] if __name__ == '__main__': main()
import random def generate_code(code_len=4): """ 生成指定長度的驗證碼 :param code_len: 驗證碼的長度(默認4個字符) :return: 由大小寫英文字母和數字構成的隨機驗證碼 """ all_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' last_pos = len(all_chars) - 1 code = '' for _ in range(code_len): index = random.randint(0, last_pos) code += all_chars[index] return code
def is_leap_year(year): """ 判斷指定的年份是否是閏年 :param year: 年份 :return: 閏年返回True平年返回False """ return year % 4 == 0 and year % 100 != 0 or year % 400 == 0 def which_day(year, month, date): """ 計算傳入的日期是這一年的第幾天 :param year: 年 :param month: 月 :param date: 日 :return: 第幾天 """ days_of_month = [ [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] ][is_leap_year(year)] total = 0 for index in range(month - 1): total += days_of_month[index] return total + date def main(): print(which_day(1980, 11, 28)) print(which_day(1981, 12, 31)) print(which_day(2018, 1, 1)) print(which_day(2016, 3, 1)) if __name__ == '__main__': main()
def main(): num = int(input('Number of rows: ')) yh = [[]] * num for row in range(len(yh)): yh[row] = [None] * (row + 1) for col in range(len(yh[row])): if col == 0 or col == row: yh[row][col] = 1 else: yh[row][col] = yh[row - 1][col] + yh[row - 1][col - 1] print(yh[row][col], end='\t') print() if __name__ == '__main__': main()
from random import randrange, randint, sample def display(balls): """ 輸出列表中的雙色球號碼 """ for index, ball in enumerate(balls): if index == len(balls) - 1: print('|', end=' ') print('%02d' % ball, end=' ') print() def random_select(): """ 隨機選擇一組號碼 """ red_balls = [x for x in range(1, 34)] selected_balls = [] selected_balls = sample(red_balls, 6) selected_balls.sort() selected_balls.append(randint(1, 16)) return selected_balls def main(): n = int(input('機選幾注: ')) for _ in range(n): display(random_select()) if __name__ == '__main__': main()
def func(s): dict = {'num': 0, 'alpha': 0, 'space': 0, 'other': 0} for i in s: if i.isdigit(): dict['num'] += 1 elif i.isalpha(): dict['alpha'] += 1 elif i.isspace(): dict['space'] += 1 else: dict['other'] += 1 return dict a = input('請輸入字符串:') print(func(a))
def func(s): if type(s) is str and s: for i in s: if i == ' ': return True elif type(s) is list or type(s) is tuple: for i in s: if not i: return True elif not s: return True a = input('請傳入對象(字符串、列表、元組):') print(func(a))