Home >Backend Development >Python Tutorial >5 essential Python entry example codes

5 essential Python entry example codes

WBOY
WBOYOriginal
2024-01-13 08:39:05922browse

5 essential Python entry example codes

Python entry code: 5 essential examples for learning

Python is a simple and easy-to-learn high-level programming language, widely used in data analysis, machine learning, and networking Reptiles and other fields. For beginners, it is important to master some basic Python code. This article will introduce 5 simple example codes to help beginners quickly get started with Python programming.

  1. Print Hello, World!
print("Hello, World!")

This is the simplest example in Python programming, used to verify whether your environment is installed correctly. Use the print function to output the string "Hello, World!" to the console.

  1. Calculating the circumference and area of ​​a circle
import math

radius = float(input("请输入圆的半径:"))
circumference = 2 * math.pi * radius
area = math.pi * radius ** 2

print("圆的周长为:", circumference)
print("圆的面积为:", area)

This example is used to demonstrate how to perform simple mathematical calculations. The user inputs the radius of the circle, and the code uses the math module to calculate the circumference and area of ​​the circle and outputs the results.

  1. Determine whether a number is prime
num = int(input("请输入一个正整数:"))
is_prime = True

if num <= 1:
    is_prime = False
else:
    for i in range(2, int(math.sqrt(num)) + 1):
        if num % i == 0:
            is_prime = False
            break

if is_prime:
    print(num, "是素数")
else:
    print(num, "不是素数")

This example shows how to use loops and conditional statements to determine whether a number is prime in Python. The code uses the remainder operator to determine whether it is divisible. If it is divisible, it is not a prime number.

  1. Guessing Number Game
import random

number = random.randint(1, 100)
guess = int(input("猜一个1到100的数字:"))

while guess != number:
    if guess > number:
        print("猜大了")
    else:
        print("猜小了")
    guess = int(input("再猜一个1到100的数字:"))

print("恭喜你猜对了!")

This example shows how to use loops and random number generators to implement a simple guessing number game. The code will generate a random number between 1 and 100, then prompt the user to guess a number, and output whether the guess is higher or lower based on the user's guess until the guess is correct.

  1. Crawling web content
import requests

url = "https://www.example.com"
response = requests.get(url)
content = response.text

print(content)

This example shows how to use the requests library for web crawling. The code uses the requests library to send a GET request to obtain the content of the specified URL and output the content to the console.

These are 5 basic Python example codes, covering basic concepts such as printing, mathematical operations, conditional judgments, loops, random numbers and web crawlers. By studying these sample codes, beginners can quickly master the basic knowledge of Python programming and lay a solid foundation for further learning and application.

The above is the detailed content of 5 essential Python entry example codes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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