python tutorial
Python In Keyword
Learn more about Python, one of the world’s most versatile and popular programming languages.
Tutorial Contents
There are multiple keywords in Python that help in writing conditional logic in code. One such keyword is in
.
Python In Keyword in Conditional Statements
The in
keyword allows developers to test whether strings, tuples, lists and other sequence-based data types contain a certain value. in
keyword can be used to test whether certain characters are present in a string or a certain value present in a list or tuple. in
keyword is very useful to write conditional statements that will execute a block of code only when a value or character is found in a list or string.
Python In Keyword in for Loops
in
keyword is also useful while iterating through a for loop. in
keyword with a for loop provides each item or value in the string or list without having to initialize a loop variable.
# using 'in' keyword in conditional statements
>>> grades = [65, 83, 75, 100, 57, 90]
>>> if 100 in grades:
… print('We have found a perfect score')
…
We have found a perfect score
# using 'in' keyword in for loops
>>> todo_list = ['start project proposal', 'review the proposal with Team Lead', 'continue working on the proposal', 'present the proposal']
>>> for todo in todo_list:
… print('Things to do today: ', todo)
…
Things to do today: start project proposal
Things to do today: review the proposal with Team Lead
Things to do today: continue working on the proposal
Things to do today: present the proposal
Learn Python Today
Get hands-on experience writing code with interactive tutorials in our free online learning platform.
- Free and fun
- Designed for beginners
- No downloads or setup required