admin 管理员组

文章数量: 887021


2024年2月23日发(作者:python的parser库)

python菜鸟教程100例

1. 输出 "Hello, World!"

```python

print("Hello, World!")

```

2. 计算两个数字的和

```python

num1 = 1

num2 = 2

sum = num1 + num2

print("两个数字的和为:", sum)

```

3. 判断一个数是正数、负数还是零

```python

num = 10

if num > 0:

print("正数")

elif num < 0:

print("负数")

else:

print("零")

```

4. 判断一个数是否为素数

```python

num = 7

if num > 1:

for i in range(2, num):

if (num % i) == 0:

print(num, "不是素数")

break

else:

print(num, "是素数")

else:

print(num, "不是素数")

```

5. 求一个数的阶乘

```python

num = 5

factorial = 1

if num < 0:

print("负数没有阶乘")

elif num == 0:

print("0 的阶乘为 1")

else:

for i in range(1, num + 1):

factorial = factorial * i

print(num, "的阶乘为", factorial)

```

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

```python

string = "level"

if string == string[::-1]:

print(string, "是回文字符串")

else:

print(string, "不是回文字符串")

```

7. 打印九九乘法表

```python

for i in range(1, 10):

for j in range(1, i + 1):

print(i, "*", j, "=", i * j, end="t")

print()

```

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

```python

year = 2020

if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:

print(year, "是闰年")

else:

print(year, "不是闰年")

```

9. 转换华氏温度为摄氏温度

```python

fahrenheit = 100

celsius = (fahrenheit - 32) * 5 / 9

print("华氏温度", fahrenheit, "对应的摄氏温度为", celsius)

```

10. 计算斐波那契数列

```python

def fibonacci(n):

if n <= 0:

return "输入的数字必须大于 0"

elif n == 1:

return 0

elif n == 2:

return 1

else:

return fibonacci(n-1) + fibonacci(n-2)

num = 10

for i in range(1, num+1):

print(fibonacci(i), end=" ")

```


本文标签: 是否 判断 摄氏温度 数字 字符串