Skip to content

Commit ca57c37

Browse files
author
=Steavo
committed
Initial commit
0 parents  commit ca57c37

19 files changed

+320
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
other_lambda/

Decorators.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def div (a,b): #original function
2+
print(a/b)
3+
4+
5+
def smart_div(func): #decorator
6+
def inner(a,b): #new function which modifies the existing function
7+
if a<b:
8+
a,b=b,a
9+
return func(a,b)
10+
return inner
11+
12+
13+
div = smart_div(div) #assigning 2 functions (new function and old function)
14+
div(2,4) #calling the modiefied new function

Fibonacci.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def fib(n):
2+
3+
a=0
4+
b=1
5+
if n==0:
6+
print(0)
7+
elif(n<0):
8+
print("Enter valid number")
9+
else:
10+
a=0
11+
b=1
12+
print(a)
13+
print(b)
14+
15+
for i in range(2,n):
16+
c=a+b
17+
a=b
18+
b=c
19+
print(c)
20+
21+
fib(5)
22+
23+
24+
25+

Functions.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
#Types of arguments (Formal and Actual Parameters)
3+
4+
#Position
5+
def sum(a,b):
6+
c=a+b
7+
print(c)
8+
sum(5,6) #the value 5 and 6 are passed to the variables a and b respectively by their position
9+
print("------------")
10+
11+
#keyword
12+
def person(name,age):
13+
print(name)
14+
print(age-5)
15+
person(age=28,name='steavo')
16+
print("---------")
17+
18+
#Default
19+
def person (name ,age=18):
20+
print(name)
21+
print(age)
22+
person('steavo')
23+
print("-------------")
24+
#Variable length argument #important
25+
26+
27+
def sum(a,*b): #here first value is alloted to a, then the remaining values are converted into a tuple and stored in b
28+
c=a
29+
for i in b: #for iterating throught the tuple beacuse a tuple and an integer variable cannot be added
30+
c=c+i
31+
print(c)
32+
sum(3,3,4,4,5,56,8)
33+
34+
def sum(*b): #it can also be written like this
35+
c=0
36+
for i in b:
37+
c=c+i
38+
print(c)
39+
sum(1,2,3,4,5,6,7,8,9,10)
40+
41+
42+
43+
44+
45+
'''
46+
def add_sub(x,y):
47+
c=x+y
48+
d=x-y
49+
return c,d #we can return more than one values in python
50+
51+
result1,result2=add_sub(5,4)
52+
print(result1,result2)
53+
#'''

Lamda.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
Lamda functions are anonymuos functions which do not have a name and consist of only one expression
3+
'''
4+
'''
5+
f = lambda a: a*a
6+
result=f(5) #here f is the function
7+
print(result)
8+
9+
'''
10+
f = lambda a,b: a+b
11+
result=f(5,6)
12+
print(result)

Matrix.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from numpy import *
2+
arr=array ([ [1,2,3],[4,5,6] ])
3+
print(arr)
4+
print("----------------")
5+
m=matrix(arr) # matrix function is inbuilt in numpy #one way of creating a matrix
6+
print(m) #ouput looks similar but we can perform certain operations
7+
print("----------------")
8+
9+
# when input is taken from the user (second way of creating a matrix)
10+
m=matrix('3,2;4,1')
11+
print(m)
12+
print("----------------")
13+
m=matrix('3,2;4,1;1,3;7,7') # rows are separated from other rows by using a semicolon in between
14+
print("----------------")
15+
print(m)
16+
print("----------------")
17+
m1=matrix('1,2,3;4,5,6;7,8,9')
18+
print(m1)
19+
print("----------------")
20+
print(diagonal(m1)) #inbuilt function used to print the diagonal values of a matrix
21+
print(m1.min()) #prints min value in matrix
22+
print(m1.max()) #prints max value in matrix
23+
print("----------------")
24+
m2=matrix('9,8,7;6,5,4;3,2,1')
25+
m3=m1+m2 #matrix additon
26+
print(m3)
27+
print("----------------")
28+
m4=m1*m2 #matrix multplication
29+
print(m4)
30+
31+
32+
33+
34+
35+

Recursion.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
print(sys.getrecursionlimit())
3+
'''
4+
def greet():
5+
print("Hello")
6+
greet()
7+
8+
greet()
9+
10+
'''

Sample.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def shout(text):
2+
return text.upper()
3+
4+
print(shout('Hello'))
5+
6+
yell = shout
7+
8+
print(yell('Hello'))

Two_d_array.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from numpy import *
2+
arr1 = array([
3+
[1,2,3,4,5,6],
4+
[3,4,5,6,7,8]
5+
6+
])
7+
print(arr1)
8+
print(arr1.dtype) #it gives the data type of the array (here it is int32)
9+
print(arr1.ndim) #it gives what type of array it is
10+
print(arr1.shape) #it gives the number of rows and coloumns
11+
print(arr1.size) # if gives the size of the array i.e total number of elements
12+
print(arr1.flatten()) # it would flatten the array that is convet from 2d to 1d array
13+
14+
arr2=arr1.reshape(3,4) # (rows,columns ) required
15+
print(arr2)
16+
17+
arr3=arr1.reshape(2,2,3) #it would create a 3d array , which will have two 2 d arrays and
18+
# each 2d array would be having two 1d array which would be having 3 values each
19+
#reshape( two 2d array, two 1d array, three values in each 1d)
20+
print(arr3)
21+
22+

Userinput.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
x=['ss',3.9,3]
2+
for i in x:
3+
print(i)
4+
5+
#############################################################################
6+
7+
for i in range(1,6):
8+
for j in range(1,6):
9+
print("#",end =" ")
10+
11+
############################################################################
12+
a=int(input("Enter a number"))
13+
b=int(input("Enter another number"))
14+
print(a+b)
15+

factorial.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
def fact(n):
4+
fact=1
5+
for i in range(1,n+1):
6+
fact = fact*i
7+
# i+=1
8+
9+
return fact
10+
11+
12+
13+
14+
res=fact(5)
15+
print(res)

for_else.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#for else in python
2+
3+
nums=[14,34,43,34,56]
4+
5+
for num in nums :
6+
if num%5==0:
7+
print(num)
8+
break #break is compulsory
9+
else: #this else doesnt comes under if statement , but comes under for loop
10+
print("not found") #if no number is found in the list so instead of iterating over the if statement
11+
#else statement can be used with respect to for
12+

global.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
a=10
2+
b=7
3+
c=5
4+
f=0
5+
def dd():
6+
x=2
7+
8+
fh=globals()['a'] #will refer to all the global variables in the code unless specified by []
9+
print(fh)
10+
11+
12+
'''
13+
a=10
14+
def something():
15+
global a
16+
a=15
17+
#a=15
18+
print("in function",a)
19+
20+
something()
21+
22+
print("outside",a)
23+
'''

numpy1.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
#'''
3+
from numpy import *
4+
5+
arr=array([1, 2,3,4,5])
6+
print(arr)
7+
print(arr.dtype)
8+
#'''
9+
10+
arr=linspace(1,15,3) #by using linspace function
11+
print(arr) #takes three parameters start stop
12+
"""
13+
from numpy import *
14+
#arr=linspace(0,5,6)
15+
arr=logspace(0,4,2)
16+
print(arr)
17+
print('%.2f' %arr[1])

other_lambda.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
from functools import reduce
3+
4+
doubles=[4,12,16,8,12,4]
5+
sum1=reduce(lambda a,b:a+b,doubles)
6+
print(sum1)
7+
8+
###########################################################################
9+
10+
def is_even(n):
11+
return n%2==0
12+
13+
nums=[9,99,8,8,467]
14+
evens=list(filter(is_even,nums))
15+
print(evens)
16+
17+
18+

passing_list_in_function.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
def count(lst):
3+
even =0
4+
odd=0
5+
6+
for i in lst :
7+
if i%2==0:
8+
even+=1
9+
else:
10+
odd+=1
11+
12+
return even,odd
13+
14+
lst=[2,44,53,43,3,32,23,34,32,324,2]
15+
even,odd =count(lst)
16+
17+
print("even :{} odd: {}".format(even,odd))

patterns.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
for r in range(5):
3+
for c in range(r):
4+
print("#",end=" ") #end =" " is used such that the control wont go to the next line
5+
print() #used for getting into new line // print("\n") can also be used
6+
'''
7+
'''
8+
for r in range(4):
9+
for c in range(r+1):
10+
print("#",end=" ") #end =" " is used such that the control wont go to the next line
11+
print() #used for getting into new line // print("\n") can also be used
12+
'''
13+
'''
14+
for r in range(4,0,-1): #range(4)
15+
for c in range(r): # range(4-r)s
16+
print("#",end=" ") #end =" " is used such that the control wont go to the next line
17+
print() #used for getting into new line // print("\n") can also be used
18+
19+
'''

special_name.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(__name__)

test.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from numpy import *
2+
arr=array([1,2,3])
3+
print(arr.size)

0 commit comments

Comments
 (0)