python如何输出值

原创
admin 1天前 阅读数 3 #Python

Python中输出值的基本方式是使用print函数,以下是一些例子,展示了如何在Python中输出不同类型的数据。

1、输出字符串:

print("Hello, World!")

2、输出整数:

print(123)

3、输出浮点数:

print(3.14159)

4、输出列表:

print([1, 2, 3, 4, 5])

5、输出字典:

print({"name": "John", "age": 25, "city": "New York"})

6、输出自定义对象的字符串表示:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    def __str__(self):
        return f"{self.name}, {self.age}"
        
person = Person("John", 25)
print(person)  # 输出: John, 25

Python输出值的一些基本示例,在实际编程过程中,可能会输出更复杂的数据结构和自定义对象,但基本方法是一样的。

作者文章
热门
最新文章