Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Inheritance/Inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "The animal makes a sound."
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
class Cow(Animal):
def speak(self):
return f"{self.name} says Moo!"
def main():
dog = Dog("Buddy")
cat = Cat("Whiskers")
cow = Cow("Daisy")
animals = [dog, cat, cow]
for animal in animals:
print(animal.speak())
if __name__ == "__main__":
main()
29 changes: 29 additions & 0 deletions machine learning .py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Advanced: Machine Learning Classifier Example
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# Load sample dataset
data = load_iris()
X, y = data.data, data.target

# Split into train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize and train the model
model = DecisionTreeClassifier()
model.fit(X_train, y_train)

# Make predictions
predictions = model.predict(X_test)

# Evaluate
accuracy = accuracy_score(y_test, predictions)
print(f"✅ Model trained successfully!")
print(f"Accuracy: {accuracy:.2f}")

# Predict for a single sample
sample = X_test[0].reshape(1, -1)
predicted_class = model.predict(sample)[0]
print(f"Example prediction → Class: {data.target_names[predicted_class]}")
24 changes: 24 additions & 0 deletions object oriented programing .py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Task #32: Inheritance and Method Overriding Example

class Animal:
def __init__(self, name):
self.name = name

def sound(self):
return "Some generic animal sound"

# Dog inherits from Animal
class Dog(Animal):
def sound(self):
return "Woof! Woof!"

# Cat inherits from Animal
class Cat(Animal):
def sound(self):
return "Meow!"

# Demonstration
animals = [Dog("Buddy"), Cat("Kitty"), Animal("Creature")]

for animal in animals:
print(f"{animal.name} says: {animal.sound()}")
20 changes: 20 additions & 0 deletions string slicing and indexing task .py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# String Slicing and Indexing Example

# Input a string from the user
text = input("Enter a string: ")

# Displaying first and last character
print("First character:", text[0])
print("Last character:", text[-1])

# Display a substring (from index 2 to 5)
print("Substring (index 2 to 5):", text[2:6])

# Reverse the string
print("Reversed string:", text[::-1])

# Print every second character
print("Every 2nd character:", text[::2])

# Length of the string
print("Length of the string:", len(text))