随便给我一段python代码
时间: 2024-08-27 07:00:58 浏览: 28
当然可以。这是一个简单的Python代码片段,它定义了一个函数并使用了输入参数的多态性:
```python
def print_length(item):
if isinstance(item, str): # 判断是否为字符串
length = len(item)
print(f"The string's length is: {length}")
elif isinstance(item, list): # 判断是否为列表
length = len(item)
print(f"The list's length is: {length}")
else:
print("The input type is not supported.")
# 测试示例
print_length("Hello") # 输出:The string's length is: 5
print_length([1, 2, 3, 4, 5]) # 输出:The list's length is: 5
print_length(123) # 输出:The input type is not supported.
相关问题
请帮我随便写一段python代码
当然可以,以下是一个简单的Python程序,它会输出“Hello, World!”在屏幕上:
```python
print("Hello, World!")
```
如果你想让程序接受用户输入,并将输入的字符串显示在屏幕上,可以使用以下代码:
```python
name = input("What's your name? ")
print("Hello, " + name + "!")
```
这将提示用户输入他们的名字,并将该名称与“Hello, ”连接起来,最终将结果打印到屏幕上。
用Python随便生成一段尾插法的代码
这里是一个简单的尾插法示例:
```python
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
curr_node = self.head
while curr_node.next:
curr_node = curr_node.next
curr_node.next = new_node
def print_list(self):
curr_node = self.head
while curr_node:
print(curr_node.data)
curr_node = curr_node.next
# Example Usage
linked_list = LinkedList()
linked_list.append(1)
linked_list.append(2)
linked_list.append(3)
linked_list.print_list()
```
运行结果:
```
1
2
3
```