Skip to content

Commit acd9575

Browse files
committed
Python practice . .
1 parent bfb2b94 commit acd9575

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1079
-4
lines changed

Class_In_Python.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# class in python . .
2+
3+
# creating new class..
4+
class computer:
5+
# creating function . .
6+
def config(self):
7+
print("i5, 16gb, 1TB")
8+
9+
# creating opject of class . .
10+
com1 = computer()
11+
com2 = computer()
12+
13+
# call class with object . .
14+
computer.config(com1)
15+
16+
# calling function using class object . .
17+
com2.config()
18+
19+
20+
# creat new class . .
21+
class Car:
22+
wheels = 4 # class / special variable . .
23+
# declaration of new init function . .
24+
25+
@staticmethod
26+
def __init__(self):
27+
# instance variable . .
28+
self.mil = 10
29+
self.com = "BMW"
30+
31+
class feature:
32+
def power(self):
33+
return 'powerful'
34+
35+
# c1 = Car()
36+
# c1.mil = 11
37+
# print(c1.mil, c1.com, c1.wheels)
38+
# Car.feature(c1)
39+
# print(c1.feature)
40+
41+

Inheritance_In_Pyhton.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Inheritance in pyhton . .
2+
3+
class A:
4+
def feature1(self):
5+
print("feature1 working . . ")
6+
7+
# single level inheritance . .
8+
class B(A):
9+
# inti function . .
10+
def __init__(self):
11+
print("In b init . .")
12+
13+
def feature2(self):
14+
print("feature2 working . . ")
15+
16+
a1 = A()
17+
b1 = B()
18+
19+
a1.feature1()
20+
b1.feature1()
21+
b1.feature2()
22+
23+
# multi-level inheritance . .
24+
class C(B):
25+
def __init__(self):
26+
super().__init__()
27+
print("In c init . .")
28+
29+
# class C is subclass of class B so class c can use class b init function . .
30+
# If you create object of sub class it will first try find init of sub class
31+
# if it is not found then it will call init of super class
32+
def feature3(self):
33+
print("feature3 working . . ")
34+
35+
c1 = C()
36+
c1.feature3()
37+
38+
# multiple inheritance . .
39+
class D(C, B):
40+
def __init__(self):
41+
super().__init__()
42+
print("In d init . .")
43+
44+
# multiple inheritence using comma separated classes in parenthesis
45+
def feature4(self):
46+
return super().feature4()
47+
48+
d1 = D()
49+
50+
# multi-level Inheritance . .
51+
class X:
52+
print("Feature x working . .")
53+
54+
class Y(X):
55+
print("feature y working . .")
56+
57+
class Z(Y):
58+
print("feature z working . .")
59+
60+
# multiple inheritance . .
61+
class W(Z, Y):
62+
pass
63+
64+
z1 = Z()

InputsInPython.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
name = input("What is your name ? ")
2-
print(name)
2+
print(name)
33

44
superHeroName = input("What is your superHero name ? ")
5-
print("Hello " +superHeroName)
5+
print(name + " " + superHeroName + "\nLength of string : " + str(len(name + " " + superHeroName)))
6+
7+
8+
# new python input excercise . .
9+
10+
print("Welcome to our band name generator ! ! ")
11+
12+
city = input("What is your birth place : \n")
13+
14+
pet_Name = input("Enter your pet name : \n")
15+
16+
print("your band name could be : " + city + " " + pet_Name)

Polymorphism_In_Pyhton.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# from multipledispatch import dispatch
2+
3+
# Polymorphism in python . .
4+
"""In Python polymorphism is the ability of an object to take on multiple forms or act as """
5+
6+
# duck typing . .
7+
"Duck Typing means if it behaves like duck it probably is duck . . "
8+
9+
class pyCharm:
10+
def execute(self):
11+
print("Executing in Pycharm")
12+
print("compiling and running . . ")
13+
14+
# this both class have same method name execute it behaves same. .
15+
16+
class MyEditor:
17+
def execute(self):
18+
print("Executing my editor...")
19+
print("spell ckeck . .")
20+
print("convension check . . ")
21+
print("Compile and running . . ")
22+
23+
class Laptop:
24+
25+
def code(self, ide):
26+
ide.execute()
27+
28+
# ide = pyCharm()
29+
ide = MyEditor()
30+
lap = Laptop()
31+
lap.code(ide)
32+
33+
# Operator Overloading. . .
34+
35+
a = 5
36+
b = 4
37+
print(a + b)
38+
39+
# using magic method . .
40+
# this are operator overloading
41+
# same method name but the arguments are differrent like number of arguments and type of arguments . . .
42+
print(int.__add__(a, b)) # add method
43+
print(int.__sub__(a, b)) # sub method
44+
print(int.__mul__(a, b)) # mul method
45+
46+
47+
# method overloading . . .
48+
49+
# python dose not support method overloading in normal way . .
50+
class student:
51+
def __init__(self) -> None:
52+
pass
53+
54+
# in this method parameters have overloading . . .
55+
def sum(self, a=None, b=None, c=None):
56+
s = 0
57+
if a is not None and b is not None and c is not None:
58+
s = a + b + c
59+
elif a is not None and b is not None:
60+
s = a + b
61+
else:
62+
s = a
63+
return s
64+
65+
s1 = student()
66+
print(s1.sum(12, 34))
67+
68+
# # method overloading in other way . .
69+
# @dispatch(int, int)
70+
# def A(n, m):
71+
# s = n + m
72+
# print(s)
73+
74+
# def A(n, m, o):
75+
# s = n + m - o
76+
# print(s)
77+
78+
79+
# A(23, 12)
80+
# A(23, 14, 45)
81+
82+
83+
# Method Overriding . .
84+
85+
class A:
86+
def show(self):
87+
print("In A show")
88+
89+
# in this show method of class A override in class B . .
90+
class B(A):
91+
def show(self):
92+
print("In B show")
93+
94+
a1 = B()
95+
a1.show()
96+

age_At_Rollercoaster.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
print("Welcome to RollerCoaster ! ")
2+
3+
height = int(input("Enter your height in cm : "))
4+
5+
if height >= 150:
6+
print("You can ride the rollercoaster ! ")
7+
age = int(input("Enter your age : "))
8+
if age <= 20:
9+
print("pay $7")
10+
else:
11+
print("pay $10")
12+
else:
13+
print("Sorry, You have to grow taller before you can ride.")
14+

area_Function.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import math
2+
3+
def area(height, width, cover):
4+
area = height * width
5+
area_to_cover = math.ceil(area / cover)
6+
print(f"Area for use = {area_to_cover}")
7+
8+
height = int(input("Enter Height : "))
9+
width = int(input("Enter width :"))
10+
area(height, width, cover=5)

automatic_Password_generator.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Automatic Password generator . .
2+
3+
import random
4+
5+
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
6+
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
7+
'w', 'x', 'y', 'z'
8+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
9+
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
10+
'W', 'X', 'Y', 'Z'
11+
]
12+
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
13+
symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
14+
'-', '_', '+', '/', '|']
15+
16+
print("Welcome to password Generator ! ")
17+
18+
total_Alpha = int(input("How many letters would you like in your password : \n"))
19+
total_Num = int(input("How many numbers would you like in your password : \n"))
20+
total_Sym = int(input("How many symbols would you like in your password : \n"))
21+
22+
23+
password_List = [] # empty list
24+
25+
# using random choice method . .
26+
for letters in range(1, total_Alpha + 1):
27+
password_List += random.choice(alphabets)
28+
29+
for num in range(1, total_Num + 1):
30+
password_List += random.choice(numbers)
31+
32+
for symbls in range(1, total_Sym + 1):
33+
password_List += random.choice(symbols)
34+
35+
# easy method . .
36+
37+
password = ""
38+
for lettter in password_List:
39+
password += lettter
40+
41+
print(f"Your password : {password}")
42+
43+
# hard method . . .
44+
45+
# random shuffle method use . .
46+
random.shuffle(password_List)
47+
print(password_List)
48+
49+
# converting password list to single word . .
50+
password = ""
51+
for lettter in password_List:
52+
password += lettter
53+
54+
print(f"Your password : {password}")

binary_Search.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Binary search method using python . .
2+
3+
def binary_Search(arr, key):
4+
start = 0
5+
end = len(arr) - 1
6+
for i in range(len(arr)):
7+
# calculate mid value . .
8+
mid = int((start + end) / 2)
9+
# get ans when mid is equal to key number
10+
if arr[mid] == key:
11+
return mid
12+
13+
elif arr[mid] < key:
14+
start = mid + 1 # reset start point
15+
16+
else:
17+
end = mid - 1 # reset end point . .
18+
19+
return -1
20+
21+
# list
22+
arr = input("Enter number into list with ascending order : ").split()
23+
# indexing list
24+
for i in range(len(arr)):
25+
arr[i] = int(arr[i])
26+
# print(arr)
27+
28+
key = int(input("Enter which number you want to find from list : "))
29+
30+
index = int(binary_Search(arr, key))
31+
32+
if index == -1:
33+
print("Not found . .")
34+
else:
35+
print(f"Number is found at index {index}")

blackjack_cards_game.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import random
2+
3+
def deal_card():
4+
"""return random card from deck . . """
5+
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
6+
card = random.choice(cards)
7+
return card
8+
9+
user_cards = []
10+
computer_cards = []
11+
12+
for _ in range(2):
13+
user_cards.append(deal_card)
14+
computer_cards.append(deal_card)
15+
16+
17+
def calculate_score(cards):
18+
if sum(cards) == 21 and len(cards) == 2:
19+
return 0
20+
21+
if 11 in cards and sum(cards) > 21:
22+
cards.remove(11)
23+
cards.append(1)
24+
25+
return sum(cards)
26+
27+
calculate_score(user_cards)

0 commit comments

Comments
 (0)