# Python 定义示例代码
# 1. 定义变量
x = 10 # 定义一个整数类型的变量 x,并赋值为 10
# 2. 定义函数
def greet(name):
"""
定义一个简单的函数 greet,它接受一个参数 name,并返回一个问候语。
"""
return f"Hello, {name}!"
# 3. 定义类
class Person:
"""
定义一个类 Person,它有一个构造函数 __init__ 和一个方法 say_hello。
"""
def __init__(self, name):
self.name = name # 初始化对象的属性
def say_hello(self):
return f"Hello, I'm {self.name}."
# 4. 定义模块(假设在一个名为 my_module.py 的文件中)
# 在另一个文件中可以通过 import 导入这个模块
# import my_module
# 示例调用
print(greet("Alice")) # 输出: Hello, Alice!
person = Person("Bob")
print(person.say_hello()) # 输出: Hello, I'm Bob.
上一篇:pythonlist
下一篇:python中数据类型
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站