作者:佚名 时间:2024-02-22 09:24:16 阅读:(11)
编程是一项充满无限可能的技能,掌握编程可以让你打开全新的世界。对于初学者来说,掌握一些常见的编程代码是迈向编程大门的第一步。本文将为您介绍100个初学编程者必备的代码示例。让我们一起开始探索编程的奇妙世界吧!
print("Hello, World!")
a = 5 b = 7 c = a + b print(c)
import math radius = 4 area = math.pi * radius ** 2 print(area)
num = 8 if num % 2 == 0: print("This number is even.") else: print("This number is odd.")
lst = [1, 2, 3, 4, 5] avg = sum(lst) / len(lst) print(avg)
for i in range(1, 10): for j in range(1, i+1): print(f"{j} x {i} = {i*j}\t", end='') print()
s = "racecar" if s == s[::-1]: print("This is a palindrome.") else: print("This is not a palindrome.")
a, b = 0, 1 for i in range(20): print(a, end=' ') a, b = b, a+b
import random lst = [] for i in range(10): lst.append(random.randint(1, 100)) print(lst)
s = "HelLo, WoRLd!" s_lower = s.lower() print(s_lower)
s = "HelLo, WoRLd!" s_upper = s.upper() print(s_upper)
lst = [5, 3, 8, 2, 7, 4, 1, 9, 6] lst.sort() print(lst)
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")
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)
string = "Hello, World!" reversed_string = string[::-1] print(reversed_string)
num = 16 if (num**0.5).is_integer(): print(num, "is a perfect square.") else: print(num, "is not a perfect square.")
s = "Hello, World!" count = 0 for c in s: if c.isalpha(): count += 1 print(count)
num = 5 factorial = 1 for i in range(1, num+1): factorial *= i print(factorial)
lst = [1, 2, 3, 2, 4, 5, 4, 6, 7, 6] unique_lst = list(set(lst)) print(unique_lst)
import random lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] random.shuffle(lst) print(lst)
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] lst.reverse() print(lst)
s = "this is a test string" words = s.split() words.sort() sorted_s = ' '.join(words) print(sorted_s)
a = 15 b = 25 while b != 0: t = b b = a % b a = t print("The greatest common divisor is:", a)
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)
s = "Hello, World!" s_underscore = s.replace(' ', '_') print(s_underscore)
num = 12321 if str(num) == str(num)[::-1]: print(num, "is a palindrome.") else: print(num, "is not a palindrome.")
s = "12345" if s.isdigit(): print(s, "consists entirely of digits.") else: print(s, "does not consist entirely of digits.")
import math num = 16 sqrt = math.sqrt(num) print(sqrt)
my_list = ["apple", "banana", "cherry", "date", "elderberry"] sorted_list = sorted(my_list, key=len) print(sorted_list)
lst = ["apple", "banana", "pear", "orange", "kiwi"] lst.sort(key=len, reverse=True) print(lst)
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)
num = 5 cube = num ** 3 print(cube)
num = 5 fourth_power = num ** 4 print(fourth_power)
lst = ["apple", "banana", "pear", "orange", "kiwi"] s = ', '.join(lst) print(s)
lst = ["apple", "banana", "pear", "orange", "kiwi"] s = ' '.join(lst) print(s)
lst = ["apple", "banana", "pear", "orange", "kiwi"] s = '\n'.join(lst) print(s)
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.")
s = "Hello, World!" swap_s = s.swapcase() print(swap_s)
s = "racecar" if s == s[::-1]: print(s, "is a palindrome.") else: print(s, "is not a palindrome.")
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)
s = "Hello, World!" sub_s = "World" if sub_s in s: print(s, "contains", sub_s) else: print(s, "does not contain", sub_s)
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)
lst = ["pear", "banana", "kiwi", "apple", "orange"] lst.sort(key=lambda x: (len(x), x)) print(lst)
import math num = 9 sqrt_num = math.sqrt(num) print(sqrt_num)
lst = [1, 2, 3, 2, 4, 3, 5, 6, 1] unique_lst = list(set(lst)) print(unique_lst)
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)
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)
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)
lst = [1, 2, 3, 4, 5, 6] pairs = [(lst[i], lst[i+1]) for i in range(0, len(lst)-1, 2)] print(pairs)
lst = [1, 2, 3, 4, 5, 6] groups = [[lst[i], lst[i+1]] for i in range(0, len(lst), 2)] print(groups)
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)
import random lst = [1, 2, 3, 4, 5, 6] random.shuffle(lst) print(lst)
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)
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)
lst = [1, 2, 3, 4, 5, 6] max_num = max(lst) min_num = min(lst) print("Max:", max_num) print("Min:", min_num)
lst = [1, 2, 3, 4, 5, 6] sum_lst = sum(lst) print("Sum:", sum_lst)
lst = [1, 2, 3, 4, 5, 6] avg_lst = sum(lst) / len(lst) print("Average:", avg_lst)
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)
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)
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)
def lcm(a, b): return (a * b) // gcd(a, b) a = 48 b = 36 lcm_ab = lcm(a, b) print("LCM:", lcm_ab)
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)
s = "This is a test string" words = s.split() reversed_words = ' '.join([word[::-1] for word in words]) print(reversed_words)
s = "Hello World" lower_s = s.lower() print(lower_s)
s = "Hello World" upper_s = s.upper() print(upper_s)
s = "Hello World" swapcase_s = s.swapcase() print(swapcase_s)
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))
import random lst = [1, 2, 3, 4, 5, 6] random.shuffle(lst) print(lst)
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))
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))
lst = [1, 2, 3, 2, 4, 3, 5, 6, 1] unique_lst = list(set(lst)) print(unique_lst)
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)
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)
import math x = 16 sqrt_x = math.sqrt(x) print(sqrt_x)
x = 8 cbrt_x = x**(1/3) print(cbrt_x)
binary_num = "1011" decimal_num = int(binary_num, 2) print(decimal_num)
octal_num = "17" decimal_num =
hex_num = "1F" decimal_num = int(hex_num, 16) print(decimal_num)
x = 11 binary_x = bin(x)[2:] print(binary_x)
x = 31 hex_x = hex(x)[2:] print(hex_x)
x = 31 hex_x = hex(x)[2:] print(hex_x)
import random random_num = random.randint(1, 10) print(random_num)
import random random_float = random.random() print(random_float)
import random random_float = random.uniform(1.0, 10.0) print(random_float)
import random lst = ['apple', 'banana', 'orange'] random_item = random.choice(lst) print(random_item)
import random lst = ['apple', 'banana', 'orange', 'pear', 'grape'] random_items = random.sample(lst, 3) print(random_items)
lst = [1, 2, 3, 4, 5, 6] sum_lst = sum(lst) print(sum_lst)
lst = [1, 2, 3, 4, 5, 6] avg_lst = sum(lst) / len(lst) print(avg_lst)
import statistics lst = [1, 2, 3, 4, 5, 6] median_lst = statistics.median(lst) print(median_lst)
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)
import statistics lst = [1, 2, 3, 4, 5, 6] stdev_lst = statistics.stdev(lst) print(stdev_lst)
import statistics lst = [1, 2, 3, 4, 5, 6] var_lst = statistics.variance(lst) print(var_lst)
lst = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] sorted_lst = sorted(lst) print(sorted_lst)
lst = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] reverse_sorted_lst = sorted(lst, reverse=True) print(reverse_sorted_lst)
lst1 = [1, 2, 3] lst2 = [4, 5, 6] merged_lst = lst1 + lst2 print(merged_lst)
lst1 = [1, 2, 3] lst2 = [4, 5, 6] zipped_lst = list(zip(lst1, lst2)) print(zipped_lst)
lst = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] distinct_lst = list(set(lst)) print(distinct_lst)
s = "level" is_palindrome = s == s[::-1] print(is_palindrome)
s = "Hello World" reversed_words = ' '.join(word[::-1] for word in s.split()) print(reversed_words)
s = "hello world" char_count = {char: s.count(char) for char in set(s)} print(char_count)
以上就是云梦编程为大家介绍的关于python初学编程100个代码大全的全部内容了,了解更多相关文章请关注云梦编程网!