Jovian
⭐️
Sign In

The for loop is used to iterate over a sequence and other iterable objects. The syntax for "For" loop in python is

for val in sequence:
statements

In [1]:
Languages = ["Python", "C", "R"]
for x in Languages:
  print(x)
Python C R

The while loop is used to iterate over a block of code as long as the condition is true. The syntax for while loop in python is

while test_expression:
statements

In [2]:
counter = 1
while counter < 6:
  print(counter)
  counter += 1
1 2 3 4 5

The if…else statement is for decision making.The syntax for if else statement in python is

if test expression:
statement(s)

In [13]:
a = 1
b = 2
if b > a:
  print("b is greater than a")
else :
  print("a is greater than b")
b is greater than a

Break statement terminates the current loop and resumes execution at the next statement.The break statement can be used in both while and for loops.In nested loops, the break statement stops the execution of the innermost loop and start executing the next line of code after the block.The syntax of break statement is

break

In [8]:
# Use of break statement inside for loop
for val in "string":
    if val == "i":
        break
    print(val)

print("Outside For Loop")
s t r Outside For Loop

Continue statement returns the control to the beginning of the while loop and rejects all the remaining statements in the current iteration of the loop.The continue statement can be used in both while and for loops.The syntax of continue statement is

continue

In [9]:
# Use of continue statement inside For loops

for val in "string":
    if val == "i":
        continue
    print(val)

print("Outside For Loop")
s t r n g Outside For Loop

A function is a block of organized, reusable code which is used to group related operations. Functions enables code modularity and reusability.The syntax of Function in python is

def function_name(parameters):
"""docstring"""
statement(s)

In [6]:
#Python Function to reverse a string
def reverse(s): 
  str = "" 
  for i in s: 
    str = i + str
  return str

reverse("WiDSDatathon")
Out[6]:
'nohtataDSDiW'

Lambdas, also known as anonymous functions, are small, restricted functions which is written in a single line and doesnt have a function name . The syntax to write lambda functions in python are

lambda p1, p2: expression

In [5]:
cube = lambda x : x * x * x
print("The cube of 5 is :",cube(5))
The cube of 5 is : 125