Skip to content

Commit aa809f7

Browse files
Add files via upload
1 parent 8525916 commit aa809f7

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

+890
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Defining the Student class
2+
class Student:
3+
def __init__(self,name):
4+
self.name= name
5+
6+
# Creating two objects
7+
jack = Student("Jack")
8+
bob = Student("Bob")
9+
jack2=Student("Jack")
10+
11+
# Verifying the object's identity
12+
print(f"Are jack and bob the same object? {jack is bob}")
13+
print(f"Is jack different from bob ? {jack is not bob}")
14+
print(f"Are jack and jack2 the same object? {jack is jack2}")
15+
16+
# Verifying the IDs of jack and jack2
17+
print(f"The ID of jack is {id(jack)}")
18+
print(f"The ID of jack2 is {id(jack2)}")
19+
20+
# Checking the types of jack and bob
21+
print(f"Is jack a Student object? {type(jack) is Student}")
22+
print(f"Is jack an int type? {type(jack) is int}")
23+
print(f"Is bob not an int type? {type(bob) is not int}")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# String
2+
message = "Welcome"
3+
print(f"The string is: {message}")
4+
print(f"Is 'com' present in the string? {'com' in message}") # True
5+
print(f"Is 'com' absent in the string? {'com' not in message}") # False
6+
print("-"*10)
7+
# Tuple
8+
numbers = (1, 2, 3)
9+
print(f"The contents of the tuple are: {numbers}")
10+
print(f"Is 1 present in the tuple? {1 in numbers}") # True
11+
print(f"Is 2 absent in the tuple? {2 not in numbers}") # False
12+
print("-"*10)
13+
# Dictionary
14+
sample_dict = {"key1": 10, "key2": 25}
15+
print(f"The contents of the dictionary are: {sample_dict}")
16+
print(f"Is key1 a key in the dictionary? {'key1' in sample_dict}") # True
17+
print(f"Is 20 a value in the dictionary? {20 in sample_dict.values()}") # False
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Color:
2+
fav_color = "Green"
3+
4+
def __init__(self, color):
5+
self.fav_color = color
6+
7+
def instance_method(self):
8+
print("The instance method is called.")
9+
print(f"My favorite color is: {self.fav_color}")
10+
print("-"*15)
11+
12+
@staticmethod
13+
def static_method():
14+
print("The static method is called.")
15+
print("You can call me without creating an instance.")
16+
print(f"My favorite color is: {Color.fav_color}")
17+
print("-"*15)
18+
19+
@classmethod
20+
def class_method(cls):
21+
print("The class method is called.")
22+
print("You can call me without creating an instance.")
23+
print(f"My favorite color is: {cls.fav_color}")
24+
print("-" * 15)
25+
26+
# Creating an object from the Color class
27+
favorite_color = Color("Blue")
28+
29+
# Calling the instance method
30+
favorite_color.instance_method()
31+
# Calling the static method
32+
Color.static_method()
33+
# Calling the class method
34+
Color.class_method()
35+
36+
# print("*"*20)
37+
38+
# # Alternative code
39+
# # Calling the instance method
40+
# favorite_color.instance_method() # Also works
41+
# # Calling the static method
42+
# favorite_color.static_method() # Also works
43+
# # Calling the class method
44+
# favorite_color.class_method() # Also works
45+
46+
# print("*"*20)
47+
48+
# # Alternative code
49+
# # Calling the instance method
50+
# Color.instance_method(favorite_color) # Also works
51+
# # Calling the static method
52+
# Color.static_method() # Also works
53+
# # Calling the class method
54+
# Color.class_method() # Also works
55+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Color:
2+
color = "Green"
3+
def display(self):
4+
print(f"My favorite color is: {self.color}")
5+
6+
# @classmethod
7+
def update_color(cls, newcolor):
8+
cls.color = newcolor
9+
print(f"The default color is updated to {newcolor}.")
10+
11+
print(f"The current default is: {Color.color}")
12+
# making an instance of the Color class
13+
color1= Color()
14+
color1.display()
15+
color1.update_color("Blue")
16+
color1.display()
17+
print(f"The current default is: {Color.color}") # Green now
18+
# print(color1.color) # Still Blue
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Parent:
2+
def instance_method(self):
3+
print(f"The instance method is called.{self}")
4+
@staticmethod
5+
def static_method():
6+
print("The static method is called.")
7+
@classmethod
8+
def class_method(cls):
9+
print(f"The class method is called.{cls}")
10+
11+
class Child(Parent):
12+
""" This is a Child class."""
13+
pass
14+
15+
16+
# Creating an object from the Parent class
17+
sample_object = Parent()
18+
sample_object.instance_method()
19+
# Parent.static_method()
20+
sample_object.static_method() # Also ok
21+
# Parent.class_method()
22+
sample_object.class_method() # Also ok
23+
24+
print("*"*20)
25+
26+
# Using the Child class object now,
27+
sample_object = Child()
28+
sample_object.instance_method()
29+
30+
# Child.static_method()
31+
sample_object.static_method()# Also ok
32+
33+
# Child.class_method()
34+
sample_object.class_method()# Also ok
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
# class Parent:
48+
# @classmethod
49+
# def display(cls):
50+
# print(f"The display() method is called from {cls}")
51+
#
52+
# class Child(Parent):
53+
# pass
54+
#
55+
# sample_object=Parent()
56+
# sample_object.display()
57+
# sample_object=Child()
58+
# sample_object.display()

chapter01/ch01_d01_hello_world.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello World!")

chapter01/ch01_d02_comments.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Testing whether 2 is greater than 1
2+
print(2>1)
3+
4+
x=5 # Assigning 5 to x
5+
x = x + 2 # Adjusting the border
6+
7+
'''
8+
I am using multi-line comments using three single quotes.
9+
However, it is not recommended.
10+
These are common in classes, functions, or modules.
11+
'''
12+
13+
"""
14+
I am using multi-line comments using three double quotes.
15+
These are common in classes, functions, or modules.
16+
"""
17+
18+
# Now I'm showing multiple single-line comments
19+
# Multiplying 5 with 25
20+
# And printing the result
21+
print(5*25)

chapter01/ch01_exercises.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
print("Exercise-1.1")
2+
print("*")
3+
print("**")
4+
print("***")
5+
6+
print("-"*10)
7+
8+
print("Exercise-1.2")
9+
print("The sum of 10,15 and 25.5 is as follows:")
10+
print(10+15+25.5)
11+
12+
print("-"*10)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# age=21
2+
# age
3+
4+
print("Assigning multiple variables in a single line")
5+
age, students=21,5
6+
7+
print(age)
8+
print(students)
9+
print("***********")
10+
print("Assigning same Value to multiple variables")
11+
x=y=z=5
12+
print(x)
13+
print(y)
14+
print(z)
15+
16+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
x=25
2+
y=50
3+
print("Initial values of x and y:")
4+
print(x)
5+
print(y)
6+
x=y # Assigning the current value of y to x
7+
# y=x # Assigning the current value of x to y
8+
print("After the assignment, x and y are as follows:")
9+
print(x)
10+
print(y)

0 commit comments

Comments
 (0)