100个python代码大全详细介绍

发布时间:2024年01月04日

1.输出Hello World

print("Hello World!")

2.计算两个数的和

num1 = 5
num2 = 7
sum = num1 + num2
print("The sum of", num1, "and", num2, "is", sum)

3.判断一个数是否是正数

num = 10
if num > 0:
    print("Positive number")
else:
    print("Not a positive number")

4.判断一个数是否是奇数

num = 7
if num % 2 == 1:
    print("Odd number")
else:
    print("Even number")

5.计算一个数的阶乘

num = 5
fact = 1
for i in range(1, num+1):
    fact = fact * i
print("Factorial of", num, "is", fact)

6.判断一个字符串是否是回文

string = "racecar"
if string == string[::-1]:
    print("Palindrome")
else:
    print("Not a palindrome")

7.求两个数的最大公约数

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

num1 = 12
num2 = 30

print("GCD of", num1, "and", num2, "is", gcd(num1, num2))

8.打印九九乘法表

for i in range(1, 10):
    for j in range(1, i+1):
        print(i, "*", j, "=", i*j, end="\t")
    print()

9.判断一个数是否是质数

num = 17

if num > 1:
    for i in range(2, num):
        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")

10.判断一个年份是否是闰年

year = 2024

if (year % 4) == 0:
    if (year % 100) == 0:
        if (year % 400) == 0:
            print(year, "is a leap year")
        else:
            print(year, "is not a leap year")
    else:
        print(year, "is a leap year")
else:
    print(year, "is not a leap year")

11.计算一个数的平方根

import math

num = 25

sqrt = math.sqrt(num)
print("Square root of", num, "is", sqrt)

12.输出斐波那契数列

def fibonacci(n):
    if n <= 1:
        return n
    else:
        return (fibonacci(n-1) + fibonacci(n-2))

nterms = 10

if nterms <= 0:
    print("Please enter a positive integer")
else:
    print("Fibonacci sequence:")
    for i in range(nterms):
        print(fibonacci(i))

13.判断一个字符串是否是数字

string = "12345"

if string.isdigit():
    print("String is a number")
else:
    print("String is not a number")

14.计算一个数的立方

num = 3

cube = num ** 3
print("Cube of", num, "is", cube)

15.判断一个数是否是完全数

def isPerfectNumber(num):
    sum = 0
    for i in range(1, num):
        if num % i == 0:
            sum += i
    if sum == num:
        return True
    else:
        return False

num = 28

if isPerfectNumber(num):
    print(num, "is a perfect number")
else:
    print(num, "is not a perfect number")

16.将一个列表中的元素逆序排列

list1 = [1, 2, 3, 4, 5]

list1.reverse()
print("Reversed list:", list1)

17.判断一个数是否是回文数

def isPalindrome(num):
    temp = num
    rev = 0
    while num > 0:
        dig = num % 10
        rev = rev * 10 + dig
        num = num // 10
    if temp == rev:
        return True
    else:
        return False

num = 121

if isPalindrome(num):
    print(num, "is a palindrome number")
else:
    print(num, "is not a palindrome number")

18.生成随机数

import random

print(random.randint(1, 100))

19.计算一个数的绝对值

num = -5

abs_value = abs(num)
print("Absolute value of", num, "is", abs_value)

20.将字符串转换为小写字母

string = "HELLO WORLD"

print(string.lower())

21.计算一个数的平方

num = 5

square = num ** 2
print("Square of", num, "is", square)

22.判断一个数是否为偶数

num = 6

if num % 2 == 0:
    print(num, "is an even number")
else:
    print(num, "is not an even number")

23.判断一个数是否为负数

num = -7

if num < 0:
    print(num, "is a negative number")
else:
    print(num, "is not a negative number")

24.将两个列表合并为一个

list1 = [1, 2, 3]
list2 = [4, 5, 6]

merged_list = list1 + list2
print("Merged list:", merged_list)

25.计算一个数的立方根

num = 27

cube_root = num ** (1/3)
print("Cube root of", num, "is", cube_root)

26.将一个字符串转换为大写字母

string = "hello world"

print(string.upper())

27.判断一个数是否是素数

num = 11

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")

28.计算一个数的十进制对数

import math

num = 1000

log_value = math.log10(num)
print("Logarithmic value of", num, "is", log_value)

29.删除列表中指定位置的元素

list1 = [1, 2, 3, 4, 5]

del list1[2]
print("Updated list:", list1)

30.判断一个字符串是否是子串

string1 = "hello world"
string2 = "world"

if string2 in string1:
    print(string2, "is a substring of", string1)
else:
    print(string2, "is not a substring of", string1)

31.计算两个数的差

num1 = 10
num2 = 6

diff = num1 - num2
print("Difference between", num1, "and", num2, "is", diff)

32.判断一个数是否为正整数

num = 10.5

if num > 0 and num.is_integer():
    print(num, "is a positive integer")
else:
    print(num, "is not a positive integer")

33.交换两个变量的值

num1 = 5
num2 = 7

temp = num1
num1 = num2
num2 = temp

print("After swapping, num1 is", num1, "and num2 is", num2)

34.计算正弦值

import math

angle = 45

sin_value = math.sin(math.radians(angle))
print("Sine value of", angle, "degrees is", sin_value)

35.判断一个数是否为完全平方数

def isPerfectSquare(num):
    sqrt = int(num ** 0.5)
    return sqrt * sqrt == num

num = 16

if isPerfectSquare(num):
    print(num, "is a perfect square")
else:
    print(num, "is not a perfect square")

36.计算两个数的积

num1 = 4
num2 = 5

product = num1 * num2
print("Product of", num1, "and", num2, "is", product)

37.判断一个数是否为偶数

num = 8

if num % 2 == 0:
    print(num, "is an even number")
else:
    print(num, "is not an even number")

38.计算一个数的倒数

num = 5

reciprocal = 1 / num
print("Reciprocal of", num, "is", reciprocal)

39.将字符串反转

string = "hello world"

reverse_string = string[::-1]
print("Reverse of", string, "is", reverse_string)

40.将字符串中的所有字母转换为小写字母

string = "HELLO WORLD"

print(string.lower())

41.判断一个数是否为完全立方数

def isPerfectCube(num):
    cube_root = int(num ** (1/3))
    return cube_root * cube_root * cube_root == num

num = 64

if isPerfectCube(num):
    print(num, "is a perfect cube")
else:
    print(num, "is not a perfect cube")

42.将两个列表按照元素顺序相加

list1 = [1, 2, 3]
list2 = [4, 5, 6]

sum_list = [x + y for x, y in zip(list1, list2)]
print("Sum of two lists:", sum_list)

43.判断一个数是否是非负整数

num = -5

if num >= 0 and num.is_integer():
    print(num, "is a non-negative integer")
else:
    print(num, "is not a non-negative integer")

44.计算一个数的立方根

num = 27

cube_root = num ** (1/3)
print("Cube root of", num, "is", cube_root)

45.计算一个数的幂

num = 2
power = 10

result = num ** power
print(num, "to the power of", power, "is", result)

46.将一个字符串中的所有字母转换为大写字母

string = "hello world"
uppercase_string = string.upper()
print(uppercase_string)

47.判断一个数是否为负整数

num = -10
if num < 0 and isinstance(num, int):
    print(num, "是一个负整数")
else:
    print(num, "不是一个负整数")

48.计算正切值

import math
angle = 30
tan_value = math.tan(math.radians(angle))
print("角度", angle, "的正切值为", tan_value)

49.判断一个数是否是非负数

num = -5.5
if num >= 0 and isinstance(num, (int, float)):
    print(num, "是一个非负数")
else:
    print(num, "不是一个非负数")

50.判断两个字符串是否相等

string1 = "hello"
string2 = "HELLO"
if string1 == string2:
    print("字符串相等")
else:
    print("字符串不相等")

51.计算两个数的商和余数

dividend = 17
divisor = 3
quotient = dividend // divisor
remainder = dividend % divisor
print("商:", quotient)
print("余数:", remainder)

52.判断一个数是否是浮点数

num = 3.14
if isinstance(num, float):
    print(num, "是一个浮点数")
else:
    print(num, "不是一个浮点数")

53.判断一个年份是否为闰年

year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print(year, "是一个闰年")
else:
    print(year, "不是一个闰年")

54.判断一个字符串是否为空

string = ""
if len(string) == 0:
    print("字符串为空")
else:
    print("字符串不为空")

55.计算两个数的平均值

num1 = 4
num2 = 6
average = (num1 + num2) / 2
print("平均值:", average)

56.判断一个数是否是非零数

num = 0
if num != 0 and isinstance(num, (int, float)):
    print(num, "是一个非零数")
else:
    print(num, "是一个零数")

57.判断一个数是否是整数

num = 5.5
if isinstance(num, int):
    print(num, "是一个整数")
else:
    print(num, "不是一个整数")

58.计算列表中元素的和

list1 = [1, 2, 3, 4, 5]
sum_list = sum(list1)
print("列表元素的和:", sum_list)

59.判断一个数是否是非正整数

num = -10
if num <= 0 and isinstance(num, int):
    print(num, "是一个非正整数")
else:
    print(num, "不是一个非正整数")

60.计算两个数的差的绝对值

num1 = 10
num2 = 6
abs_diff = abs(num1 - num2)
print("两个数的差的绝对值为:", abs_diff)

61.判断一个字符串是否包含数字

string = "hello123"
if any(char.isdigit() for char in string):
    print("字符串包含数字")
else:
    print("字符串不包含数字")

62.判断一个列表是否为空

list1 = []
if len(list1) == 0:
    print("列表为空")
else:
    print("列表不为空")

63.求一个数的平方根

import math
num = 16
sqrt_num = math.sqrt(num)
print("数", num, "的平方根为", sqrt_num)

64.判断一个字符串是否为回文字符串

def is_palindrome(string):
    if string == string[::-1]:
        return True
    else:
        return False

string = "level"
if is_palindrome(string):
    print("字符串是回文字符串")
else:
    print("字符串不是回文字符串")

65.判断一个年份是否为闰年的函数

def is_leap_year(year):
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return True
    else:
        return False

year = 2024
if is_leap_year(year):
    print(year, "是一个闰年")
else:
    print(year, "不是一个闰年")

66.计算列表中的最大值

list1 = [1, 5, 3, 8, 2]
max_value = max(list1)
print("列表中的最大值:", max_value)

67.判断一个数是否是正整数

num = 10
if num > 0 and isinstance(num, int):
    print(num, "是一个正整数")
else:
    print(num, "不是一个正整数")

68.将一个字符串中的所有字母转换为小写字母

string = "Hello World"
lowercase_string = string.lower()
print(lowercase_string)

69.求两个数的最大公约数

def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

num1 = 24
num2 = 36
gcd_value = gcd(num1, num2)
print("两个数的最大公约数:", gcd_value)

70.判断一个数是否是非正数

num = 0
if num <= 0 and isinstance(num, (int, float)):
    print(num, "是一个非正数")
else:
    print(num, "不是一个非正数")

71.计算列表中元素的平均值

list1 = [1, 2, 3, 4, 5]
average = sum(list1) / len(list1)
print("列表元素的平均值:", average)

72.判断一个数是否是奇数

num = 7
if num % 2 != 0 and isinstance(num, int):
    print(num, "是一个奇数")
else:
    print(num, "不是一个奇数")

73.判断一个字符串是否为纯数字字符串

string = "12345"
if string.isdigit():
    print("字符串是纯数字字符串")
else:
    print("字符串不是纯数字字符串")

74.判断一个数是否是正数

num = 10
if num > 0 and isinstance(num, (int, float)):
    print(num, "是一个正数")
else:
    print(num, "不是一个正数")

75.计算两个数的乘积

num1 = 4
num2 = 5
product = num1 * num2
print("两个数的乘积:", product)

76.判断一个列表是否包含重复元素

list1 = [1, 2, 3, 4, 4]
if len(set(list1)) < len(list1):
    print("列表包含重复元素")
else:
    print("列表不包含重复元素")

77.判断一个数是否是偶数

num = 8
if num % 2 == 0 and isinstance(num, int):
    print(num, "是一个偶数")
else:
    print(num, "不是一个偶数")

78.判断一个字符串是否为全大写字符串

string = "HELLO"
if string.isupper():
    print("字符串是全大写字符串")
else:
    print("字符串不是全大写字符串")

79.判断一个数是否是非负整数

num = 10
if num >= 0 and isinstance(num, int):
    print(num, "是一个非负整数")
else:
    print(num, "不是一个非负整数")

80.计算两个数的差

num1 = 10
num2 = 6
difference = num1 - num2
print("两个数的差:", difference)

81.判断一个字符串是否为全小写字符串

string = "hello"
if string.islower():
    print("字符串是全小写字符串")
else:
    print("字符串不是全小写字符串")

82.判断一个数是否是非负数

num = 0
if num >= 0 and isinstance(num, (int, float)):
    print(num, "是一个非负数")
else:
    print(num, "不是一个非负数")

83.判断一个字符串是否以指定的前缀开头

string = "hello world"
prefix = "hello"
if string.startswith(prefix):
    print("字符串以指定的前缀开头")
else:
    print("字符串不以指定的前缀开头")

84.计算两个数的除法结果

num1 = 10
num2 = 2
result = num1 / num2
print("两个数的除法结果:", result)

85.判断一个数是否为正数

num = 10
if num > 0 and isinstance(num, (int, float)):
    print(num, "是一个正数")
else:
    print(num, "不是一个正数")

86.判断一个字符串是否以指定的后缀结尾

string = "hello world"
suffix = "world"
if string.endswith(suffix):
    print("字符串以指定的后缀结尾")
else:
    print("字符串不以指定的后缀结尾")

87.判断一个数是否是非负浮点数

num = 3.14
if num >= 0 and isinstance(num, float):
    print(num, "是一个非负浮点数")
else:
    print(num, "不是一个非负浮点数")

88.判断一个字符串是否为全字母字符串

string = "abcd"
if string.isalpha():
    print("字符串是全字母字符串")
else:
    print("字符串不是全字母字符串")

89.判断一个数是否为负数

num = -5
if num < 0 and isinstance(num, (int, float)):
    print(num, "是一个负数")
else:
    print(num, "不是一个负数")

90.判断一个字符串是否为全数字字符串

string = "12345"
if string.isnumeric():
    print("字符串是全数字字符串")
else:
    print("字符串不是全数字字符串")

91.求一个列表中的最小值

list1 = [5, 2, 8, 1, 3]
min_value = min(list1)
print("列表中的最小值:", min_value)

92.判断一个数是否为负浮点数

num = -3.14
if num < 0 and isinstance(num, float):
    print(num, "是一个负浮点数")
else:
    print(num, "不是一个负浮点数")

93.判断一个字符串是否为全空格字符串

string = "    "
if string.isspace():
    print("字符串是全空格字符串")
else:
    print("字符串不是全空格字符串")

94.判断一个数是否为非正数

num = -10
if num <= 0 and isinstance(num, (int, float)):
    print(num, "是一个非正数")
else:
    print(num, "不是一个非正数")

95.判断一个字符串是否只包含字母和数字

string = "hello123"
if string.isalnum():
    print("字符串只包含字母和数字")
else:
    print("字符串不只包含字母和数字")

96.判断一个数是否为负整数或零

num = 0
if num <= 0 and isinstance(num, int):
    print(num, "是一个负整数或零")
else:
    print(num, "不是一个负整数或零")

97.将一个列表中的元素按照升序排序

list1 = [5, 2, 8, 1, 3]
sorted_list = sorted(list1)
print("升序排序后的列表:", sorted_list)

98.判断一个数是否为负整数或负浮点数

num = -5.5
if num < 0 and isinstance(num, (int, float)):
    print(num, "是一个负整数或负浮点数")
else:
    print(num, "不是一个负整数或负浮点数")

99.判断一个字符串是否只包含字母和空格

string = "Hello World"
if all(char.isalpha() or char.isspace() for char in string):
    print("字符串只包含字母和空格")
else:
    print("字符串不只包含字母和空格")

100.将一个字符串按照指定的分隔符切割成列表

string = "apple,banana,orange"
separator = ","
split_list = string.split(separator)
print("切割后的列表:", split_list)

请注意,以上示例代码仅用于演示基本概念和操作,并可能不包含完整的错误处理或边界情况处理。在实际开发中,建议根据具体需求进行适当的修改和完善

文章来源:https://blog.csdn.net/zznn0306/article/details/135390277
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。