Python for Developers 入门
Jasmin Ludolf
Senior Data Science Content Developer
# Define quantity quantity = 10# Single quotes ingredient_name = 'pasta'# Double quotes also work ingredient_name = "pasta"
ingredient_name = "Basil Leaves"# Convert to lowercase ingredient_name = ingredient_name.lower()
# List of ingredients ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]# Get the value at the first index ingredients[0]
# Creating a dictionary
recipe_dict = {"pasta": 500,
"tomatoes": 400,
"garlic": 15,
"basil": 20,
"olive oil": 30,
"salt": 5}
# Create a set ingredients_set = {"pasta", "tomatoes", "pasta", "basil", "garlic", "olive oil", "salt"}# Create a tuple serving_sizes = (1, 2, 4, 6, 8)
| 运算符 | 功能 |
|---|---|
== |
等于 |
!= |
不等于 |
> |
大于 |
>= |
大于等于 |
< |
小于 |
<= |
小于等于 |
| 关键字 | 功能 | 用法 |
|---|---|---|
if |
条件满足则执行 | 流程中的第一步 |
elif |
否则再判断条件 | 在 if 之后 |
else |
以上都不满足则执行 | 在 elif 之后 |
# Ingredients list ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]# Loop through and print each ingredient for ingredient in ingredients:print(ingredient)
pasta
tomatoes
garlic
basil
olive oil
salt
ingredients_to_add = 5
items_added = 0
while items_added < ingredients_to_add:
items_added += 1
remaining = ingredients_to_add - items_added
if remaining > 3:
print("Several ingredients remaining")
elif remaining >= 1:
print("Almost done!")
else:
print("Shopping list complete!")
| 关键字 | 功能 |
|---|---|
and |
评估多条件是否都为真 |
or |
评估是否有任一条件为真 |
in |
判断值是否在数据结构中 |
not |
判断值是否不在数据结构中 |
list.append()其他内置函数
zip()enumerate()包与模块
ostimevenvpandasrequestsnumpy$$
Python for Developers 入门