Must-see introductory code examples for Python programming beginners
Introduction:
Python is a simple, easy-to-learn, powerful programming language, suitable for beginners to get started . This article will introduce some basic Python code examples to beginners to help them get started quickly.
The introduction to any programming language is indispensable without the Hello, World! program. The following is the Python version of Hello, World! code:
print("Hello, World!")
Save the above code as a file named helloworld.py
, and run the file in the terminal to output "Hello, World!".
The following is a simple calculator program that can implement the addition, subtraction, multiplication and division operations of two numbers:
# 加法 def add(a, b): return a + b # 减法 def subtract(a, b): return a - b # 乘法 def multiply(a, b): return a * b # 除法 def divide(a, b): if b != 0: return a / b else: print("Error: divide by zero") # 测试计算器 print(add(3, 5)) print(subtract(8, 2)) print(multiply(4, 6)) print(divide(10, 2)) print(divide(10, 0))
Replace the above code Save it as a file named calculator.py
and run the file in the terminal to output the calculation results.
The following is a program for the guessing game. Players need to guess the random numbers generated by the program:
import random # 生成随机数 target = random.randint(1, 100) # 猜数字游戏 while True: guess = int(input("请输入一个数字: ")) if guess == target: print("猜对了!") break elif guess < target: print("猜小了,再试试!") else: print("猜大了,再试试!")
Place the above code Save it as a file named numbergame.py
and run the file in the terminal to start the number guessing game.
The following is a program to judge leap year. The user inputs a year, and the program judges whether the year is a leap year:
year = int(input("请输入一个年份: ")) if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: print(year, "是闰年") else: print(year, "不是闰年")
Convert the above Save the code as a file named leapyear.py
and run the file in the terminal to determine whether the year is a leap year.
The following is a program to draw simple graphics, using Python's turtle library to draw a square and a regular pentagon:
import turtle # 绘制正方形 for _ in range(4): turtle.forward(100) turtle.right(90) # 绘制正五边形 for _ in range(5): turtle.forward(100) turtle.right(72)
Save the above code as a file named shapes.py
and run the file in the terminal to draw squares and regular pentagons.
Conclusion:
This article provides some basic Python code examples, suitable for beginners to get started. By writing and running these code examples, beginners can understand the basic syntax of Python and the use of commonly used libraries, laying a solid foundation for in-depth learning and application of Python. I hope this article can be helpful to beginners and bring a pleasant programming learning process!
The above is the detailed content of Must read: Python introductory code examples for beginners. For more information, please follow other related articles on the PHP Chinese website!