The Python in keyword is returns True if a certain element is present in a sequence [list/tuple/string/range], else it will return False. It is a case-sensitive keyword. It cannot be used as variable-name.
The in keyword is used in two purpose −
Following is the syntax of the Python in keyword −
#Using if-else statement if element in sequence: statement1 statement2 else: statement3 statement4 #using for loop for element in sequence: statement1 statement2Example
Following is the basic example of Python in keyword −
Var1="TutorialsPoint" ele1='s' ele2='z' result_1 = ele1 in Var1 result_2 = ele2 in Var1 print("The element",ele1,"is present in",Var1,"True/False :",result_1) print("The element",ele2,"is present in",Var1,"True/False :",result_2)Output
Following is the output of the above code −
The element s is present in TutorialsPoint True/False : True The element z is present in TutorialsPoint True/False : False'in' keyword in List
The in keyword is used to find whether the element is present in the list or not. If it is present in the list, then it returns True else, returns False.
ExampleIn the following example, We have created a list named List1 which contains some programming language and we are trying to check whether element namedele1 present in list or not −
List1 = ['Python','Java','mysql','CSS','HTML'] ele1='CSS' if ele1 in List1: print(ele1,"is present in the given list :",List1) else: print(ele1,"is not present in the given list :",List1)Output
Following is the output of the above code −
CSS is present in the given list : ['Python', 'Java', 'mysql', 'CSS', 'HTML']'in' Keyword in Loop
The in is used to check the whether the element is in the given sequence or not. The sequence may be list, tuple, string, range. If the element is present in the sequence than the loop will execute.
ExampleLets understand usage of in keyword in for loop. We have used range as a sequence. In this range, the first numeric value is included and second numeric value is excluded. As 15 value is excluded else block is executed −
for x in range(0,15): print(x,end=" ") else: print("False")Output
Following is the output of the above code −
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 False'in' Keyword in Tuple
The in keyword can be used in tuple to identify whether the element is present in tuple or not.
ExampleLets understand the in keyword in tuple with the following example −
Tuple1=(14,23,89,34,7) ele1=100 if ele1 in Tuple1: print(ele1,"is present in given tuple :",Tuple1) else: print(ele1,"is not present in given tuple :",Tuple1)Output
Following is the output of the above code −
100 is not present in given tuple : (14, 23, 89, 34, 7)'in' Keyword in Strings
The in keyword in strings using for loop is used to iterate the given string.
ExampleFollowing is an example for in keyword in strings using for loop −
string1="TutorialsPoint" for i in string1: print(i,end=" ")Output
Following is the output of the above code −
T u t o r i a l s P o i n t
python_keywords.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4