Python 102!Python 简介:中级概念。

2025-06-08

Python 102!Python 简介:中级概念。

图像

在Lux Academy东非数据科学学院举办的 Python 训练营的第一部分中,我们介绍了 Python 的基本概念,包括:

  • Python 安装和开发环境设置。

  • Python 中的注释和语句。

  • Python 中的变量。

  • 关键字、标识符和文字。

  • Python 中的运算符。

  • 比较运算符和条件

  • Python 中的数据类型

  • 控制流。

  • Python 中的函数。

如果您还没有阅读,可以使用此链接阅读有关这些概念的更多信息。

在本教程的第二部分中,我们将介绍中级 Python 概念,包括:

  • 基础数据结构,这些列表,元组,字典集。

  • 函数和递归。

  • Python 中的匿名函数或 lambda 函数。

  • 全球、本地和非本地。

  • 
Python 全局关键字。

  • 
Python 模块。

  • 
Python 包。

  • Python 中的类。

  • 闭包和装饰器

  • 继承和封装。

基础数据结构列表、元组、字典集。

数据结构是一种描述组织数据片段的特定方式,以便更容易地应用操作和算法,例如树型数据结构通常允许高效的搜索算法,因此无论何时在程序中实现搜索组件,建议使用树型数据结构。

基本上,一般计算机科学中的数据结构只是一种组织数据以使某些操作更容易或更难的方式。

问题:数据类型和数据结构有什么区别?

用外行人的话来说,数据结构是程序中组织和存储数据的特殊格式。可以把它们想象成附加了结构的数据。

在 Python 中,您可以使用内置类型(如列表、集合、元组等),或者使用类和函数定义自己的类型。

1)列表- 列表是项目的有序集合,您可以使用[ ]list()构造函数创建它们。

  • 要访问或编辑列表中的值,请使用索引。
  • 要添加项目,请使用 append() 方法。
  • 要删除项目,请使用 remove 或 del 语句。
# how to create a list.
todo = ['sleep', 'clean', 'sleep']

# how to access or edit a list items.
todo[0] = 'vacuum'

# how to add an items. 
todo.append('mow yard')

# how to remove an items
todo .remove('sleep')

Enter fullscreen mode Exit fullscreen mode

元组

元组是类似列表的有序项目集合,但不可变,即不可更改。

我们通过在可选()中提供逗号分隔的值或使用 tuple() 构造函数来创建元组。元组一旦创建,就无法更改,也就是说,您无法添加、编辑或删除其中的任何项。

# how to create a tuple 
student = ("freshman", 15, 4.0)

# how to access an item
print(student[0])

# how to access items
print(student[0:2])

# Hoe to delet a tuple
del student
Enter fullscreen mode Exit fullscreen mode

字典。

字典用于以键:值对的形式存储数据值。

字典是一个有序*、可更改且不允许重复的集合。

mycar = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
Enter fullscreen mode Exit fullscreen mode

集合用于在单个变量中存储多个项目,我们还可以将集合定义为无序、无索引且不支持重复的集合。

myset = {"apple", "banana", "cherry"}
print(myset)
Enter fullscreen mode Exit fullscreen mode

Python 中的函数。

函数是一组有组织的可重复使用的指令块,用于执行某些相关操作。

例如:

如果你有 16 行代码,在程序中出现了 4 次,你不必重复 4 次。你只需编写一个函数并调用它。

功能可实现:

  • 代码的可重复使用性最大限度地减少了冗余。

  • 程序分解使事物变得有条理。

在 Python 中我们有两种类型的函数:

1). 用户定义函数。

2). 内置函数。


def my_function(x):
    return list(dict.fromkeys(x))
    mylist = myfunction(["a", "b", "c", "d"])
    print(mylist)

my_function()
Enter fullscreen mode Exit fullscreen mode

 参数和参数

参数是函数定义时括号内定义的变量。简单来说,它们是在声明函数时写的。

def sum(a,b):
  print(a+b)

# Here the values 1,2 are arguments
sum(1,2)
Enter fullscreen mode Exit fullscreen mode

参数是函数调用时传递给函数的值。它可以是变量、值或对象,作为输入传递给函数或方法。它们是在调用函数时写入的

def sum(a,b):
  print(a+b)

# Here the values 1,2 are arguments
sum(1,2)
Enter fullscreen mode Exit fullscreen mode

装饰器。

装饰器是 Python 中的一种设计模式,它允许用户在不修改现有对象结构的情况下为其添加新功能。装饰器通常在要装饰的函数定义之前调用。

from fastapi import FastAPI

app = FastAPI()

#Here is the decorator 
@app.get("/")
async def root():
    return {"message": "Hello World"}
Enter fullscreen mode Exit fullscreen mode

Python 中的匿名函数或 lambda 函数。

Lambda 函数,也称为“匿名函数”,与常规 Python 函数相同,但可以在不指定名称的情况下进行定义。普通函数使用 def 关键字定义,而匿名函数使用 lambda 关键字定义。不过,匿名函数只能使用一行表达式。


lambda arguments: expression 

Enter fullscreen mode Exit fullscreen mode

Lambda 函数可以包含任意数量的参数,但只能包含一个表达式。该表达式会被求值并返回。任何需要函数对象的地方都可以使用 Lambda 函数。


double = lambda x: x * 2

print(double(5))

Enter fullscreen mode Exit fullscreen mode

Python 全局变量、局部变量和非局部变量。

在 Python 中,在函数外部或全局作用域内声明的变量称为全局变量。这意味着全局变量可以在函数内部或外部访问。

x = "global"

def foo():
    print("x inside:", x)


foo()
print("x outside:", x)
Enter fullscreen mode Exit fullscreen mode

在函数体内或局部范围内声明的变量称为局部变量

def foo():
    y = "local"


foo()
print(y)
Enter fullscreen mode Exit fullscreen mode

非局部变量用于未定义局部作用域的嵌套函数中。这意味着变量既不能位于局部作用域,也不能位于全局作用域。

def outer():
    x = "local"

    def inner():
        nonlocal x
        x = "nonlocal"
        print("inner:", x)

    inner()
    print("outer:", x)


outer()
Enter fullscreen mode Exit fullscreen mode
鏂囩珷鏉ユ簮锛�https://dev.to/grayhat/python-102-introduction-to-python-intermediate-concepts-1881
PREV
我如何克隆 VSCode 并将其用作我的投资组合
NEXT
现代数据工程路线图 - 2024