我可以为您提供一些简单的Python代码示例,这些示例涵盖了从基础到中级的各种概念。
1. **打印 "Hello, World!"**
print("Hello, World!")
2. **变量和算术运算**
a = 5 b = 10 sum = a + b difference = a - b product = a * b quotient = b / a print("Sum:", sum) print("Difference:", difference) print("Product:", product) print("Quotient:", quotient)
3. **条件语句**
x = 10 if x > 5: print("x is greater than 5") elif x < 5: print("x is less than 5") else: print("x is equal to 5")
4. **循环**
for i in range(5): print(i) # While loop i = 0 while i < 5: print(i) i += 1
5. **列表**
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # Adding an element to the list fruits.append("orange") print(fruits)
6. **字典**
person = { "name": "John", "age": 30, "city": "New York" } print(person["name"]) print(person["age"])
7. **函数**
def greet(name): print("Hello, " + name + "!") greet("Alice")
8. **列表推导式**
squares = [x**2 for x in range(10)] print(squares)
这些只是Python编程的一些基础示例,Python是一种非常强大且灵活的语言,可以执行更复杂的任务,如文件操作、网络编程、数据库接口、图形界面开发、科学计算等,如果您对某个特定主题或概念有疑问,欢迎随时提问。