Advance Programming Slide 4
Advance Programming Slide 4
PROGRAMMING
PROF. GULRAIZ SATTAR
LAMBDA / ANONYMOUS
FUNCTION
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 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
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
Output:
def natural (n): 10
print (n) 9
natural (10) 8
7
6
5
4
3
2
DIRECT FUNCTION
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
var = 1,2,3,true,”zero”
print (len(var))
Output: 6
COPY
var = 1,2,3,true,”zain”
var2 = var
print (var2)
Output: 1,2,3,true,”zain”
ADD / CHANGE
var = 1,2,3,4,true,”zain”
var[1] = “zain”
print (var)
Output: 1,2,3,true,”zain”
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)
var = 1,2,3,true,”zain”
var.append (“Good”)
print (var)
Output: error
INDEX
var = (1,2,3,4,true,”zain”)
l = var.index = (“zain”)
print (l)
Output: 5
POP - DELETE
var = (1,2,3,4,true,”zain”)
l = var.pop()
print (l)
Output: No change
POP - DELETE
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
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