mob6454cc716fb02023-08-21 21:00:41
文章標(biāo)簽 python編程代碼大全 算法 數(shù)據(jù)結(jié)構(gòu) c++ 字符串 文章分類 Python 后端開發(fā)
Java、Python等主流編程語言如今火的不行,初學(xué)編程都有哪100個(gè)代碼呢?筆者結(jié)合實(shí)際開發(fā)經(jīng)驗(yàn)和同學(xué)們最迫切關(guān)注的技術(shù)熱點(diǎn),總結(jié)了100個(gè)常用的代碼實(shí)現(xiàn),具體如下:
1.輸出 "Hello, World!":
print("Hello, World!")
2.求兩個(gè)數(shù)的和:
a = 5 b = 7 c = a + b print(c)
3.計(jì)算圓的面積:
import math radius = 4 area = math.pi * radius ** 2 print(area)
4.判斷一個(gè)數(shù)是否為偶數(shù):
num = 8 if num % 2 == 0: print("This number is even.") else: print("This number is odd.")
5.計(jì)算一個(gè)列表的平均值:
lst = [1, 2, 3, 4, 5] avg = sum(lst) / len(lst) print(avg)
6.打印九九乘法表:
for i in range(1, 10): for j in range(1, i+1): print(f"{j}x{i}={i*j}", end='\t') print()
7.判斷一個(gè)字符串是否為回文字符串:
s = "racecar" if s == s[::-1]: print("This is a palindrome.") else: print("This is not a palindrome.")
8.輸出斐波那契數(shù)列的前20項(xiàng):
a, b = 0, 1 for i in range(20): print(a, end=' ') a, b = b, a+b
9.生成一個(gè)包含10個(gè)隨機(jī)數(shù)的列表:
import random lst = [] for i in range(10): lst.append(random.randint(1, 100)) print(lst)
10.將一個(gè)字符串中的大寫字母轉(zhuǎn)換為小寫字母:
s = "HelLo, WoRLd!" s_lower = s.lower() print(s_lower)
11.將一個(gè)字符串中的小寫字母轉(zhuǎn)換為大寫字母:
s = "HelLo, WoRLd!" s_upper = s.upper() print(s_upper)
12.將一個(gè)列表中的元素按照從小到大的順序排列:
lst = [5, 3, 8, 2, 7, 4, 1, 9, 6] lst.sort() print(lst)
13.判斷一個(gè)數(shù)是否為質(zhì)數(shù):
num = 17 if num > 1: for i in range(2, int(num/2)+1): if (num % i) == 0: print(num, "is not a prime number") break else: print(num, "is a prime number") else: print(num, "is not a prime number")
14.求一個(gè)列表中的最大值和最小值:
lst = [5, 3, 8, 2, 7, 4, 1, 9, 6] max_val = max(lst) min_val = min(lst) print("Max value:", max_val) print("Min value:", min_val)
15.將一個(gè)字符串翻轉(zhuǎn):
string = "Hello, World!" reversed_string = string[::-1] print(reversed_string)
16.判斷一個(gè)數(shù)是否為完全平方數(shù):
num = 16 if (num**0.5).is_integer(): print(num, "is a perfect square.") else: print(num, "is not a perfect square.")
17.計(jì)算一個(gè)字符串中包含的字母?jìng)€(gè)數(shù):
s = "Hello, World!" count = 0 for c in s: if c.isalpha(): count += 1 print(count)
18.計(jì)算一個(gè)數(shù)的階乘:
num = 5 factorial = 1 for i in range(1, num+1): factorial *= i print(factorial)
19.將一個(gè)列表中的元素去重:
lst = [1, 2, 3, 2, 4, 5, 4, 6, 7, 6] unique_lst = list(set(lst)) print(unique_lst)
20.將一個(gè)列表中的元素隨機(jī)排序:
import random lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] random.shuffle(lst) print(lst)
21.將一個(gè)列表中的元素逆序排列:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] lst.reverse() print(lst)
22.一個(gè)字符串中的單詞按照字典序排序:
s = "this is a test string" words = s.split() words.sort() sorted_s = ' '.join(words) print(sorted_s)
23.計(jì)算兩個(gè)數(shù)的最大公約數(shù):
a = 15 b = 25 while b != 0: t = b b = a % b a = t print("The greatest common divisor is:", a)
24.計(jì)算兩個(gè)數(shù)的最小公倍數(shù):
a = 15 b = 25 gcd = 0 for i in range(1, b+1): if (a * i) % b == 0: gcd = a * i break print("The least common multiple is:", gcd)
25.將一個(gè)字符串中的空格替換為下劃線:
s = "Hello, World!" s_underscore = s.replace(' ', '_') print(s_underscore)
26.判斷一個(gè)數(shù)是否為回文數(shù):
num = 12321 if str(num) == str(num)[::-1]: print(num, "is a palindrome.") else: print(num, "is not a palindrome.")
27.判斷一個(gè)字符串是否為全數(shù)字:
s = "12345" if s.isdigit(): print(s, "consists entirely of digits.") else: print(s, "does not consist entirely of digits.")
28.計(jì)算一個(gè)數(shù)的平方根:
import math num = 16 sqrt = math.sqrt(num) print(sqrt)
29.將一個(gè)列表中的元素按照長(zhǎng)度從小到大排序:
my_list = ["apple", "banana", "cherry", "date", "elderberry"] sorted_list = sorted(my_list, key=len) print(sorted_list)
30.將一個(gè)列表中的元素按照長(zhǎng)度從大到小排序:
lst = ["apple", "banana", "pear", "orange", "kiwi"] lst.sort(key=len, reverse=True) print(lst)
31.將一個(gè)字符串中的每個(gè)單詞首字母大寫:
s = "this is a test string" words = s.split() capitalized_words = [word.capitalize() for word in words] capitalized_s = ' '.join(capitalized_words) print(capitalized_s)
32.計(jì)算一個(gè)數(shù)的立方:
num = 5 cube = num ** 3 print(cube)
33.計(jì)算一個(gè)數(shù)的四次方:
num = 5 fourth_power = num ** 4 print(fourth_power)
34.將一個(gè)列表中的元素用逗號(hào)連接成一個(gè)字符串:
lst = ["apple", "banana", "pear", "orange", "kiwi"] s = ', '.join(lst) print(s)
35.將一個(gè)列表中的元素用空格連接成一個(gè)字符串:
lst = ["apple", "banana", "pear", "orange", "kiwi"] s = ' '.join(lst) print(s)
36.將一個(gè)列表中的元素用換行符連接成一個(gè)字符串:
lst = ["apple", "banana", "pear", "orange", "kiwi"] s = '\n'.join(lst) print(s)
37.判斷一個(gè)數(shù)是否為質(zhì)數(shù):
num = 17 is_prime = True if num > 1: for i in range(2, int(num**0.5)+1): if num % i == 0: is_prime = False break else: is_prime = False if is_prime: print(num, "is a prime number.") else: print(num, "is not a prime number.")
38.將一個(gè)字符串中的字母大小寫互換:
s = "Hello, World!" swap_s = s.swapcase() print(swap_s)
39.判斷一個(gè)字符串是否為回文字符串:
s = "racecar" if s == s[::-1]: print(s, "is a palindrome.") else: print(s, "is not a palindrome.")
40.計(jì)算兩個(gè)數(shù)的和、差、積和商:
a = 5 b = 3 sum = a + b difference = a - b product = a * b quotient = a / b print("The sum is:", sum) print("The difference is:", difference) print("The product is:", product) print("The quotient is:", quotient)
41.判斷一個(gè)字符串中是否包含另一個(gè)字符串:
s = "Hello, World!" sub_s = "World" if sub_s in s: print(s, "contains", sub_s) else: print(s, "does not contain", sub_s)
42.將一個(gè)字符串中的數(shù)字取出來并求和:
s = "There are 7 apples and 3 oranges." nums = [int(i) for i in s.split() if i.isdigit()] sum_nums = sum(nums) print(sum_nums)
43.將一個(gè)列表中的元素按照字符串長(zhǎng)度排序,并按照長(zhǎng)度相同的元素字典序排序:
lst = ["pear", "banana", "kiwi", "apple", "orange"] lst.sort(key=lambda x: (len(x), x)) print(lst)
44.計(jì)算一個(gè)數(shù)的平方根:
import math num = 9 sqrt_num = math.sqrt(num) print(sqrt_num)
45.將一個(gè)列表中的元素去重:
lst = [1, 2, 3, 2, 4, 3, 5, 6, 1] unique_lst = list(set(lst)) print(unique_lst)
46.將一個(gè)字符串中的每個(gè)單詞逆序輸出:
s = "This is a test string" words = s.split() reversed_words = [word[::-1] for word in words] reversed_s = ' '.join(reversed_words) print(reversed_s)
47.將一個(gè)字符串中的每個(gè)單詞的字母順序逆序輸出:
s = "This is a test string" words = s.split() reversed_words = [''.join(reversed(word)) for word in words] reversed_s = ' '.join(reversed_words) print(reversed_s)
48.將一個(gè)字符串中的每個(gè)單詞的首字母移到末尾:
s = "This is a test string" words = s.split() new_words = [word[1:] + word[0] + 'ay' for word in words] new_s = ' '.join(new_words) print(new_s)
49.將一個(gè)列表中的元素兩兩配對(duì):
lst = [1, 2, 3, 4, 5, 6] pairs = [(lst[i], lst[i+1]) for i in range(0, len(lst)-1, 2)] print(pairs)
50.將一個(gè)列表中的元素分組:
lst = [1, 2, 3, 4, 5, 6] groups = [[lst[i], lst[i+1]] for i in range(0, len(lst), 2)] print(groups)
51.將一個(gè)字符串中的每個(gè)單詞的首字母大寫,并將其余字母小寫:
s = "THIS IS A TEST STRING" words = s.split() new_words = [word.capitalize() for word in words] new_s = ' '.join(new_words) print(new_s)
52.將一個(gè)列表中的元素隨機(jī)排序:
import random lst = [1, 2, 3, 4, 5, 6] random.shuffle(lst) print(lst)
53.將一個(gè)字符串中的字母往后移動(dòng)指定的位數(shù):
def shift_letter(letter, shift): shifted_letter = chr((ord(letter) - 97 + shift) % 26 + 97) return shifted_letter s = "hello" shift = 3 shifted_s = ''.join([shift_letter(letter, shift) for letter in s]) print(shifted_s)
54.將一個(gè)字符串中的字母往前移動(dòng)指定的位數(shù):
def shift_letter(letter, shift): shifted_letter = chr((ord(letter) - 97 - shift) % 26 + 97) return shifted_letter s = "khoor" shift = 3 shifted_s = ''.join([shift_letter(letter, shift) for letter in s]) print(shifted_s)
55.求一個(gè)列表中的最大數(shù)和最小數(shù):
lst = [1, 2, 3, 4, 5, 6] max_num = max(lst) min_num = min(lst) print("Max:", max_num) print("Min:", min_num)
56.求一個(gè)列表中的元素之和:
lst = [1, 2, 3, 4, 5, 6] sum_lst = sum(lst) print("Sum:", sum_lst)
57.求一個(gè)列表中的元素平均值:
lst = [1, 2, 3, 4, 5, 6] avg_lst = sum(lst) / len(lst) print("Average:", avg_lst)
58.求一個(gè)列表中的中位數(shù):
def median(lst): sorted_lst = sorted(lst) lst_len = len(lst) mid_index = lst_len // 2 if lst_len % 2 == 0: return (sorted_lst[mid_index-1] + sorted_lst[mid_index]) / 2 else: return sorted_lst[mid_index] lst = [1, 2, 3, 4, 5, 6] median_lst = median(lst) print("Median:", median_lst)
59.求一個(gè)列表中的眾數(shù):
from collections import Counter lst = [1, 2, 3, 2, 4, 3, 5, 6, 1] cnt = Counter(lst) mode = cnt.most_common(1)[0][0] print("Mode:", mode)
60.求兩個(gè)數(shù)的最大公約數(shù):
def gcd(a, b): if a % b == 0: return b else: return gcd(b, a % b) a = 48 b = 36 gcd_ab = gcd(a, b) print("GCD:", gcd_ab)
61.求兩個(gè)數(shù)的最小公倍數(shù):
def lcm(a, b): return (a * b) // gcd(a, b) a = 48 b = 36 lcm_ab = lcm(a, b) print("LCM:", lcm_ab)
62.將一個(gè)字符串中的每個(gè)單詞首尾顛倒:
s = "This is a test string" words = s.split() reversed_words = [word[::-1] for word in words] reversed_s = ' '.join(reversed_words) print(reversed_s)
63.將一個(gè)字符串中的每個(gè)單詞逆序輸出,但保留單詞之間的順序:
s = "This is a test string" words = s.split() reversed_words = ' '.join([word[::-1] for word in words]) print(reversed_words)
64.將一個(gè)字符串中的大寫字母轉(zhuǎn)換為小寫字母:
s = "Hello World" lower_s = s.lower() print(lower_s)
65.將一個(gè)字符串中的小寫字母轉(zhuǎn)換為大寫字母:
s = "Hello World" upper_s = s.upper() print(upper_s)
66.將一個(gè)字符串中的大小寫字母互換:
s = "Hello World" swapcase_s = s.swapcase() print(swapcase_s)
67.判斷一個(gè)字符串是否為回文字符串:
def is_palindrome(s): s = s.lower() s = ''.join([c for c in s if c.isalnum()]) return s == s[::-1] s = "A man, a plan, a canal: Panama" print(is_palindrome(s))
68.將一個(gè)列表中的元素隨機(jī)排序:
import random lst = [1, 2, 3, 4, 5, 6] random.shuffle(lst) print(lst)
69.判斷一個(gè)列表是否為遞增列表:
def is_increasing(lst): return all(x < y for x, y in zip(lst, lst[1:])) lst = [1, 2, 3, 4, 5, 6] print(is_increasing(lst))
70.判斷一個(gè)列表是否為遞減列表:
def is_decreasing(lst): return all(x > y for x, y in zip(lst, lst[1:])) lst = [6, 5, 4, 3, 2, 1] print(is_decreasing(lst))
71.將一個(gè)列表中的元素去重:
lst = [1, 2, 3, 2, 4, 3, 5, 6, 1] unique_lst = list(set(lst)) print(unique_lst)
72.將一個(gè)列表中的元素按照出現(xiàn)次數(shù)從高到低排序:
from collections import Counter lst = [1, 2, 3, 2, 4, 3, 5, 6, 1] cnt = Counter(lst) sorted_lst = sorted(lst, key=lambda x: cnt[x], reverse=True) print(sorted_lst)
73.將一個(gè)字符串中的每個(gè)單詞按照出現(xiàn)次數(shù)從高到低排序:
from collections import Counter s = "This is a test string with some repeated words like test and some" words = s.split() cnt = Counter(words) sorted_words = sorted(words, key=lambda x: cnt[x], reverse=True) sorted_s = ' '.join(sorted_words) print(sorted_s)
74.計(jì)算一個(gè)數(shù)的平方根:
import math x = 16 sqrt_x = math.sqrt(x) print(sqrt_x)
75.計(jì)算一個(gè)數(shù)的立方根:
x = 8 cbrt_x = x**(1/3) print(cbrt_x)
76.將一個(gè)二進(jìn)制數(shù)轉(zhuǎn)換為十進(jìn)制數(shù):
binary_num = "1011" decimal_num = int(binary_num, 2) print(decimal_num)
77.將一個(gè)八進(jìn)制數(shù)轉(zhuǎn)換為十進(jìn)制數(shù):
octal_num = "17" decimal_num =
78.將一個(gè)十六進(jìn)制數(shù)轉(zhuǎn)換為十進(jìn)制數(shù):
hex_num = "1F" decimal_num = int(hex_num, 16) print(decimal_num)
79.將一個(gè)整數(shù)轉(zhuǎn)換為二進(jìn)制數(shù):
x = 11 binary_x = bin(x)[2:] print(binary_x)
80.將一個(gè)整數(shù)轉(zhuǎn)換為八進(jìn)制數(shù):
x = 31 hex_x = hex(x)[2:] print(hex_x)
81.將一個(gè)整數(shù)轉(zhuǎn)換為十六進(jìn)制數(shù):
x = 31 hex_x = hex(x)[2:] print(hex_x)
82.生成一個(gè)隨機(jī)整數(shù):
import random random_num = random.randint(1, 10) print(random_num)
83.生成一個(gè)隨機(jī)小數(shù):
import random random_float = random.random() print(random_float)
84.生成一個(gè)指定范圍的隨機(jī)小數(shù):
import random random_float = random.uniform(1.0, 10.0) print(random_float)
85.從一個(gè)列表中隨機(jī)選擇一個(gè)元素:
import random lst = ['apple', 'banana', 'orange'] random_item = random.choice(lst) print(random_item)
86.從一個(gè)列表中隨機(jī)選擇指定個(gè)數(shù)的元素:
import random lst = ['apple', 'banana', 'orange', 'pear', 'grape'] random_items = random.sample(lst, 3) print(random_items)
87.計(jì)算一個(gè)列表中的所有元素之和:
lst = [1, 2, 3, 4, 5, 6] sum_lst = sum(lst) print(sum_lst)
88.計(jì)算一個(gè)列表中的所有元素的平均值:
lst = [1, 2, 3, 4, 5, 6] avg_lst = sum(lst) / len(lst) print(avg_lst)
89.計(jì)算一個(gè)列表中的所有元素的中位數(shù):
import statistics lst = [1, 2, 3, 4, 5, 6] median_lst = statistics.median(lst) print(median_lst)
90.計(jì)算一個(gè)列表中的所有元素的中位數(shù):
from collections import Counter lst = [1, 2, 3, 2, 4, 3, 5, 6, 1] cnt = Counter(lst) mode_lst = [k for k, v in cnt.items() if v == max(cnt.values())] print(mode_lst)
91.計(jì)算一個(gè)列表中所有元素的標(biāo)準(zhǔn)差:
import statistics lst = [1, 2, 3, 4, 5, 6] stdev_lst = statistics.stdev(lst) print(stdev_lst)
92.計(jì)算一個(gè)列表中所有元素的方差:
import statistics lst = [1, 2, 3, 4, 5, 6] var_lst = statistics.variance(lst) print(var_lst)
93.對(duì)一個(gè)列表進(jìn)行排序:
lst = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] sorted_lst = sorted(lst) print(sorted_lst)
94.對(duì)一個(gè)列表進(jìn)行倒序排序:
lst = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] reverse_sorted_lst = sorted(lst, reverse=True) print(reverse_sorted_lst)
95.將兩個(gè)列表合并:
lst1 = [1, 2, 3] lst2 = [4, 5, 6] merged_lst = lst1 + lst2 print(merged_lst)
96.將兩個(gè)列表進(jìn)行zip操作:
lst1 = [1, 2, 3] lst2 = [4, 5, 6] zipped_lst = list(zip(lst1, lst2)) print(zipped_lst)
97.將一個(gè)列表中的元素去重:
lst = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] distinct_lst = list(set(lst)) print(distinct_lst)
98.判斷一個(gè)字符串是否為回文串:
s = "level" is_palindrome = s == s[::-1] print(is_palindrome)
99.將一個(gè)字符串中的單詞進(jìn)行反轉(zhuǎn):
s = "Hello World" reversed_words = ' '.join(word[::-1] for word in s.split()) print(reversed_words)
100.將一個(gè)字符串中的字符進(jìn)行計(jì)數(shù):
s = "hello world" char_count = {char: s.count(char) for char in set(s)} print(char_count)