python常见基础考试题
时间: 2023-09-05 17:10:06 浏览: 42
以下是一些常见的 Python 基础考试题:
1. 请问如何在 Python 中输出 "Hello, world!"?
```
print("Hello, world!")
```
2. 如何将一个字符串转换为整数?
```
int("123")
```
3. 如何获取列表中的第三个元素?
```
my_list[2]
```
4. 如何向列表中添加一个元素?
```
my_list.append("new_item")
```
5. 如何在 Python 中定义一个函数?
```
def my_function():
# function code here
```
6. 如何从列表中删除一个元素?
```
my_list.remove("item_to_remove")
```
7. 如何使用 for 循环遍历一个列表?
```
for item in my_list:
# loop code here
```
8. 如何判断一个字符串是否包含另一个字符串?
```
if "substring" in my_string:
# code to execute if substring is in my_string
```
9. 如何使用条件语句 if-else?
```
if x > 10:
# code to execute if x is greater than 10
else:
# code to execute if x is less than or equal to 10
```
10. 如何从用户输入中读取字符串?
```
user_input = input("Please enter a string: ")
```