8000 2024/03/19 - Adding Demo python files · rphadtare/python_by_examples@99e95ac · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 99e95ac

Browse files
committed
2024/03/19 - Adding Demo python files
1 parent dfcb932 commit 99e95ac

9 files changed

+351
-3
lines changed

concepts/PythonDemo01_Variables.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def _inside_func() -> None:
5151
b = a
5252
print("ID of a ", id(a))
5353
print("ID of b ", id(b))
54-
a = 100
55-
print("ID of a after assigning new value ", id(a))
56-
54+
b = 100
55+
print("after assigning new value ")
56+
print("ID of a ", id(a))
57+
print("ID of b ", id(b))

concepts/PythonDemo04_Functions.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# This file gives demo about functions
2+
3+
def function_with_arg(arg1, arg2="Pooja"):
4+
print("Value of arg1 ", arg1)
5+
print("Value of arg2 ", arg2)
6+
print("*" * 25)
7+
8+
9+
print("Calling function_with_arg with one parameter")
10+
function_with_arg("Rohit")
11+
12+
print("Calling function_with_arg with two parameter values")
13+
function_with_arg("Rohit", "Phadtare")
14+
15+
# calling function with arguments name
16+
print("Calling function_with_arg with argument name")
17+
function_with_arg(arg2="Rohit", arg1="Phadtare")
18+
19+
20+
def function_var_arg_demo(*args):
21+
print("Inside function_var_arg_demo...")
22+
cnt = 1
23+
for i in args:
24+
print("param{} value is {} ".format(cnt, i))
25+
cnt += 1
26+
27+
print("*" * 25)
28+
29+
30+
function_var_arg_demo("Rohit", "Prakash", "Phadtare")
31+
32+
33+
def function_var_karg_demo(**kargs_list):
34+
print("Inside function_var_karg_demo...")
35+
cnt = 1
36+
for param_name, param_value in kargs_list.items():
37+
print("{} value is {} ".format(param_name, param_value))
38+
cnt += 1
39+
40+
print("*" * 25)
41+
42+
43+
function_var_karg_demo(first_name="Rohit", middle_name="Prakash", last_name="Phadtare")
44+
45+
46+
def lambda_func_demo(input_list):
47+
print("Inside lambda_func_demo .. ")
48+
print("Given list : ", input_list)
49+
50+
list_even_num = list(filter(lambda n: (n % 2 == 0), input_list))
51+
52+
print("Filtered list with even values : ", list_even_num)
53+
print("*" * 25)
54+
55+
56+
lambda_func_demo(range(10, 21))

concepts/PythonDemo05_Arrays.py

Lines changed: 43 additions & 0 deletions
+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This file gives demo about array
2+
from array import *
3+
4+
# creating array of integers and empty array of float
5+
i_arr = array('i', [10, 20, 30])
6+
f_arr = array('f')
7+
8+
# Traverse array
9+
for i in range(len(i_arr)):
10+
print("arr[", i, "]", " - ", i_arr[i])
11+
12+
# insert elements using append and insert method
13+
f_arr.append(11) # no need of index in append method as it will append at end position
14+
print("11 is appeneded in f_arr")
15+
f_arr.insert(0, 22) # insert will need index to insert value at that position and adjust old values post insertion
16+
print("22 is inserted in f_arr at 0 index")
17+
f_arr.insert(5, 33) # if index is not present then it will add at current last_index + 1 position
18+
print("33 is inserted in f_arr at 5 index")
19+
20+
# update element at last position in arr1
21+
print("f_arr - ", f_arr)
22+
f_arr[-1] = 55
23+
print("Post update of element at last position f_arr is - ", f_arr)
24
25+
# delete element from array
26+
print("Deleting element from i_arr - ", i_arr[0])
27+
del i_arr[0]
28+
print("i_arr post deletion - ", i_arr)
29+
30+
# search element in array
31+
ele = input("enter number to search")
32+
cnt = 0
33+
34+
for i in range(len(i_arr)):
35+
if i_arr[i] == int(ele):
36+
print("Number {} found at {} position in i_arr".format(ele, i))
37+
cnt += 1
38+
39+
if cnt == 0:
40+
print("number {} is not present in i_arr ".format(ele))
41+
42+
# using in operator
43+
print("Number {} is present in i_arr : ".format(ele), int(ele) in i_arr)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This file gives demo about class and object
2+
3+
# Class Person
4+
class Person:
5+
def __init__(self, pid, name=None, age=None, gender=None):
6+
"""constructor for person class"""
7+
self.pid = pid
8+
self.name = name
9+
self.age = age
10+
self._gender = gender
11+
12+
def display_person_details(self):
13+
print("Person details are -")
14+
print("ID: ", self.pid, " Name: ", self.name, " Age : ",
15+
self.age, " Gender : ", self._gender)
16+
print("*" * 100)
17+
18+
19+
# Creating Object
20+
p1 = Person(1, "Rohit Phadtare", 31, 'M')
21+
p2 = Person(2)
22+
23+
p1.display_person_details()
24+
p2.display_person_details()
25+
26+
27+
class Shape:
28+
29+
def __init__(self, name_of_shape):
30+
self._name = name_of_shape
31+
32+
def __str__(self):
33+
return "Shape is " + self._name
34+
35+
36+
my_shape = Shape("Circle")
37+
print(my_shape)
38+
39+
for _ in range(100):
40+
print(_)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file gives demo about multilevel inheritance
2+
3+
class Animal:
4+
5+
animal_id = 0
6+
def __init__(self, name, id):
7+
print("This is animal...")
8+
self.__name = name
9+
self.animal_id = id
10+
11+
def speak(self):
12+
print("Animal {} with id {} speaks ..."
13+
.format(self.__name, self.animal_id))
14+
15+
16+
class Dog(Animal):
17+
18+
def __init__(self, name, id_):
19+
super().__init__("Dog", id_)
20+
print("This is Dog..")
21+
self.__name = name
22+
23+
def speak(self):
24+
print("Dog with name '{}' and with id {} barks ..."
25+
.format(self.__name, self.animal_id))
26+
27+
28+
d = Dog("Tobo", 1)
29+
d.speak()
30+
print(d.__dict__)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This file gives demo about multilevel inheritance
2+
3+
4+
class Company:
5+
def __init__(self):
6+
print("This is Company ..")
7+
8+
def show(self):
9+
print("We manufactures vehicles ..")
10+
11+
12+
class BikeCompany(Company):
13+
14+
def __init__(self):
15+
super().__init__()
16+
print("This is BikeCompany ..")
17+
18+
def show(self):
19+
super().show()
20+
print("We manufactures bikes ..")
21+
22+
23+
class CarCompany(Company):
24+
25+
def __init__(self):
26+
super().__init__()
27+
print("This is CarCompany ..")
28+
29+
def show(self):
30+
super().show()
31+
print("We manufactures cars ..")
32+
33+
34+
class BusCompany(Company):
35+
36+
def __init__(self):
37+
super().__init__()
38+
print("This is BusCompany ..")
39+
40+
def show(self):
41+
super().show()
42+
print("We manufactures buses ..")
43+
44+
45+
class Honda(BusCompany, CarCompany, BikeCompany):
46+
47+
def __init__(self):
48+
super().__init__()
49+
print("This is Honda company ..")
50+
51+
def show(self):
52+
super().show()
53+
print("We are Honda company")
54+
55+
56+
h1 = Honda()
57+
h1.show()
58+
59+
print(h1.__class__.__mro__)
60+
61+
62+
63+
64+
65+
66+

concepts/PythonDemo09_Polymorphism.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Person:
2+
3+
def __init__(self, name_="", id_=0):
4+
self.name = name_
5+
self.id = id_
6+
7+
def __str__(self):
8+
return "Person [ name = {} , id = {} ]".format(self.name, self.id)
9+
10+
def show(self):
11+
print(self)
12+
print("*"*50)
13+
14+
15+
class Student(Person):
16+
def __init__(self, name_="", id_=0, std_=""):
17+
super().__init__(name_, id_)
18+
self.std = std_
19+
20+
def __str__(self):
21+
return "Student [ name = {} , id = {}, std = {} ]".format(self.name, self.id, self.std)
22+
23+
def show(self):
24+
print(self)
25+
print("*"*50)
26+
27+
28+
class Employee(Person):
29+
def __init__(self, name_="", id_=0, salary=0):
30+
super().__init__(name_, id_)
31+
self.salary = salary
32+
33+
def __str__(self):
34+
return "Employee [ name = {} , id = {}, salary = {} ]".format(self.name, self.id, self.salary)
35+
36+
def show(self):
37+
print(self)
38+
print("*"*50)
39+
40+
41+
p = Person("Rohit", 1)
42+
s = Student("Rohit", 1, "12th")
43+
e = Employee("Rohit", 1, 2000)
44+
45+
for x in (p, s, e):
46+
x.show()
47+

concepts/PythonDemo10_Abstraction.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This file gives demo about abstraction
2+
3+
from abc import ABC
4+
5+
6+
class Shape(ABC):
7+
def draw(self):
8+
pass
9+
10+
def show(self, shape_name=''):
11+
print("This is {} shape".format(shape_name))
12+
13+
14+
class TriangleShape(Shape):
15+
16+
def __init__(self, shape_name="Triangle"):
17+
self.shapeName = shape_name
18+
19+
def draw(self, a=0, b=0, c=0):
20+
print("Triagle[side1={}, side2={}, side3={}]".format(a, b, c))
21+
print("*"*50)
22+
23+
24+
class RectangleShape(Shape):
25+
26+
def __init__(self, shape_name="Rectangle"):
27+
self.shapeName = shape_name
28+
29+
def draw(self, length=0, breadth=0):
30+
print("Rectangle[length={}, breadth={}".format(length, breadth))
31+
print("*"*50)
32+
33+
34+
s1 = TriangleShape()
35+
s2 = RectangleShape()
36+
37+
for s in [s1, s2]:
38+
s.show(s.shapeName)
39+
if isinstance(s, TriangleShape):
40+
s.draw(10, 20, 30)
41+
else:
42+
s.draw(15, 25)
43+

concepts/Sample.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class BaseClass:
2+
def call_me(self):
3+
print("Base Class Method")
4+
5+
class LeftSubclass(BaseClass):
6+
def call_me(self):
7+
super().call_me()
8+
print("Left Subclass Method")
9+
10+
class RightSubclass(BaseClass):
11+
def call_me(self):
12+
super().call_me()
13+
print("Right Subclass Method")
14+
15+
class Subclass(LeftSubclass, RightSubclass):
16+
def call_me(self):
17+
super().call_me()
18+
print("Subclass Method")
19+
20+
subClass = Subclass()
21+
print(subClass.__class__.__mro__)
22+
subClass.call_me()

0 commit comments

Comments
 (0)
0