UNIT 3 Flashcards
Recursion and how to exit the function
recursion is implemented using _________ , ISA example of recursion
a function calls itself to solve smaller instances of the same problem. This process continues until it reaches a stopping condition known as the baimporse case.
Base Case: the simplest subproblem can be the problem can be reduced to
Recursive case: part where function calls itself to solve a simpler program
recursive step: performs repeating action until the subproblem cant be broken down anymore
it breaks down the prob ex factorial if input is 5 is goes in reverse until base condition and finds 0! then
uses the value returned and then adds it from ex 0 or other functions like 1!… till 4!*5 =120, possible to iterate each value (Fibonacci series)
if value is added outside the recurring function it adds that value to lets say the returned value of 0 or u can directly input function values it will start from first and go to end
calling the function inside the function until it repeatedly until it reaches base case, else goes into an infinite loop
where is it use:
Solving problems with repeated patterns
simplify complex problems into smaller, easier problems
Handling unknown levels of depth (number of steps to a problem) to exit a function u can press return
using stack because activation records are
to be stored in LIFO order (last in first out).
activation record: arguments, return
address and local variables of the function
Stack is a linear data structure in which elements are inserted to the bottom and deleted from the top.
STACK MEMORY ALLOCATION:
contiguous blocks of memory (Function call Stack)
size of memory known to compiler, and memory is allocated when function is called
local memory such as variable intil. inside functions are stored as temp on function call stack and deleted once function returns
allocation of memory is handled by compiler using predefined routines
example of recursion:
def fact(n): #Recursive Function
if n == 0 : #BASE CASE
res = 1
else:
res = n * fact(n - 1)
return res
Recursion V Iteration
Recursion
Function calls itself
Implemented using Function calls
Termination condition is defined within
the recursive function
Leads to infinite recursion, if does not
meet termination condition
It is slower than iteration
Uses more memory than iteration
ITERATION
Set of program statements executed
repeatedly
Implemented using Loops
Termination condition is defined in the
definition of the loop
Leads to infinite loop, if the condition in
the loop never becomes false
It is faster than recursion
Uses less memory compared to recursion
callback function, and code example adv and dis and built in function, and calling multiple functions
a function passed to another function as an argument
Calling function (outer function) can call the callback function as many times as required
* Calling function can pass appropriate parameters according to the task to the called functions. This allows information hiding.
* Improves code modularity and reusability
* Allows you to dynamically change the working of a function without changing its core implementation
example code
def add(a,b):
print(a+b)
def mul(a,b):
print(a+b)
def divide(a,b)
print(a/b)
def compute(func, a,b)
func(a,b)
compute(add, 5,3)
the func can be any name
function is called inside an other function and its calculation is done
EXAMPLE built in function= li.sort(key=len)
MULTIPLE FUNCTIONS CALLED ONCE USING LIST
similar to function of decorator
def function(func_list, x, y):
print(“Inside function”)
for func in func_list:
def add(x,y):
z = x+y
print(‘Sum =’,z)
def divide(x,y):
z = x/y
print(‘Quotient =’,z)
cb_list=[add, divide]
function(cb_list, 10, 5)
the func is called into function and then for loop is used to go throu all the functions, and then cb_list is used to add all the funcs into a list and then call the function which has all the funcs with values
what is closure with example
a nested function which has access to a free variable from an enclosing function that has finished its execution.
a nested function which
inner function is defined inside another function,
the inner function has access to variables and of the outer function
- It has access to a free variable in outer scope
- It is returned from the enclosing function
- A free variable - a variable that is not bound in the local scope.
Closures with immutable variables such as numbers and strings - use the nonlocal keyword.
The outer function returns the inner function
even if u del the outer fun u can still do the same function using the inner function
python example:
def outer(msg): #outer function
def inner(): # This is the nested function
print(msg) msg=free var
return inner # returns the nested function
another example:
def division(y): #outer function
def divide(x): #inner function
return x/y
return divide
Output:
10.0
32.0
return divide
d1=division(2) #refers to divide
d2=division(3) #refers to divide
print(d1(20))
print(d2(96))
free var is accessed by outer hence thats used
THUS,
IMP: ID of both outer function and inner function is the same
Not all nested functions are closures
for it to be closure:
The inner function has access to the non-local variables or local
variables of the outer function.
2. The outer function must return the inner function.
NON LOCAL VARIABLE
def f1(): #outer function
x=0
def f2(): #inner function
nonlocal x # x - that belongs to scope of outer function is made non-local
x=x+1
return x
return f2
when when var is not declared on the outside variable, u have to use non local, to describe the var so that its non local and can be used in the outer function
Decorators
first @greet called=>say=>@bye=>say
a function allows to modify the behavior of function or class. without modifying the base function/source code or When we need to run the same code on multiple functions. This avoids writing duplicate code.
functions are argument into another function and then called inside the wrapper function.
multiple decorators
def greet(func):
def wrapper():
print(“Hello!”)
func()
return wrapper
def bye(func):
def wrapper():
func()
print(“Goodbye!”)
return wrapper
@greet
@bye
def say():
print(“hi.”)
say()
OR
d=greet(bye(say))
d()
OUTPUT:
Hello!
hi.
Goodbye!
example code
def bono(func):
def wrapper():
return “ok lewis close that gap: “ + func()
def ham():
return “its hammer time”
ham = bono(ham)
print(ham())
Normal code without @:
def sch(f):
def wrapper():
print(“sch:7”)
print(“ham:7”)
f() # Call def (ver)
return wrapper
def ver():
print(“ver:4”)
v = sch(ver)
v()
OUTPUT
sch:7
ham:7
ver:4
what is a generator
Code to check above generator function
is a function that returns an iterator that produces a sequence of values when iterated over
Iterator - an object that can be iterated upon, like a list
it returns an iterator object with a sequence of values.
A yield statement is lazy
When the generator function is called, it does not execute the function body immediately. Instead, it returns a generator object that can be iterated over to produce the values. Pauses exceution value can be acessed later
Generator functions return a generator object that is iterable (used as an iterator).
Generator objects are accessed
FOR LOOP
def square(n):
for i in range(1,n+1):
yield i**2
for i in square(5):
print(i)
CALLING THE NEXT METHOD(same code)
#Create a generator
gen = square(5)
print(next(gen)) # 1
print(next(gen)) # 4
print(square) =>
0x7f43eda29170>
Generator expression aka for loops and pipeline
Generator function - square
- is another way of writing the generator function.
*Similar to list comprehension technique but instead instead of storing the elements in a list in memory, it creates generator objects. - Syntax:
for element in iterable)
allows for the declaration of a function that behaves like an iterator, making it a faster, cleaner ,easier way to create an iterator.
* are useful to produce a large sequence of values, but don’t want to store all of them in memory at once.
* The simplification of code is a result of generator function and generator expression support provided by Python.
EXAMPLE CODE
generator_exp=(i**2 for i in range(5) if i%2==0)
for i in generator_exp:
print(i)
Pipe line Generators:
Multiple generators can be used to pipeline a series of operations
Generator function - fibonacci_numbers
def fibonacci_numbers(nums):
x,y=0,1
for i in range(nums):
x,y=y,x+y
yield x
def square(nums):
for num in nums:
yield num**2
print(sum(square(fibonacci_numbers(3))))
here
yield vs return
.Discuss the advantages of using generators over lists in terms of memory efficiency.
Generators are memory-efficient because they produce values on-the-fly and dont store the
entire sequence in memory.
YIELD
Returns a value and pauses the execution
while maintaining the internal states
Used to convert a regular Python function into a generator
Used when the generator returns an
intermediate result to the caller
Code written after yield statement
execute in next function call
It can run multiple times
RETURN
Returns a value and terminates the
execution of the function
Used to return the result to the caller statement
Used when a function is ready to send a
value
Code written after return statement
won’t execute
It only runs a single time
We can’t include return inside a generator function. If we do, it will terminate the
function.
what are GUI and tkinter
syntax GUI
visual method of interacting with software applications.
syntax of GUI
import tkinter
root= tkinter.Tk() #
#creates a new window
root.title(“ “)
root.geometry(‘ ‘) #dimensions
root.mainloop()
#loops continuously until we close the window
mainloop()
* A function that continuously loops and displays the window till we
close it or an action closes the window.
until the user exits
can track the movements of the mouse on the window because it
constantly loops
widgets and basic syntax
Elements in windows
Each separate widget is a Python object.
parent as a parameter
- The ‘root’ does not have a parent.
To display it:
pack() - packs widgets in rows or columns (stacking layout)
2. grid() - position widgets using rows and column table like layout
3. place() - set the position and size of a window, in absolute terms, or relative to another window. can place anywhere unlike pack() and grid()
Button=> press
cavnas —> shapes and import pics
Checkbutton—> number of options as checkboxes
Entry—-> single-line text field that accepts values from the user
Frame —> To group and organize other widgets
Label —> a single-line caption, can contain images also.
Listbox —> a list of options
Menu —> all kinds of menus required in the application
Menubutton —> display the menu items to the user
Spinbox->“entry” widget in which value can be input just by selecting a fixed value of numbers
Panned window: A container widget that is mainly used to handle different panes
button
b=Button(parent,options)
from tkinter import *
win =Tk() => button syntax
win.title(“TITLE”)
win.geometry(‘300x200’)
b=Button(win, text=’Submit’)
b.pack()
=> adds the button to the window
win.mainloop()
BUTTON FUNCTIONS
b=Button(win, text=’‘,command=….)
pady = height of button
activeforeground=”yellow”
#color of text when clicked
activebackground=”red”
#color of button when u clicked
pack(side=LEFT)
direction of side, only for left and right
command=func name
when u click action can happen due to func
CANVAS
W=Canvas(parent,option
=value)
cv=Canvas(win, bg = “red”, height =”300”)
cv.pack()
win.mainloop()
creating arcs:
import tkinter
import tkinter as tk
from tkinter import*
root = tkinter.Tk()
root.title(“Arc Example”)
cv = Canvas(root, width=400, height=400, bg=”white”)
cv.pack()
coord = 10, 10, 300, 300
arc1 = cv.create_arc(coord, start=0, extent=150, fill=”pink”)
arc2 = cv.create_arc(coord, start=150, extent=215, fill=”green”)
root.mainloop()
IMPORT PICTURE
from tkinter import Tk
from tkinter import Canvas, PhotoImage
import tkinter
from tkinter import Canvas, PhotoImage
run=tkinter.Tk()
canvas=Canvas(run,height=300,width=300)
canvas.pack()
file=PhotoImage(file=”az.png”)
canvas.create_image(20,20, anchor=”nw”,image=file)
canvas.image = file
run.mainloop()
coordinates (20, 20),
anchor=NW: top-left corner be anchored at the (20, 20). NW =>”North-West” corner (top-left).
image=filename: specifies the image
functions of checkbutton, and radio button
W = Checkbutton(parent,option=value)
from tkinter import *
cb1=Checkbutton(win,text=”Yes”, variable=1)
cb1.pack()
USING IntVar()=># to store the state as 1,0
import tkinter
from tkinter import*
def printt():
print(k.get())
#.get(): Retrieves the current value stored in the check box
root=tkinter.Tk()
k=IntVar()
c=Checkbutton(root,text=”press me”,variable=k,)
c.pack()
b=Button(root, text=”helo thrr”,command=printt)
b.pack()
root.mainloop()
onvalue= can be int or string use
String(var) => to store the state as as string 1,0
offvalue=same thing
normal OUT=bool
import tkinter as tk
def pri():
boolean_value = k.get() == “1”
print(boolean_value)
print(k.get())
root = tk.Tk()
k = tk.StringVar()
checkbox = tk.Checkbutton(root, text=”Check me”, variable=k, onvalue=”1”, offvalue=”0”)
checkbox.pack()
button = tk.Button(root, text=”Print Value”, command=pri)
button.pack()
root.mainloop()
RADIO BUTTON:
allows u to click only one option
Radiobutton(funcname, option)
1)deselect() – turn off the checkbutton
2) flash(): flashed between the active and normal colors.
3) invoke(): invoke the method associated with the checkbutton.
4) select(): to turn on checkbutton.
5) toggle(): to toggle btw the diff Check buttons.
syntax and functions of entry widgets
tkinter message box dialog
syntax for message box
W = Entry(parent,options)
1. get() – Returns the entry’s current text as a string
2. delete(): Deletes characters from the widget
3. insert(index,name): Inserts string ‘name’ before the character at the given index
e2 = Entry(f1, width=20, show=”*”) field
e2.place(x=100, y=90)
*show function displays the what entry box text should be shown as
FOR PASSWORD EX
result.config(text=”Login Success!”, fg=”green”)
.config used to update the config of widget ex Entry to button in this case
syntax for custom message when button is pressed after checkbox is selected
chk1=StringVar()
def clk():
print(chk1.get())
variable=chk1
this FUNCTION clk(), is called in button
b1=Button(win,text=”check”,command=clk)
Vars to hold the state of the checkbuttons
ckb1_var = tk.IntVar()