新建一个列表并输出的 python 代码
时间: 2023-05-24 07:02:07 浏览: 42
Sure! Here's the code to create and print a list in Python:
```python
my_list = [1, 2, 3, "hello", True]
print(my_list)
```
This will output:
```
[1, 2, 3, 'hello', True]
```
相关问题
用python新建一个空列表,将1加入列表,并输出
好的,以下是使用Python创建一个空列表,将数字1添加到列表中,并将列表输出的代码:
```python
# 创建一个空列表
my_list = []
# 将数字1添加到列表中
my_list.append(1)
# 输出列表
print(my_list)
```
执行以上代码,将会输出 `[1]`,表示数字1已成功添加到了列表中。
python输出内容至一个新建文件
您可以使用Python内置的open()函数打开一个文件,并使用write()方法向文件写入内容。以下是一些示例代码:
```python
# 打开一个新文件并写入内容
with open('new_file.txt', 'w') as file:
file.write('Hello, world!')
# 以追加模式打开一个文件并在文件末尾添加内容
with open('existing_file.txt', 'a') as file:
file.write('更多内容')
# 读取一个文件的内容
with open('existing_file.txt', 'r') as file:
contents = file.read()
print(contents)
```
这些代码将创建一个新文件"new_file.txt",并在其中写入"Hello, world!"。第二个代码块将打开一个现有的文件,并在其末尾添加"更多内容"。最后一个代码块将打开文件"existing_file.txt"并读取其内容,然后将其打印到屏幕上。