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
Description And Code Example
False & True
Data values from the data type Boolean
False == ( 1 > 2 )
True == ( 2 > 1 )
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
break
Ends loop prematurely
while ( True ):
break # no infinite loop
print( "hello world" )
continue
Finishes current loop iteration
while ( True ):
continue
print( "43" ) # dead code
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" )
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))
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
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)
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
in
Checks whether element is
in sequence
42 in [ 2 , 39 , 42 ] ๐ True
is
Checks whether both
elements point to the same
object
y = x = 3
x is y ๐ True
[ 3 ] is [ 3 ] ๐ False
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
lambda
Function with no name (anonymous)
(lambda x: x + 3)(3) ๐ returns 6
return
Terminates function execution and passes the execution flow to the caller.
An optional value after the return keyword specifies the
result.
Top comments (0)