UNIT 3 Flashcards

1
Q

Recursion and how to exit the function

recursion is implemented using _________ , ISA example of recursion

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Recursion V Iteration

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

callback function, and code example adv and dis and built in function, and calling multiple functions

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is closure with example

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Decorators

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is a generator

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Generator expression aka for loops and pipeline

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

yield vs return

.Discuss the advantages of using generators over lists in terms of memory efficiency.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what are GUI and tkinter
syntax GUI

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

widgets and basic syntax

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

button

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

CANVAS

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

functions of checkbutton, and radio button

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

syntax and functions of entry widgets

tkinter message box dialog
syntax for message box

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

syntax for custom message when button is pressed after checkbox is selected

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

combos(drop down list)

A

combo=Combobox(win)
combo[‘values’]=(1,2,3,4,5,’Text’)
combo.current(1)
combo.pack()

17
Q

LABEL WIDGET

A

ENTRY
Entry(parent,options)

to enter or display single line of text

=Label(parent, value = “”).place(x = 30,y = 50 )

.place = position of where i should be

18
Q

message box

A

messagebox.funcname
(Title, Message, [,options] )
from tkinter import messagebox

showinfo() important information

showwarning() some type of warning

showerror() error message

askquestion() a dialog box that asks a question with two options: YES or NO

askokcancel() a dialog box that asks a question with two
options: OK or CANCEL

askretrycancel() asks a question with two
options: RETRY or CANCEL

askyesnocancel() a question with three
options: YES or NO or CANCEL

from tkinter import messagebox

messagebox.showinfo(“Warning”,”why!”)

19
Q

Frames

A

A frame - rectangular region on the screen.
* Used to implement complex widgets.
* Organize a group of widgets.

W = frame(parent,options)

frame.pack() #frame in the default location (top- center).

fram.pack(side =BOTTOM)
or (side=”bottom”)
#for bottom frame
relative to the main frame

dimensions of FRAMES
fram=Frame(win,bg=
“black”,width=500,height=300)

NESTED FRAME
from tkinter import *
root = Tk()

Outer frame
f1 = Frame(root, height=420, width=420, bg=”black”)
f1.pack()

Nested frame inside f1
f2 = Frame(f1, height=269, width=269, bg=”gray”)
f2.pack(pady=20, padx=20)
root.mainloop()

20
Q

Module

A

IMPORT MECHANISM

Source Code Compilation:

When module is imported, Py checks if there is a .pyc (compiled) file in the __pycache__ DIR.
else, it compiles the source code (.py file) into bytecode.

Byte Code Storage:
stored in the __pycache__ directory.
can be reused on different systems.

Subsequent Imports:

a. If the source code remains unchanged, it uses the existing .pyc file for subsequent imports, skipping compilationstep for improved performance.

b. If the source code is changed, .pyc must be created again, and __pycache__ directory will be updated.

when u import a py file 1st time and 2nd time why no output?
.pyc is the recent in pycache. even if we modify the file, wont work as it creates the .pyc in the same interpreter session. doesn’t check the time stamp of module.
use reload()

restart interpreter session.

checks the timestamp of .pyc file. If timestamp is recent, it builds the .pyc and runs again. Else, simply runs again.

SYNTAX:
import importlib
import new#new=filename
importlib.reload(new)

21
Q

WAYS OF IMPORTING MODULES

A

in CMD
import FILENAME
dir() to see module name
in symbol table

DOT OPERATOR
allows you to access functions, variables, or classes defined in a module.
ex:
import filename
filename.func()
filename.a #for vars

USING ALIAS
import module1 as m
m.f1()
print(“value is”,m.a)
m.a=m.a+2 #allowed
print(“value is”,m.a)
here identifier name added directly into symbol table

from new import func/var

> > > from new import func,lok
func()
in gae
lok()
lolol

dir() =>func names, not module name!

USING import*, identifiers with underscores_ (are not IMPORTED!)

from new import *
»> func()
im gae

when funcs are imported, u must call them

22
Q

BUILT in FUCTIONS IMPORT

A

Usage of built-in module-
to print current working directory
import os
cwd=os.getcwd()
print(“CWD:”, cwd)

USING BUILTIN functions module os to list the files and directories

import os
path = “/”
dir_list= os.listdir(path)
print(dir_list)

23
Q

sys.path

A

module in directory MyLib

When we import modules, python interpreter locates from
⮚ directory from which the program is getting executed.
⮚ directory specified in the PYTHON PATH variable
(default location where PY searches)
⮚ default directory

sys.path
a list of interpreter’s search path for modules.
When import a module, Py looks for it in directories listed in sys.path.

list can be modified during runtime.
to modify: append and insert.

depends on the order, will check module after the other modules in the line

import sys
print(“Original sys.path:”, sys.path)
#sys.path:contains the directories where the interpreter looks for modules.
sys.path.append(“/path/to/new/directory”)
#adds the specified directory
print(“Modified sys.path:”, sys.path)

append:
adds a directory to the end of the sys.path list.

INSERT:
can add a dir at a specific position in the sys.path list.
takes: The index and dir

import sys
print(“Original sys.path:”, sys.path)
new_dir = “/path/to/new/directory”
sys.path.insert(0, ‘new_dir’)
print(“Modified sys.path:”, sys.path)

24
Q

user defined module
__name__
__doc__
__dict__

A

Special Vars/attributes
__name__

inbuilt global var:
has name of the module. the var = main if you run the code in script mode. Else if imported return name of module without py

SYNTAX:
filename.__name__

name attribute is set to main on the interpreter
»>_name
‘__ main__’

can be if statements like
if __name__==’__main__’

__doc__
anything can be documented using Docstrings.

accessed using the __doc__

def add():
‘'’Performing addition of two numbers.’’’

print(add.__doc__)

OUTPUT
Performing addition of two numbers.
Using help

__dict__
an object’s data. keeps track of all the info (attributes) about an object and their values, in the form of key-value pairs, where:

class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed

Create an object
my_dog = Dog(“Buddy”, “Golden Retriever”)

View attributes
print(my_dog.__dict__) # {‘name’: ‘Buddy’, ‘breed’: ‘Golden Retriever’}

Add a new attribute
my_dog.__dict__[‘age’] = 3
print(my_dog.age) # 3

Modify an attribute
my_dog.__dict__[‘name’] = “Charlie”
print(my_dog.name) # Charlie

25
Q

Testing-Pytest

A

a robust testing framework

to test codes

from simple unit to complex functional tests.

framework=> set of tools and libraries designed to streamline development

FEATURES
no need of API
2. useful plugins
3. Can be written as a function or method
4. Gives useful failure info without debuggers
5. Can be used to run doc tests and unit tests

-to use pytest = put pytest -in cmd
-put dir of file
-pytest filename.py

Passed test: test ran sucessfully, no errors

Failed Test: Test did not pass

Warnings: Non-critical issues, such as deprecations, bad practices. wont case test to fail

assertion expression tested for truth else=> AssertionError

26
Q

doctest

A

module in py

easy generation of tests

tests your code by running examples embedded in the docstrings and checking if the outputs = expected results.

finds patterns in docstring.
* Docstrings - description of a class/function to provide better understanding of code. Used for testing using
doctest

Need for Doctest
* To check a module’s docstrings are up-to-date (to ensure code still
work as documented).
* To perform regression testing (verifying the changes made to the code

will not impact the existing functionalities of the Software).
CODE
import doctest

def fact(n):
“"”Returns the factorial of a number n.

>>> fact(5)
120
>>> fact(0)
1
>>> fact(1)
1
"""
if n == 0 or n == 1:
    return 1
else:
    return n * fact(n - 1)

Call the testmod func to run the docstring tests
doctest.testmod(name=’fact’, verbose=True)

if verbose= False(default) output= in case of failure only.

python filename.py

27
Q

pdb module

A

It is a module with a set of utilities for debugging of Py programs.

  • pdb internally uses bdb (basic debugger) and cmd modules.
  • pdb runs purely in the command line.

Pdb debugger can be invoked in two ways
1. Command Line
python -m pdb fileName.py
2.pdb.set_trace()

import pdb
pdb.set_trace()

diff commands
list command - lists entire code with -> current line of code being executed

(Pdb) list
1 -> def fact(n):
2 f = 1
3 for i in range(1,n+1):
4 print (i)
5 f = f * i
6 return f
7 print(“Factorial of 5 =”,fact(5))
[EOF]

step command – moves line by line and if the next line is a function is moves inside the function line by line as well

next: Moves line by line, executes a called function completely without stepping inside a function, and pauses after the function call (at the next line of the calling function).
doesnt stop execution

break command – set breakpoints within a program. Line number must be given, where debugger will pause execution

ex: (Pdb) break 10

  • break command – Display all break points using break command without line number

continue command – This resumes the execution of the program, continuing from the point where the debugger paused (at a breakpoint

28
Q

Multiple copies of the recursive function are created in memory.

A recursive function must contain at least 1 return statement.

How can closures hide data

Lexical scoping

A

FALSE, There is only one copy of the function code in memory. The function itself is defined only once.

True

The inner function (closure) has access to vars defined in its enclosing function but these vars are not accessible outside the function

def counter():
count = 0 #var is hidden inside the counter function

Lexical scoping
scope (or context) of a var => where the var is defined in the code, not where the function is called.

A function remembers the vars from the scope where it was defined (not where it is called).

29
Q

What is closure in python? List any 3 criteria for closure
IMP learn syntax

11 A name clash results when the same identifier is used in more than one function of a given program.

12 A fully qualified identifier is an identifier with the name of the module that it belongs to prepended to it.

13 The main module of a Python program is the one that is directly
executed when a program is run.

A

def fib():
a,b=0,1
while True:
yield a
a,b=b,a+b

fi=fib()
for i in range(5):
print(next(fi))

False,True,True