Must read: Python introductory code examples for beginners

王林
Release: 2024-01-04 15:10:59
Original
1729 people have browsed it

Must read: Python introductory code examples for beginners

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.

  1. Hello, World!

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!")
Copy after login

Save the above code as a file named helloworld.py, and run the file in the terminal to output "Hello, World!".

  1. Calculator

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))
Copy after login

Replace the above code Save it as a file named calculator.py and run the file in the terminal to output the calculation results.

  1. Guess the number game

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("猜大了,再试试!")
Copy after login

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.

  1. Judge leap year

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, "不是闰年")
Copy after login

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.

  1. Simple graphics drawing

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)
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!