Continuation of Basics of Python

SHARMITHA AR
4 min readJan 28, 2024

--

What is Operator?

Ans: An operator is used to perform operations between two values or variables

Different type of Operators in Python

  1. Arithmetic Operator
  2. Assignment Operator
  3. Comparison Operator (Relational Operator)
  4. Logical Operator
  5. Membership Operator
  6. Identity Operator
  7. Bitwise Operator

Arithmetic Operator: Arithmetic operators are used to perform arithmetic operations between variables

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)
  5. Modulus (%)
  6. Exponentiation (**)
  7. Floor Division(//)

Comparison Operators [Relationship Operator]

Comparison Operators are used to compare two values and Output of Comparison operator is always an Boolean value

Type of Comparison Operators

  1. Equal-to (==)
  2. Not Equal-to (!=)
  3. Greater Than (>)
  4. Less Than (<)
  5. Greater Than or Equal-to (>=)
  6. Less Than or Equal-to (<=)

Logical Operators

Logical Operators are used to combine conditional statements Will first understand what is conditional statements before going to logical operators.
Type of Conditional Statement in Python: Basically we have 3 Conditional Statements in Python

  1. if
  2. elif
  3. else

if True:
print(‘we are doing great’)
else:
print(‘worst’)

we are doing great

x = 20
y = 25

# Conditional Statement
if x == y:
print(‘Equal’)
elif x > y:
print(‘Greater’)
else:
print(‘Smaller’)

Smaller

Type of Logical Operators:

  1. and (Logical and)
  2. or (Logical or)
  3. not (Logical not)

Identity Operators

Identity operators are used to compare objects

  1. is → returns True if both variables are same object
  2. is not — -> returns Trueif both variables are not same object

list1 = [10,20,30]
list2 = [10,20,30]
list3 = list1
# Is operator # It prints True because it is storing in same memory location
list3 is list1

True

# Now will check for memory location
print(‘Memory Location for list1 =’,id(list1))
print(‘Memory Location for list2 =’,id(list2))
print(‘Memory Location for list3 =’,id(list3))

if id(list1) == id(list2):
print(‘List1 mand list2 store in same memory location’)
elif id(list2) == id(list3):
print(‘List2 and list3 store in same memory location’)
elif id(list1) == id(list3):
print(‘List1 and list3 store in same memory location’)

Memory Location for list1 = 1728733038016
Memory Location for list2 = 1728733030912
Memory Location for list3 = 1728733038016
List1 and list3 store in same memory location

Membership Operators

Membership Operators are used to check if the certain value is present in a list

  1. in
  2. not in

list1 = [2,4,23,5]
a=19
a in list1 # it will check if 19 is present in list1 or not if it is not present it return False else it prints True

False

Bitwise Operators

Bitwise operators are used to compare binary numbers

Type of Bitwise Operators

  1. Bitwise AND (&) — Sets each bit to 1 if both bits are 1
  2. Bitwise OR (|) — Sets each bit to 1 if one of the bits is 1
  3. Bitwise XOR (^) — Sets each bit to 1 if only one of the bits is 1
  4. Bitwise NOT (~) — Inverts all bits
  5. Left Shift (<<) — Shift left by pushing in zeros from the right and let the leftmost bits fell off
  6. Right Shift (>>) — shift right by pushing copies of the leftmost bit by adding zero

# Example for bit wise and(&) operator
print(bin(10))
print(bin(12))

0b1010
0b1100

What is Functions

A function is a block of code which only runs when it is called.

Why do we use function

We use function to Re-usability of code, and Minimizes redundancy (repeating the same code again and again)

Steps involved in a function

  1. You will be defining a function
  2. You will be calling a function

Type of function

  1. Built in function
  2. user defined function

def greet():
print(‘welcome’)
greet()

welcome

Types of Arguments in Python

  1. Formal Arguments
  2. Actual Arguments
  3. Position Argument
  4. Keyword Argument
  5. Default Argument
  6. Variable length Argument
  7. Keyword variable length argument

Example:

# Default Argument
def info(name, age=18):
print(‘Name =’,name)
print(‘Age =’,age)
info(‘Salman’)
info(‘Jagruti’,23)

Name = Salman
Age = 18
Name = Jagruti
Age = 23

what is Lambda Function

A lambda function is an anonymous function (i.e., defined without a name) that can take any number of arguments but, unlike normal functions, evaluates and returns only one expression.

example1:

# Add 10 to argument a, and return the result:
x = lambda a : a + 10
print(x(5))

example2:

name=’kiran’
upper_case=lambda name:name
print(name. upper())

KIRAN

Why Use Lambda Functions?

The power of lambda is better shown when you use them as an anonymous function inside another function. Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number:

def myfunc(n):
return lambda a:a*n
mydoubler = myfunc(2)
mydoubler(11)

22

Now will Perform Map in Lambda Function

Map in Python is a function that works as an iterator to return a result after applying a function to every item of an iterable

Syntax

map(func,*iterables) -> * varaiable number of elements iteration

# Now will square the number using lambda function
num = [2,4,6,8,10]
square_output = list(map(lambda x:x**2,num))
square_output

[4, 16, 36, 64, 100]

--

--