Python Conditions

  • Equals: x == y
  • Not Equals: x != y
  • Less than: x < y
  • Less than or equal to: x <= y
  • Greater than: x > y
  • Greater than or equal to: x >=y

Python Recursion

In python a function can call itself.

Use: Looping

To be avoided while using recursion:

  1. Writing a never ending program.
  2. Writing a program which uses excessive power.
  3. Writing a program which uses excess memory.

Example

def recursion(m):
if(m > 0):
result = m + tri_recursion(m – 1)
print(result)
else:
result = 0
return result

print(“\n\nRecursion Example Results”)
recursion(6)

Passing a List as an Argument

We can send any data type as argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

E.g. if we send a List as an argument, it will still be a List when it reaches the function:

def cricket_team(teams):
for x in teams:
print(x)

players = [“rohit”, “kohli”, “bumrah”]

cricket_team(players)

Default Parameter Value

If we call a function without argument, it calls default value.

def our_function(state = “Karnataka”):
print(“I am from “ + state)

our_function(“Maharashtra”)
our_function(“Kerala”)
our_function()
our_function(“Telangana”)

Keyword Arguments

Key, value pair arguments can be given to functions in python.

The order can be arbitrary.

def my_subjects(subject3, subject2, subject1):
print(“The first subject is “ + subject1)

my_subjects(subject1 = “English”, subject2 = “Kannada”, subject3 = “Hindi”)