num =10if num >0:print("Positive number")else:print("Not a positive number")
4.判断一个数是否是奇数
num =7if num %2==1:print("Odd number")else:print("Even number")
5.计算一个数的阶乘
num =5
fact =1for i inrange(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.求两个数的最大公约数
defgcd(a, b):if b ==0:return a
else:return gcd(b, a%b)
num1 =12
num2 =30print("GCD of", num1,"and", num2,"is", gcd(num1, num2))
8.打印九九乘法表
for i inrange(1,10):for j inrange(1, i+1):print(i,"*", j,"=", i*j, end="\t")print()
9.判断一个数是否是质数
num =17if num >1:for i inrange(2, num):if(num % i)==0:print(num,"is not a prime number")breakelse:print(num,"is a prime number")else:print(num,"is not a prime number")
10.判断一个年份是否是闰年
year =2024if(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.输出斐波那契数列
deffibonacci(n):if n <=1:return n
else:return(fibonacci(n-1)+ fibonacci(n-2))
nterms =10if nterms <=0:print("Please enter a positive integer")else:print("Fibonacci sequence:")for i inrange(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 **3print("Cube of", num,"is", cube)
15.判断一个数是否是完全数
defisPerfectNumber(num):sum=0for i inrange(1, num):if num % i ==0:sum+= i
ifsum== num:returnTrueelse:returnFalse
num =28if isPerfectNumber(num):print(num,"is a perfect number")else:print(num,"is not a perfect number")
defisPalindrome(num):
temp = num
rev =0while num >0:
dig = num %10
rev = rev *10+ dig
num = num //10if temp == rev:returnTrueelse:returnFalse
num =121if 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 **2print("Square of", num,"is", square)
22.判断一个数是否为偶数
num =6if num %2==0:print(num,"is an even number")else:print(num,"is not an even number")
23.判断一个数是否为负数
num =-7if num <0:print(num,"is a negative number")else:print(num,"is not a negative number")
num =27
cube_root = num **(1/3)print("Cube root of", num,"is", cube_root)
26.将一个字符串转换为大写字母
string ="hello world"print(string.upper())
27.判断一个数是否是素数
num =11if num >1:for i inrange(2,int(num/2)+1):if(num % i)==0:print(num,"is not a prime number")breakelse: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)
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)
import math
angle =45
sin_value = math.sin(math.radians(angle))print("Sine value of", angle,"degrees is", sin_value)
35.判断一个数是否为完全平方数
defisPerfectSquare(num):
sqrt =int(num **0.5)return sqrt * sqrt == num
num =16if isPerfectSquare(num):print(num,"is a perfect square")else:print(num,"is not a perfect square")
defisPerfectCube(num):
cube_root =int(num **(1/3))return cube_root * cube_root * cube_root == num
num =64if 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 inzip(list1, list2)]print("Sum of two lists:", sum_list)
43.判断一个数是否是非负整数
num =-5if num >=0and 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)
defis_leap_year(year):if(year %4==0and year %100!=0)or(year %400==0):returnTrueelse:returnFalse
year =2024if is_leap_year(year):print(year,"是一个闰年")else:print(year,"不是一个闰年")