python

第一部分:基础语法

1. 变量的定义与使用

  • name = "John"
  • age = 25

2. 数据类型操作

  • num1 = 10
  • num2 = 5
  • result = num1 + num2
  • print(result)

3. 条件语句

  • score = 85
  • if score >= 90:
  • print("优秀")elif score >= 80:
  • print("良好")else:
  • print("及格")

4. 循环结构

  • for i in range(1, 6):
  • print(i)

第二部分:函数和模块

1. 函数的定义与调用

  • def add(a, b):
  • return a + bresult = add(3, 4)
  • print(result)

2. 参数传递

  • def greet(name):
  • print("Hello, " + name)
  • greet("Alice")

3. 创建和使用模块

  • # math_utils.py
  • def square(num):
  • return num ** 2
  • # main.py
  • import math_utilsresult = math_utils.square(5)print(result)

第三部分:面向对象编程

1. 类的定义和使用

class Person:    def __init__(self, name):        self.name = name

    def greet(self):        print("Hello, my name is " + self.name)

person = Person("John")person.greet()

2. 对象的创建和操作

class Circle:    def __init__(self, radius):        self.radius = radius    def area(self):        return 3.14 * self.radius ** 2circle = Circle(5)print(circle.area())

3. 继承和多态

class Animal:    def sound(self):        pass

class Dog(Animal):    def sound(self):        print("Woof!")

class Cat(Animal):    def sound(self):        print("Meow!")

dog = Dog()cat = Cat()dog.sound()cat.sound()

第四部分:文件操作

1. 读取文件

with open("data.txt", "r") as file:    data = file.read()    print(data)

2. 写入文件

with open("output.txt", "w") as file:    file.write("Hello, World!")

第五部分:异常处理

1. 捕获和处理异常

try:    num = 10 / 0except ZeroDivisionError:    print("Cannot divide by zero.")

2. 优雅地处理错误

def divide(a, b):    try:        result = a / b    except ZeroDivisionError:        result = None    finally:        return resultprint(divide(10, 2))print(divide(10, 0))

第六部分:数据结构和算法

1. 列表操作

numbers = [1, 2, 3, 4, 5]print(len(numbers))print(numbers[2])

2. 字典操作

person = {"name": "John", "age": 25}print(person["name"])print(person.get("age"))

3. 排序算法

numbers = [5, 2, 8, 1, 9]numbers.sort()print(numbers)

第七部分:网络编程

1. Socket编程

import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server_socket.bind(("localhost", 8080))server_socket.listen(1)

client_socket, address = server_socket.accept()print("Connection from: " + str(address))client_socket.close()server_socket.close()

2. HTTP协议应用

import requestsresponse = requests.get("https://www.example.com")print(response.text)

第八部分:数据库编程

1. 访问数据库

import sqlite3connection = sqlite3.connect("database.db")cursor = connection.cursor()cursor.execute("SELECT * FROM students")data = cursor.fetchall()for row in data:    print(row)connection.close()

相关推荐

  1. python

    2024-01-25 00:18:06       56 阅读
  2. python

    2024-01-25 00:18:06       57 阅读
  3. python

    2024-01-25 00:18:06       57 阅读
  4. python

    2024-01-25 00:18:06       43 阅读
  5. python

    2024-01-25 00:18:06       30 阅读
  6. <span style='color:red;'>python</span>

    python

    2024-01-25 00:18:06      29 阅读
  7. python

    2024-01-25 00:18:06       44 阅读
  8. <span style='color:red;'>Python</span>

    Python

    2024-01-25 00:18:06      28 阅读
  9. <span style='color:red;'>Python</span>

    Python

    2024-01-25 00:18:06      32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-25 00:18:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-25 00:18:06       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-25 00:18:06       82 阅读
  4. Python语言-面向对象

    2024-01-25 00:18:06       91 阅读

热门阅读

  1. 240124

    240124

    2024-01-25 00:18:06      53 阅读
  2. (BUUCTF)0ctf_2018_heapstorm2

    2024-01-25 00:18:06       46 阅读
  3. tar 命令基本用法

    2024-01-25 00:18:06       56 阅读
  4. 生成随机数C++

    2024-01-25 00:18:06       48 阅读
  5. k8s之HPA

    k8s之HPA

    2024-01-25 00:18:06      39 阅读
  6. TS的高级类型

    2024-01-25 00:18:06       53 阅读
  7. HJ10 字符个数统计【C语言】

    2024-01-25 00:18:06       56 阅读
  8. 安全认证机制之JWT

    2024-01-25 00:18:06       56 阅读
  9. HJ9 提取不重复的整数【C语言】

    2024-01-25 00:18:06       56 阅读