DEV Community

Cover image for Python Keywords๐Ÿ๐Ÿ”‘
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on

Python Keywords๐Ÿ๐Ÿ”‘

  • All programming languages reserve certain words to have a special meaning.
  • These words are called keywords.
  • With keywords, the programmer can issue commands to the compiler or interpreter.
  • They let you tell the computer what to do.
  • Without keywords, the computer could not make sense from the seemingly random text in your code file.
  • Note that as keywords are reserved words, you cannot use them as variable names.

Python Keywords ๐Ÿ—๏ธ

False True and or

not break continue class

def if elif else

for while in is

None lambda return
Enter fullscreen mode Exit fullscreen mode

Description And Code Example

False & True
  • Data values from the data type Boolean
False == ( 1 > 2 )
True == ( 2 > 1 )
Enter fullscreen mode Exit fullscreen mode
and & or & not
  • Logical operators
  • (x and y) โ†’ both x and y must be True
  • (x or y) โ†’ either x or y must be True
  • ( not x) โ†’ x must be false
x, y = True , False

# AND

(x and y) == False ๐Ÿ‘‰ True

# OR

(x or y) == True  ๐Ÿ‘‰ True

# NOT

( not y) == True  ๐Ÿ‘‰ True 
Enter fullscreen mode Exit fullscreen mode
break
  • Ends loop prematurely
while ( True ):
    break # no infinite loop
print( "hello world" )

Enter fullscreen mode Exit fullscreen mode
continue
  • Finishes current loop iteration
while ( True ):
    continue
    print( "43" ) # dead code
Enter fullscreen mode Exit fullscreen mode
if & elif & else
  • Conditional program execution
  • program starts with โ€œifโ€ branch, tries โ€œelifโ€ branches, and finishes with โ€œelseโ€ branch (until one evaluates to True).
x = int(input( "your val: " ))

if x > 3 : 
    print( "Big" )
elif x == 3 :
    print( "Medium" )
else:
    print( "Small" )   
Enter fullscreen mode Exit fullscreen mode
def
  • A function is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a function.
  • A function can return data as a result.
# Void function

def my_function():
  print("Hello from a function")

my_function()


# Void function with parameters

def hello(first_name,last_name,age):
    print("hello",first_name ,last_name)
    print("your",age,"years old")
    print("nice to meet you")

hello("Bro","Code","21")


# Function with return & parameter & argument 

def my_function(x):
  return 5 * x

print(my_function(3))
Enter fullscreen mode Exit fullscreen mode
class
  • Defines a new class a real-world concept (object oriented programming) or OOP.
  • Defines a new function or class method.
  • For latter, first parameter self points to the class object.
  • When calling class method, first parameter is implicit.
class Car:

    # class attribute
    wheels = 4

    # initializer / instance attributes
    def __init__(self, color, style):
        self.color = color
        self.style = style

    # method 1
    def showDescription(self):
        print("This car is a", self.color, self.style)

    # method 2
    def changeColor(self, color):
        self.color = color

c = Car('Black', 'Sedan')

# call method 1
c.showDescription()
# Prints This car is a Black Sedan

# call method 2 and set color
c.changeColor('White')

c.showDescription()
# Prints This car is a White Sedan
Enter fullscreen mode Exit fullscreen mode
for
  • A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
Enter fullscreen mode Exit fullscreen mode
while loops
  • With the while loop we can execute a set of statements as long as a condition is true.
i = 1
while i < 6:
  print(i)
  i += 1
Enter fullscreen mode Exit fullscreen mode
in
  • Checks whether element is in sequence
42 in [ 2 , 39 , 42 ] ๐Ÿ‘‰ True
Enter fullscreen mode Exit fullscreen mode
is
  • Checks whether both elements point to the same object
y = x = 3

x is y          ๐Ÿ‘‰ True
[ 3 ] is [ 3 ]  ๐Ÿ‘‰ False
Enter fullscreen mode Exit fullscreen mode
None
  • The None keyword is used to define a null value, or no value at all.
  • None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None.
  • Empty value constant
x = None

if x:
  print("Do you think None is True?")
elif x is False:
  print ("Do you think None is False?")
else:
  print("None is not True, or False, None is just None...")e
Enter fullscreen mode Exit fullscreen mode
lambda
  • Function with no name (anonymous)
(lambda x: x + 3)(3)  ๐Ÿ‘‰ returns 6
Enter fullscreen mode Exit fullscreen mode
return
  • Terminates function execution and passes the execution flow to the caller.
  • An optional value after the return keyword specifies the result.
def incrementor (x) :
     return x + 1
incrementor( 4 ) ๐Ÿ‘‰ returns 5
Enter fullscreen mode Exit fullscreen mode

Connect with Me ๐Ÿ˜Š

๐Ÿ”— Links

linkedin

twitter

Top comments (0)