ob-vaults/Phoenix/Python/`in` usage in python.md
2024-09-12 17:54:01 +03:30

46 lines
741 B
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

status: #doc
Tags: #python #programming
links:[[python]]
Date: 2023-05-01
___
# `in` usage in python
## Definition and Usage
The `in` keyword has two purposes:
The `in` keyword is used to check if a value is present in a sequence (list, range, string etc.).
The `in` keyword is also used to iterate through a sequence in a `for` [[loop in python|loop]]:
- Loop through a list and print the items:
```python
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
```
- Check if "banana" is present in the list:
```python
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
  print("yes")
```
---
# References
https://www.w3schools.com/python/ref_keyword_in.asp