更新时间:2023-03-17 来源:黑马程序员 浏览量:
Python编程在各行各业应用都十分广泛,对于初学者来说,掌握一些实用的Python小技巧十分关键,它们可以帮助我们在实际的工作中提高效率。接下来笔者列举一些Python实用小技巧供各位参考:
names = ['Alice', 'Bob', 'Charlie'] ages = [25, 30, 35] for name, age in zip(names, ages): print(f"{name} is {age} years old")
输出:
Alice is 25 years old Bob is 30 years old Charlie is 35 years old
fruits = ['apple', 'banana', 'orange'] for index, fruit in enumerate(fruits): print(f"Index {index}: {fruit}")
输出:
Index 0: apple Index 1: banana Index 2: orange
squares = [x**2 for x in range(1, 6)] print(squares)
输出:
[1, 4, 9, 16, 25]
fruits = ['apple', 'banana', 'orange'] prices = [0.5, 0.25, 0.75] fruit_prices = {fruit: price for fruit, price in zip(fruits, prices)} print(fruit_prices)
输出:
{'apple': 0.5, 'banana': 0.25, 'orange': 0.75}
numbers = [1, 2, 3, 2, 1, 4, 5, 4] unique_numbers = set(numbers) print(unique_numbers)
输出:
{1, 2, 3, 4, 5}
fruits = ['apple', 'banana', 'orange'] if 'apple' in fruits: print("I found an apple!")
输出:
I found an apple!
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5] sorted_numbers = sorted(numbers) print(sorted_numbers)
输出:
[1, 1, 2, 3, 4, 5, 5, 6, 9]
fruits = ['apple', 'banana', 'orange'] fruit_string = ', '.join(fruits) print(fruit_string)
输出:
apple, banana, orange
希望这些小技巧能对大家有所帮助!
【AI设计】北京143期毕业仅36天,全员拿下高薪offer!黑马AI设计连续6期100%高薪就业
2025-09-19【跨境电商运营】深圳跨境电商运营毕业22个工作日,就业率91%+,最高薪资达13500元
2025-09-19【AI运维】郑州运维1期就业班,毕业14个工作日,班级93%同学已拿到Offer, 一线均薪资 1W+
2025-09-19【AI鸿蒙开发】上海校区AI鸿蒙开发4期5期,距离毕业21天,就业率91%,平均薪资14046元
2025-09-19【AI大模型开发-Python】毕业33个工作日,就业率已达到94.55%,班均薪资20763元
2025-09-19【AI智能应用开发-Java】毕业5个工作日就业率98.18%,最高薪资 17.5k*13薪,全班平均薪资9244元
2025-09-19