Skip to content

Instantly share code, notes, and snippets.

@deepak1725
Created October 31, 2020 13:36
Show Gist options
  • Save deepak1725/cc97b2a23b6db2e05b6f26e39320ee5e to your computer and use it in GitHub Desktop.
Save deepak1725/cc97b2a23b6db2e05b6f26e39320ee5e to your computer and use it in GitHub Desktop.
Tutorial Reference
company = {"activa": "Bajaj", "access": "Suzuki", "zest": "TVS"}
def get_company(scooter):
return company.get(scooter)
scooter = input("Enter Scooter:")
print(f"{scooter} is manufactured by {get_company(scooter)}")
# ----------------
# Class
# ----------------
class Abc:
pass
# ----------------
# Functions
# ----------------
# a) Required Arg
def my_func(x):
return x*2
my_func()
# b) Keyword Arg.
def my_func_keyword(x):
return x*2
my_func_keyword(x=5)
# c) Default Arg.
def my_default_arg(number=2):
return number*2
my_default_arg()
my_default_arg(5)
my_default_arg(number=6)
# d) Variable Length
def variable_length_arg(*arg):
for each in arg:
print(each)
# ANONYMOUS Functions
x = lambda x: x*2
# --------------------------
# Pure Functions
# --------------------------
def multiply(x, y):
return x*y
# --------------------------
# Recursion
# --------------------------
def fib(n)
if (n <= 1)
return 1;
else
return fib(n - 1) + fib(n - 2);
# ------
# First Class Citizens
# ------
def multiply(x):
return x*2
def get_number(x, multiply):
return m(x)
get_number()
# -----
# Class Constructor
# -----
class Abc:
def __init__(self):
print("Contructor is called.")
# --------
# Types
# --------
# a) Integer
x = 1
# b) Float
y = 1.1
# c) Complex
z = 1+2j
# d) String
a = "Hello World."
# e) List
b = ["a", "b", "c"]
# f) Tuple
c = ("a", "b")
# g) Boolean
d = True
# h) Set
e = {"a", "b"}
# i) Dict
f = {"a": "A", "b": "B"}
# ******************************
# Loops
# a) while loop
i = 1
while i < 6:
print(i)
i += 1
# b) for loop
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
# *********************
# Exception handling
try:
print(a)
except Exception as e:
print("Exception occured.")
# ******************************
# Inheritance
# a) Single Level
# Base class
class Parent:
def func1(self):
print("This function is in parent class.")
# Derived class
class Child(Parent):
def func2(self):
print("This function is in child class.")
# Driver's code
obj = Child()
obj.func1()
obj.func2()
# ******************************
# b) Multiple
# Base class1
class Mother:
mothername = ""
def mother(self):
print(self.mothername)
# Base class2
class Father:
fathername = ""
def father(self):
print(self.fathername)
# Derived class
class Son(Mother, Father):
def parents(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
# Driver's code
s1 = Son()
s1.fathername = "RAM"
s1.mothername = "SITA"
s1.parents()
# ******************************
# c) MultiLevel
# Base class
class Grandfather:
def __init__(self, grandfathername):
self.grandfathername = grandfathername
# Intermediate class
class Father(Grandfather):
def __init__(self, fathername, grandfathername):
self.fathername = fathername
# invoking constructor of Grandfather class
Grandfather.__init__(self, grandfathername)
# Derived class
class Son(Father):
def __init__(self,sonname, fathername, grandfathername):
self.sonname = sonname
# invoking constructor of Father class
Father.__init__(self, fathername, grandfathername)
def print_name(self):
print('Grandfather name :', self.grandfathername)
print("Father name :", self.fathername)
print("Son name :", self.sonname)
# Driver code
s1 = Son('Prince', 'Rampal', 'Lal mani')
print(s1.grandfathername)
s1.print_name()
# ******************************
# Hierarchical
# Base class
class Parent:
def func1(self):
print("This function is in parent class.")
# Derived class1
class Child1(Parent):
def func2(self):
print("This function is in child 1.")
# Derivied class2
class Child2(Parent):
def func3(self):
print("This function is in child 2.")
# Driver's code
object1 = Child1()
object2 = Child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment