Skip to content

Instantly share code, notes, and snippets.

@dharm6619
Created October 15, 2019 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dharm6619/2b6d39eba28a4c9cc0a598bf06ec07d1 to your computer and use it in GitHub Desktop.
Save dharm6619/2b6d39eba28a4c9cc0a598bf06ec07d1 to your computer and use it in GitHub Desktop.
Python Demo of Basic and functions
from functools import reduce
# import Module1 as model
from Module1 import Jutsu
from Module1 import Hello
from MyPackage import module1
# Defining a list over here
li = [1, 2, 3]
multiply = lambda x,y : x+y
# li2 = [i**2 for i in range(5)]
# print(li2)
li2 = [4,5,6,7,8]
add = []
# for each in li:
# sq.append(multiply(each))
# print(sq)
output = map(multiply, li2, li)
# print(list(output),type(output))
# Iterator function for returning the powers of 2
class PowerTwo:
def __init__(self, max_val_local=0):
self.max_val = max_val_local
def __iter__(self):
self.min_val = 0
return self
def __next__(self):
if self.min_val <= self.max_val:
result = 2 ** self.min_val
self.min_val += 1
return result
else:
raise StopIteration
# Iterator function for returning the square of numbers from a list
class SQL:
def __init__(self, li=[]):
self.inp = li
def __iter__(self):
self.start = 0
return self
def __next__(self):
res = []
if self.start < len(self.inp):
temp = self.inp[self.start] ** 2
res.append(temp)
self.start += 1
return temp
else:
raise StopIteration
def my_gen():
n = 1
print("This is the first call - ", n)
yield n
n += 1
print("More way to go Buddy - ", n)
yield n
n += 1
print("finally, lets put an end to this - ", n)
yield n
a = my_gen()
# next(a)
# next(a)
# next(a)
def my_even_gen():
n = 0
while n <= 20:
yield n
n += 2
# for a in my_even_gen():
# print(a)
def hello():
name = "Uchiha Sasuke"
print("HALLOOOOOOO ")
def uchiha():
nonlocal name
name = "Uchiha Itachi"
# nonlocal name
print( name," has snuck in....... Where could he be hiding >?>.......")
uchiha()
print(name)
def outer_function():
print("I'm in outer function")
def inner_function():
print("This is inner body")
return inner_function
# out = outer_function()
def out(data):
def inn():
print(data)
return inn
# got = out(10)
# got()
# del out
# got()
def mul(n):
def mkmul(x):
return x*n
return mkmul
times3=mul(3)
times5=mul(5)
# print(times3(9))
# print(times5(3))
# print(times5(times3(2)))
def closure_function(up):
value=0
print("up: ",up)
def nested_function(arg):
nonlocal value
for i in range(up+1):
value += i
print("Value: ",value)
value *= arg
print("Total is = ", value)
return nested_function
# returned_function = closure_function(5)
# returned_function(10)
# returned_function(4)
def outer():
print("I'm out for now")
def inner1():
print("Don't worry buddy I'm in")
def inner2():
print("Don't worry I'm also in....... We'll do it together")
return inner1, inner2
# out1 = outer()
# print(type(out1))
# for each in out1:
# each()
def make(func):
def inn():
print("I got decorated")
func()
return inn
@make
def ordinary():
print("I am ordinary")
# ordinary()
# pretty = make(ordinary)
# pretty()
def inc(x):
return x+1
def dec(x):
return x-1
def operate(func,x):
result = func(x)
return result
# print(operate(inc, 10))
# print(operate(dec, 10))
def smart(func):
def inner(a, b):
print("I'm going to divide ",a," and ",b)
if b==0:
print("Whoops! cannot divide")
return
else:
print(func(a, b))
return inner
def divide(a, b):
return a/b
# divide_op = smart(divide)
# divide_op(12, 6)
# divide_op(12, 0)
def line1(func):
def inner():
print("*********************************************")
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
func()
print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print("*********************************************")
return inner
@line1
def originals():
print("This is a function")
# originals()
lt = lambda x: "even" if x%2==0 else "Odd"
# lt(4)
# print(lt(5))
class test:
a = 90
# b = "Dharmendra Mishra"
c = 10
def __init__(self,first_name,last_name):
print("Hello : I am Edith")
self.first = first_name
self.last = last_name
def say_hello(self):
print("Hello ",self.first,self.last)
def add(self):
res = self.a + self.c
print("Total is: ", res)
# myclass = test("Dharmendra","Mishra")
# # print(myclass.b,myclass.a,)
# myclass.say_hello()
# myclass.add()
class Book:
def __init__(self,Book_Name, Cost):
self.book_name = Book_Name
self.cost = Cost
# print("The Book: ",self.book_name," has cost: ",self.cost)
def create_book(Name,Cost):
book = Book(Name,Cost)
return book
alchemist = create_book("The Alchemist",120)
harrypotter = create_book("Harry Potter", 500)
DaVinci = create_book("The Da Vinci Code", 350)
Book_Dict = {alchemist.book_name : alchemist.cost,
harrypotter.book_name : harrypotter.cost,
DaVinci.book_name : DaVinci.cost}
# print(Book_Dict['The Alchemist'])
def calculate_cost():
print("Enter your order")
book1 = int(input("Enter the Quantity for Alchemist - "))
book2 = int(input("Enter the Quantity for Harry Potter - "))
book3 = int(input("Enter the Quantity for DaVinci - "))
totalcost = Book_Dict['The Alchemist']*book1 + Book_Dict['Harry Potter']*book2 + Book_Dict['The Da Vinci Code']*book3
print("The Total Cost of your Order is: ",totalcost)
# calculate_cost()
class Person:
def __init__(self,Name):
self.name = Name
class Student(Person):
def __init__(self, Name, Branch, Year):
Person.__init__(self,Name)
self.branch = Branch
self.year = Year
def display_info(self):
return "%s studies %s and is in %s year." %(self.name, self.branch,self.year)
class Teacher(Person):
def __init__(self,name,exp,subject):
Person.__init__(self,name)
self.exp = exp
self.sub = subject
def dis_info(self):
print("The teacher",self.name," has a eperience of ",self.exp, " years in ",self.sub,"subject")
class BasicDetail:
def __init__(self, age, sex):
self.age = age
self.sex = sex
# arya = Teacher("AryaBhatt",7,"Mathematics")
# arya.dis_info()
class Child(Person, BasicDetail):
def __init__(self,name,age,sex):
Person.__init__(self,name)
BasicDetail.__init__(self, age,sex)
def child_display(self):
print("This child - ", self.name," is ", self.age," years old and is a ", self.sex)
# child1 = Child("Aakash",5,"Male")
# child1.child_display()
# fileRead = open('tester.txt',"r")
# content = fileRead.read()
# li1 = content.split("\n")
# print(li1)
# print(type(li1))
# fileRead.close()
#
# myfile = open('writer.txt',"w")
# myfile.write("I am Dharmendra Mishra")
# myfile.close()
#
# editer = open('writer.txt',"a")
# editer.write("\nLets add something to the end\n")
# editer.close()
#
# myfil = open('writer.txt',"r")
# cont = myfil.read()
# print(cont)
# print(type(myfil))
# myfil.close()
#
# print(myfil.name,myfil.mode,myfil.closed)
dictionary_a = [{'name':'python', 'points':10},{'name':'java', 'points':8}]
# outt = map(lambda x:x['name'].upper(),dictionary_a)
# for each in outt:
# print(each)
#
# outt1 = map(lambda x:x['points']*10,dictionary_a)
# for each in outt1:
# print(each)
#
# outt2 = map(lambda x:x['name']=="python",dictionary_a)
# for each in outt2:
# print(each)
def mul(x):
return x*5
def add(x):
return x+10
functions = [add,mul]
res = []
for i in range(5):
value = list(map(lambda x:x(i),functions))
res.append(value)
# print(res)
def even(n):
if n%2==0:
return n
a = [i for i in range(11)]
# print(a)
# list_out = map(even,a)
# # print(list_out)
# for each in list_out:
# print(each)
# li_out = filter(even,a)
# for each in li_out:
# print(each)
def sum(x1,x2):
return x1+x2
# def min(a,b):
# return a if a<b else b
print(reduce(lambda a,b:a if a<b else b,[1,2,3,4,5]))
print(reduce(lambda a,b:a if a>b else b,[1,2,3,4,5]))
list1 = ['Ram','Shyam','Jodhu','Modhu']
list2 = [4,6,7,9]
list3 = [40,45,32,12]
mapper = list(zip(list1,list2,list3))
print(mapper)
Hello("Dharmendra")
Jutsu("Kage Bun Shin")
print(module1.num1,module1.a_method())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment