0% found this document useful (0 votes)
20 views48 pages

Advance Programming Slide 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views48 pages

Advance Programming Slide 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

ADVANCE

PROGRAMMING
PROF. GULRAIZ SATTAR
LAMBDA / ANONYMOUS
FUNCTION

 A function without name is called lambda function or anonymous function.


These functions are defined by using lambda keyword.
 Syntax:
lambda avg_list: expression
CODE EXAMPLE 1

 Lambda x: print(x) a formal function


numbers = [11,20,31,40,51,60,71,80,91,100]
evenn = list(filter(lambda:x==0 and x>50, numbers))
print(“Even numbers” greater than 50:”, evenn)
Output: 20,40,60,80,100
CODE EXAMPLE 2

city = [‘Lahore’, ’Kasur’, ’Multan’, ’Okara’]


def length ():
return length(city)
sort = sorted(city, key=length)
print(“sorted words by length:”, sort)
Output: Okara, Kasur, Multan, Lahore
RECURSION

 When a function calls itself, then that function is called a recursive function
and the process is called recursion.
 def demo ():
print (“Hello”)
demo ()
Output: Hello
CODE EXAMPLE 1

 def demo ():


print (“Hello”)
demo ()
demo ()
Output: Hello
.
.
Hello
CODE EXAMPLE 2

 def sum_of_natural_numbers(n):
if n == 1:
return 1
else:
return n + sum_of_natural_numbers(n - 1)
result = sum_of_natural_numbers(5)
print(f"Sum of natural numbers up to 5 is: {result}")
Output: Sum of natural numbers up to 5 is: 15
LIMIT OF RECURSION

 The limits of recursion primarily relate to the depth of recursion, memory


usage, and performance.
 def demo ():
print (“Hello”)
demo ()
Output: Hello
LIMIT OF RECURSION

 imp sys  Output:


print (sys.get recursion limit() ) 196
sys. setrecursionlimit(200))x 996
print (sys.getrecursionlimit())
i=0
def demo():
global I
i=i+1
print (“Hello”, i)
dem ()
demo()
ADVANTAGES & DISADVANTAGES

 Advantages:  Disadvantages:
• Simplified Code • Difficult to Debug
• Breaks Complex Problem • High Memory Usage
• Use of Recursive Data Structures • Performance Overhead
• Eliminates Loop Constructs • Inefficiency without Memoization
• Memory Usage for Tail Recursion • Not Always Intuitive
DIRECT FUNCTION

 A direct function refers to a regular function that performs its task without
calling itself, meaning it is not recursive.
 In other words, a direct function is one that uses a straightforward approach
to solve a problem, typically through iterative or procedural steps, instead of
dividing the problem into smaller sub-problems as done in recursion.
CODE EXAMPLE 1

 n = int (input(“Enter the value of n:”)) Output:


def natural (n): 5
if n == 0: 4
return 3
print (n) 2
return natural (n - 1) 1
natural (n)
CODE EXAMPLE 2

Output:
 def natural (n): 10
print (n) 9
natural (10) 8
7
6
5
4
3
2
DIRECT FUNCTION

 An indirect function refers to a function that is called or used indirectly


rather than being invoked directly by its name.
 This concept can apply in various contexts, particularly in programming and
mathematical functions.
CODE EXAMPLE 1

 def num (n): Output:


if n <= 0: 109876543210
return
print (n, end=“”)
num1 (n - 1)
def num1 (n):
print (n, end=“”)
num (n - 1)
num1 (10)
TUPLE

 A tuple is an immutable (unchangeable) ordered collection of items.


 Once a tuple is created, you cannot modify, add, or remove elements from it.
 Tuples are similar to lists, but unlike lists, they use parentheses () instead of
square brackets [].
 Syntax: tuple-home=(item, item 0…item n)
KEY CHRACTERISTICS OF TUPLE

 Immutable:
Cannot be changed after creation.
 Ordered:
Elements are stored in a specific order, and this order remains unchanged.
 Allows duplicates:
Tuples can contain the same element more than once.
 Heterogeneous:
Can store elements of different data types (integers, strings, etc.).
TUPLE

 We write the items of tuple inside paranthesis “()” and each item is
separated by comma.
 Duplicates are allowed,
 Immuted in nature.
 var=(1,2,3,4,5,true,‘Asif’)
print(var)
Output: (1,2,3,4,5,true,Asif)
TYPE

 var = 1, “Asif”
print (type(var))
Output: Tuple
 var = 1
print (type(var))
Output: Integer
EXAMPLES OF TUPLE

 var = ()
print (type(var))
Output: Tuple

 var = (1,2,3,4,5)
print (var)
Output: 1,2,3,4,5
EXAMPLES OF TUPLE

 var = (1,2,3,true,”string”)
print (var)
Output: 1,2,3,true,string

 var = (1,2,3,4,5)
print (var)
Output: 1,2,3,4,5
LENGTH

 Length refers to the number of elements in a collection, such as a list, tuple,


string, dictionary, or set.

 var = 1,2,3,true,”zero”
print (len(var))
Output: 6
COPY

 Copy refers to creating a duplicate of an object.

 var = 1,2,3,true,”zain”
var2 = var
print (var2)
Output: 1,2,3,true,”zain”
ADD / CHANGE

 Add and change refer to modifying a collection by either inserting new


elements (adding) or updating existing ones (changing).

 var = 1,2,3,4,true,”zain”
var[1] = “zain”
print (var)
Output: 1,2,3,true,”zain”

 Tuple does not support item assignment.


INDEXING

 Indexing in Python refers to accessing elements of a sequence (like a list,


string, tuple, etc.) using an index number.

 The index represents the position of the element in the collection, starting
from 0 for the first element.
EXAMPLE

 var = 1,2,3,true,”zain”
print (var[4])
Output: true
APPEND OPERATION

 The append operation is used to add a single element to the end of a list. It
modifies the original list in place and doesn't return a new list.

 This operation is commonly used when you want to grow a list by adding
more elements one at a time

 Syntax: list_name.append(element)

 We cannot perform this operation in tuple.


EXAMPLE

 var = 1,2,3,true,”zain”
var.append (“Good”)
print (var)
Output: error
INDEX

 Index refers to the position of an element within a sequence, such as a list,


tuple, or string.

 It helps you locate or access specific elements by their position in the


sequence.
EXAMPLE

 var = (1,2,3,4,true,”zain”)
l = var.index = (“zain”)
print (l)
Output: 5
POP - DELETE

 In tuple, we cannot perform delete operation.

 var = (1,2,3,4,true,”zain”)
l = var.pop()
print (l)
Output: No change
POP - DELETE

 In tuple, we cannot perform delete operation.

 var = (1,2,3,4,true,”zain”)
l = var.pop
print (l)
Output: error
LIST

 The data type list is an ordered sequence that is muted and made up one or
more elements.
 A list can have elements of different data types, such as integer, float, string,
tuple or even another list.
 A list is very useful to group together elements of invited data types.
 Elements of a list are enclosed in sequence brackets and are separated by a
comma list.
 Duplicates are allowed.
EXAMPLE

 List 1 is an example of mixed data types:


list1 = [20,55,’Hello’]
print(list1)
or
list1
Output: [20,55,’Hello’]
EXAMPLE

 List 2 is an example of nested list:


list2 = [[‘Asif’,2000],[‘Haris’,2001],[‘Ali’,2002]]
print(list2)
or
list2
Output: [[‘Asif’,2000],[‘Haris’,2001],[‘Ali’,2002]]
ACCESSING ELEMENTS FROM
LIST

 Accessing elements from the list:


list1 = [20,5.5,’Hello’]
list1[0]… return first element of list 1
Output: 20
list2 = [20,5.5,’Hello’]
list1[2]… print (list 2[2])
Output: Hello
LIST SELECTING

 10 – integer
‘Asif’ – string
True – bolean
10.5 – fractional value
 list2 = [10,’Asif’,True,10.6,Haris,10]
print (list2[1:4])
Output: [‘Asif’, True, 10.6]
NUMBER COUNT

 list2 = [10,’Asif’,True,10.6,Haris,10]
print (list2.count(“Asif”))
Output: 1

 list2 = [10,’Asif’,True,10.6,Haris,10]
print (list2.count(10))
Output: 2
INDEX

 list2 = [10,’Asif’,True,10.6,Haris,10]
print (list2.index(10.6))
Output: 3

 list2 = [10,’Asif’,True,10.6,Haris,10]
print (list2.count(10))
Output: 0,5
INSERT

 list2 = [10,’Asif’,True,10.6,Haris,10]
list2.insert(2,”coding”)
print (list2)
Output: 10,’Asif’,’coding’,True,10.6,Haris,10
POP

 list2 = [10,’Asif’,True,10.6,Haris,10]
list2.pop(2)
print (list2)
Output: 10,’Asif’,10.6,Haris,10
EXTEND

 list2 = [10,’Asif’,True,10.6,Haris,10]
list3 = [‘Asif’,‘Haris’]
list2.extend(list3)
print (list2)
Output: [10, ‘Asif’, True, 10.6, ‘Haris’, 10, 'Asif', ‘Haris’]
COPY

 list2 = [10,’Asif’,True,10.6,Haris,10]
list2.copy(list3)
print (list3)
Output: [10, ‘Asif’, True, 10.6, ‘Haris’, 10]
SORT

 list2 = [10,30,34,1,0]
list2.sort() or list2.sort(reverse=true)
print (list2)
Output: [0,1,10,30,34]
 list2 = [10,30,34,1,0]
list2.reverse()
print (list2)
Output: [0,1,10,30,34]
NESTED LIST

 list2 = [10,30,34,1,0,(“Asif”,“Haris”)]
print (list2)
Output: [10,30,34,1,0,(“Asif”,“Haris”)]
LIST COMPREHENSIVE

 list2 = [“Asif”, “Ali”, “Ahmed”, “Hassan”]


a=[word for word in list2 if word.startswitch(“a”)]
print (a)
Output: ['Asif', 'Ali', 'Ahmed']
LIST UNPACKING

 list2 = [“Asif”, “Ali”]


n1,n2 = list2
print (n1)
print(n2)
Output: ['Asif', 'Ali']
THANK
YOU

You might also like