Python เบื้องต้นสำหรับนักพัฒนา
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 เบื้องต้นสำหรับนักพัฒนา