1 Python Keywords
1 Python Keywords
We can also get all the keyword names using the below
code.
import keyword
# printing all keywords at once using "kwlist()"
print("The list of keywords is : ")
print(keyword.kwlist)
Output:
The list of keywords are:
print(None == 0)
print(None == [])
The truth table for “and” is
depicted below.
3 and 0 return 0
3 and 10 return 10
10 or 20 or 30 or 10 or 70
returns 10
The expression x and y first
evaluates x; if x is false, its value
is returned; otherwise, y is
evaluated and the resulting value
is returned.
3 or 0 returns 3
3 or 10 returns 3
0 or 0 or 3 or 10 or 0 returns 3
▪ Logical Operations:
'or' returns ‘True' when at least one operand is ‘True'.
'and' returns ‘True' only when both operands are ‘True'.
'not' negates the operand.
▪ Python “in” Keyword:
It checks if ‘s’ is in the string ‘geeksforgeeks’ and prints accordingly.
It loops through the string’s characters.
▪ Python “is” Keyword:
▪ It checks if two empty strings (‘ ‘) are identical (returns ‘True').
▪ It checks if two empty dictionaries ({}) are identical (returns ‘False').
print(True or False)
Output:
print(False and True)
True
print(not True) False
if 's' in 'geeksforgeeks’: False
s is part of geeksforgeeks
print("s is part of geeksforgeeks")
geeksforgeeks
else: True
print("s is not part of geeksforgeeks") False
for i in 'geeksforgeeks’:
print(i, end=" ")
print("\r")
#whileloop
i=0
while i < 10:
if i == 6:
i += 1
continue
else:
Output
0123456
012345789
Conditional keywords in Python- if,
else, elif
i = 20
Output
if (i == 10): i is 20
print("i is 10")
elif (i == 20):
print("i is 20")
else:
print("i is not
present")
Structure Keywords in Python : def,
class, with, as, pass, lambda
Python def
def keyword is used to declare user defined functions.
def fun():
Output
print("Inside Inside Function
Function")
fun()
class in Python
class Dog:
Output
attr1 = "mammal" mammal
attr2 = "dog" I'm a mammal
def fun(self): I'm a dog
print("I'm a", self.attr1)
print("I'm a", self.attr2)
Rodger = Dog()
print(Rodger.attr1)
Rodger.fun()
With in Python
▪ as Keyword In Python
▪ This code uses the Python math module, which has
been imported with the alias gfg. It calculates and prints
the factorial of 5. The math.factorial() function is used
to calculate the factorial of a number, and in this case,
it calculates the factorial of 5, which is 120.
CODE
print(gfg.factorial(5
))
pass in Python
n = 10
for i in range(n):
# Lambda keyword
g = lambda x: x*x*x
print(g(7))
OUTPUT:
343
Return Keywords in Python- Return, Yield
# import keyword
Output
from math import factorial 3628800
import math 3628800
print(math.factorial(10))
# from keyword
print(factorial(10))