You are on page 1of 23

Operators in Python

 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Identity operators
 Membership operators
 Bitwise operators
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical operators
Identity Operators
Contd..

x = ["apple", "banana"]
y = ["apple", "banana"]
z=x

print(x is z)

# returns True because z is the same object as x

print(x is y)

# returns False because x is not the same object as y, even if they have the same content

print(x == y)

# to demonstrate the difference betweeen "is" and "==": this comparison returns True
because x is equal to y
Membership Operators
Contd..
x = ["apple", "banana"]

print("banana" in x)

# returns True because a sequence with the value "banana" is in the list

x = ["apple", "banana"]

print("pineapple" not in x)

# returns True because a sequence with the value "pineapple" is not in


the list
Bitwise operators
Functions in Python
What is a function?
 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.
 In python, functions are declared using the keyword def.
Contd..
Number of arguments
 By default, a function must be called with the correct
number of arguments. Meaning that if your function
expects 2 arguments, you have to call the function with 2
arguments, not more, and not less.
 Example:

def my_function(fname, lname):
  print(fname + " " + lname)

my_function("Emil")

It throws an error.
Arbitrary Arguments, *args

 If you do not know how many arguments that will be


passed into your function, add a * before the parameter
name in the function definition.
 This way the function will receive a tuple of arguments,
and can access the items accordingly.

def my_function(*students):
  print("The student who logged in late is " + students[2])

my_function(“Vaishak", “Anandhu", “Seema")
Keyword arguments
 You can also send arguments with the key = value syntax.
 This way the order of the arguments does not matter.

Example:
 def my_function(child3, child2, child1):
  print("The youngest child is " + child3)

my_function(child1 = "Emil", child2 = "Tobias", child3


= "Linus")
Arbitrary Keyword Arguments, **kwargs

 If you do not know how many keyword arguments that will be


passed into your function, add two asterisk: ** before the
parameter name in the function definition.
 This way the function will receive a dictionary of arguments,
and can access the items accordingly:
Example
 If the number of keyword arguments is unknown, add a
double ** before the parameter name:
 def my_function(**kid):
  print("His last name is " + kid["lname"])

my_function(fname = "Tobias", lname = "Refsnes")


Default Parameter Value

 If we call the function without argument, it uses the


default value:
Example
 def my_function(country = "No where"):
  print("I am from " + country)

my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
The pass Statement

 function definitions cannot be empty, but if you for some


reason have a function definition with no content, put in
the pass statement to avoid getting an error.
Example
 def myfunction():
  pass
Recursion
 Python also accepts function recursion, which means a defined function
can call itself.
 Example:

def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k - 1)
else:
result = 0
return result

answer = tri_recursion(6)
print("\n\nRecursion Example Result",answer)
Finding addition of 2 numbers
def calculate_sum(data1, data2):
#All the statements in the block of code must have the
same level of indentation
result_sum=data1+data2
return result_sum
result=calculate_sum(10,20)
print(result)
Practise
 Read 2 integers and return their sum if product is greater
than 1000 else return their product itself.
 Take a list of numbers, return True if first and last number
of list is same.
 Reverse a given number and return True if it same as
original number.
 Given a string and an integer number n, remove characters
from string starting from zero upto n and return a new
string.
Contd..
 Write a function to extract each digit from an integer in
reverse order.
 Print Multiplication table from 1 to 10.
 Given a list of numbers, iterate it and print only those
numbers which are divisible by 5.

You might also like