当前位置:   article > 正文

10个适合入门学习的python小程序_python解决学习和生活中实际问题的小程序

python解决学习和生活中实际问题的小程序

Python是一种广泛使用的编程语言,它简单易学,适合初学者入门。在这篇文章中,我将分享10个适合入门学习的Python小程序,希望帮助初学者更好地掌握这门语言。

  1. 打印 Hello World:

    print("Hello, World!")
    
    • 1
  2. 计算器:

    num1 = float(input("Enter first number: "))
    num2 = float(input("Enter second number: "))
    result = num1 + num2
    print("Sum:", result)
    
    • 1
    • 2
    • 3
    • 4
  3. 猜数字游戏:

    import random
    
    target_number = random.randint(1, 100)
    guess = int(input("Guess the number between 1 and 100: "))
    
    if guess == target_number:
        print("Congratulations! You guessed the correct number.")
    else:
        print("Sorry, the correct number was:", target_number)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  4. 字符串反转:

    original_string = input("Enter a string: ")
    reversed_string = original_string[::-1]
    print("Reversed string:", reversed_string)
    
    • 1
    • 2
    • 3
  5. 温度转换:

    temperature_celsius = float(input("Enter temperature in Celsius: "))
    temperature_fahrenheit = (temperature_celsius * 9/5) + 32
    print("Temperature in Fahrenheit:", temperature_fahrenheit)
    
    • 1
    • 2
    • 3
  6. 列表操作:

    my_list = [1, 2, 3, 4, 5]
    doubled_list = [x * 2 for x in my_list]
    print("Original list:", my_list)
    print("Doubled list:", doubled_list)
    
    • 1
    • 2
    • 3
    • 4
  7. 求解二次方程:

    import math
    
    a = float(input("Enter the coefficient a: "))
    b = float(input("Enter the coefficient b: "))
    c = float(input("Enter the coefficient c: "))
    
    discriminant = b**2 - 4*a*c
    
    if discriminant > 0:
        x1 = (-b + math.sqrt(discriminant)) / (2*a)
        x2 = (-b - math.sqrt(discriminant)) / (2*a)
        print("Roots are real and different. x1 =", x1, "x2 =", x2)
    elif discriminant == 0:
        x = -b / (2*a)
        print("Roots are real and the same. x =", x)
    else:
        real_part = -b / (2*a)
        imaginary_part = math.sqrt(abs(discriminant)) / (2*a)
        print("Roots are complex. x1 =", real_part, "+", imaginary_part, "i, x2 =", real_part, "-", imaginary_part, "i")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  8. 文件读写:

    with open("example.txt", "w") as file:
        file.write("Hello, this is an example.")
    
    with open("example.txt", "r") as file:
        content = file.read()
        print("File content:", content)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  9. 简单的类和对象:

    class Dog:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def bark(self):
            print("Woof!")
    
    my_dog = Dog("Buddy", 3)
    print("My dog's name is", my_dog.name, "and age is", my_dog.age)
    my_dog.bark()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  10. 基本的异常处理:

    try:
        num1 = int(input("Enter a number: "))
        num2 = int(input("Enter another number: "))
        result = num1 / num2
        print("Result:", result)
    except ValueError:
        print("Please enter valid numbers.")
    except ZeroDivisionError:
        print("Cannot divide by zero.")
    except Exception as e:
        print("An error occurred:", e)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

这些简单的小程序覆盖了一些基本的 Python 概念,包括输入输出、条件语句、循环、列表操作、文件读写、异常处理等。通过编写这些小程序,你可以逐步熟悉 Python 的语法和功能。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/532497
推荐阅读
相关标签
  

闽ICP备14008679号