In financial terms a negative balance is represented with round barackets around the number instead of - sign. Write a function numeric_value which returns actual numeric value. For example a value "(1234)" should get -1234 as numeric value. while "34" will get value as 34
β reverse_digits(4223)
β reverse_digits(1)
β reverse_digits(123)
π Congratulations! You have successfully solved problem reverse_digits!!
Scope of variables
X =10def foo(): X =20foo()print(X)
10
del X
print(X)
NameError: name 'X' is not defined
X =10def foo():# X is not defined in function locally, but is there globalyprint(X)foo()
10
If a function refers to a variable X
It looks if X is there in local namespace (functionβs namespace).
If X is in local namespace it will make use of it.
If X is not available in local namespace, then it will ask global namespace, do you have it and I will use it for reading purpose!
del X
X =10def foo(): X =20print("print from function X =",X)foo()print("print from gloabl X =",X)
print from function X = 20
print from gloabl X = 10
del X
X =10def foo(): X = X +1# conflict is ... you want to create local variable and also use global variablefoo()print(X)
UnboundLocalError: local variable 'X' referenced before assignment
l
[5, 4, 3, 2, 1]
l.append(0)
l
[5, 4, 3, 2, 1, 0]
X = [1, 1, 1]def appendzero(a): a.append(0) appendzero(X)print(X)
[1, 1, 1, 0]
Why this has happened?
arguments are referering to same object which is there in global namespace
only name with which it was refered in function is different
i =10
i.add(1) # this method is not supperted in ints
AttributeError: 'int' object has no attribute 'add'
y =10def addone(x): x = x +1# the assignment operator make x localaddone(y) # the argument has connected x and y to same locationprint(y)
10
[1, 1, 2] + [0, 0, 0] # concatenates the lists
[1, 1, 2, 0, 0, 0]
X = [1, 1, 1]def appendzero(a): a = a + [0] # the operation is same but it will not modify a in place# this will create new a in localnamespaceappendzero(X)print(X)
[1, 1, 1]
l
[5, 4, 3, 2, 1, 0]
l.insert(2, -3)
l
[5, 4, -3, 3, 2, 1, 0]
ones = [1, 1, 1, 1]
ones.index(1) # this returns
0
ones.append(0) # this does not return ... any function or method that does not return , actually returns None
ones
[1, 1, 1, 1, 0]
None.append(0)
AttributeError: 'NoneType' object has no attribute 'append'
ones.insert(2, -1) # does not return, modifies the list in place
ones.insert(2, -1).append(1)
AttributeError: 'NoneType' object has no attribute 'append'
# here student is loop variable, it will get values from the collection one by one automatically. # classroom is any collection on which you want to for student in classroom:print("Hello", student)print(student.capitalize())# after this line execution goes back to first line of for loop body
Hello chaittanya
Chaittanya
Hello shuddhatma
Shuddhatma
Hello rohit
Rohit
Hello simran
Simran
Hello alex
Alex
for c in"this text as a collection":print(c)
t
h
i
s
t
e
x
t
a
s
a
c
o
l
l
e
c
t
i
o
n
print function by default prints \n (newline) after every print call
for c in"this text as a collection":print(c, end=",")
sqrs = [] # emptyfor i inrange(10): sqrs.append(i**2)
sqrs
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
def compute_squares(nums): sqr = []for i in nums: sqr.append(i*i)return sqr
compute_squares([1, 3, 2, 5, 2, 8, 3])
[1, 9, 4, 25, 4, 64, 9]
def squares(nums): sqr = []for i in nums: sqr.append(i*i)return sqr # the momenet return statement is executed function call is over
squares([2, 3, 4, 5, 64, 3])
[4]
squares([5, 3, 4, 5, 64, 3])
[25]
problem
Write your own function called mysum. It takes list of numbers and finds sum of those
Write your own function called product. it takes list of numbers and returns product of all numbers from the list
sum([1, 2, 3, 4, 5])
15
def mysum(nums):returnsum(nums) # not like this! make use of for loop to compute from scratch
1 1 1 2 3 4
0
0+1 = 1
1+1 = 2
2+1 = 3
3+2 = 5
.
.
def mysum(nums): s =0for n in nums: s = s + nreturn s
mysum(range(10)) # sum of 0 to 9
45
mysum(range(101)) # sum of first 100 number
5050
def product(nums): p =1for i in nums: p = p * ireturn p
product([1, 2, 3, 4])
24
def factorial(n):return product(range(1, n+1)) # this will create number from 1 to n
factorial(5)
120
factorial(10)
3628800
def factorial1(n): p =1for i inrange(1, n+1): p = p * ireturn p
Pythonish way of programming is DRY (Do not repeate yourself!) .
Make use of already written function built new ones
while loop
1 1 2 3 5 8 13 21 β¦. you add last two Fibonacci numbers to get next one
print fibonacci numbers less than n
def print_fib(n):"""Print fibonnaci numbers less than n """ curr, prev =1, 1while prev < n: # after while put a condition.print(prev, end=",") curr, prev = prev+curr, curr
print_fib(20)
1,1,2,3,5,8,13,
print_fib(50)
1,1,2,3,5,8,13,21,34,
2<3
True
3>2
True
"name".startswith("n")
True
Conditions
"name".startswith("v")
False
"hel"in"hello"
True
"one"in words # check if "one" is in the list words
def countdown(n): i = nwhile i >0:print(i) i = i -1# if you forget to change the variable on which condition is dependent... while loop will execute forever