Python实现的40个入门小程序,它们涉及到Python的基础语法、文件操作、数据结构、函数等方面,适合初学者参考练习。
1. 打印“Hello, world!”
```python
print("Hello, world!")
```
2. 计算两个数之和
```python
a = int(input("请输入第一个整数:"))
b = int(input("请输入第二个整数:"))
print("两数之和为:", a+b)
```
3. 判断闰年
```python
hadoop安装实验总结year = int(input("请输入一个年份:"))
if (year%4 == 0 and year%100 != 0) or (year%400 == 0):
    print(year, "是闰年")
else:
    print(year, "不是闰年")
```
4. 猜数字游戏
```python
import random
num = random.randint(1, 100)
while True:
    guess = int(input("请猜一个1-100之间的数字:"))
    if guess > num:
        print("猜大了")
    elif guess < num:
        print("猜小了")
    else:
        print("恭喜你,猜对了!")
        break
```
5. 列表排序
```python
nums = [36, 5, 12, 9, 21]
nums.sort()
print(nums)
```
6. 字符串反转
```python
s = "hello world"
s = s[::-1]
print(s)
```
7. 文件读写
```python
# 文件写入
with open("", "w") as f:
    f.write("hello\nworld")
# 文件读取
with open("", "r") as f:
    for line in f:
        print(line.strip())
```
8. 去重
```python
nums = [3, 5, 7, 3, 9, 7]
nums = list(set(nums))
print(nums)
```
9. 生成器
```python
def fib(n):
    a, b = 0, 1
    while n > 0:
学python编程入门        yield b
        a, b = b, a+b
        n -= 1
for i in fib(10):
    print(i)
```
10. Lambda表达式
```python
nums = [36, 5, 12, 9, 21]
nums = sorted(nums, key=lambda x:x%10)
print(nums)
```
11. 过滤器
```python
nums = [36, 5, 12, 9, 21]
nums = list(filter(lambda x:x%2==0, nums))
print(nums)
```
12. 映射函数
```python
nums = [36, 5, 12, 9, 21]
nums = list(map(lambda x:x*2, nums))
print(nums)
```
13. 列表推导式
```python
nums = [i*2 for i in range(5)]
print(nums)
```
14. 字典
```python
d = {"name": "张三", "age": 18, "gender": "男"}
print(d["name"], d.get("age"), d.keys())
```
15. 列表嵌套
```python
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in matrix:
    for col in row:
        print(col, end=" ")
    print()
```
16. 函数定义
```python
def add(a, b):
    return a+b
print(add(3, 5))
```
17. 函数参数
```python
def hello(name="world"):
    print("Hello, "+name+"!")
hello("Alice")
hello()
```
18. 递归函数
```python
def fib(n):
    if n < 2:
        return n
    else:
        return fib(n-1) + fib(n-2)
print(fib(10))
```
19. 类定义
```python
class Person:
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        der = gender
p = Person("张三", 18, "男")
print(p.name, p.age, p.gender)
```
20. 继承
```python
class Student(Person):
    def __init__(self, name, age, gender, grade):
        super().__init__(name, age, gender)
        ade = grade
s = Student("李四", 17, "女", 90)
print(s.name, s.age, s.gender, s.grade)
```
21. 模块导入
```python
import random
print(random.randint(1, 10))
```
22. 包导入
```python
from math import sin
print(sin(3.14))
```
23. 异常处理
```python
try:
    num = int(input("请输入一个整数:"))
    print(num)
except ValueError:
    print("输入的不是整数")
```
24. 时间模块
```python
import time
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
```
25. 正则表达式
```python
import re
s = "hello 123 world"
pattern = r"\d+"
print(re.findall(pattern, s))
```
26. 字符串格式化
```python
name = "Alice"
age = 18
print("My name is %s. I am %d years old." % (name, age))
```
27. 迭代器
```python我的世界notch与him
nums = [1, 2, 3]
it = iter(nums)
print(next(it))
print(next(it))
print(next(it))
```
28. 生成式函数
```python
def odd(n):
    for i in range(n):竞选大队委ppt模板免费下载
        if i%2 == 1:
            yield i
java matches方法
for i in odd(10):
    print(i)
```
29. 装饰器
```python
def deco(func):
    def wrapper():
        print("start")
        func()
        print("end")

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。