Code:
h = [5, 6, 7, 8]
h.pop()
h.pop(0)
print(h)
Python Coding May 19, 2024 Python Coding Challenge No comments
h = [5, 6, 7, 8]
h.pop()
h.pop(0)
print(h)
Python Coding May 19, 2024 Python Coding Challenge No comments
This happens when you try to divide a number by zero. Always check the divisor before dividing.
# Correct way
result = 10 / 2
# Incorrect way
result = 10 / 0 # ZeroDivisionError
#clcoding.com
Python uses indentation to define code blocks. This error occurs when there’s a misalignment in the indentation.
# Correct way
if True:
print("Hello")
# Incorrect way
if True:
print("Hello") # IndentationError
#clcoding.com
Python Coding May 17, 2024 Python Coding Challenge No comments
g = [1, 2, 3]
h = [1, 2, 3]
print(g is h)
print(g == h)
Python Coding May 17, 2024 Python Coding Challenge No comments
str_a = "hello"
str_b = "hello"
print(str_a is str_b)
print(str_a == str_b)
Python Coding May 16, 2024 Python Coding Challenge No comments
dict_a = {"a": 1, "b": 2}
dict_b = {"a": 1, "b": 2}
print(dict_a is dict_b)
print(dict_a == dict_b)
This code creates two dictionaries, dict_a and dict_b, with identical key-value pairs. Then it prints the results of two different comparisons:
print(dict_a is dict_b): This checks whether dict_a and dict_b refer to the same object in memory. In this case, they are two separate dictionary objects, so the output will be False.
print(dict_a == dict_b): This checks whether the contents of dict_a and dict_b are the same. Since they have the same key-value pairs, the output will be True.
Python Coding May 14, 2024 Python Coding Challenge No comments
num = [5, 6]
*midd, lst = num, num[-1]
print(midd, lst)
Let's break down the code step by step:
num = [5, 6]: This line creates a list called num containing two integers, 5 and 6.
*midd, lst = num, num[-1]: Here, we're using extended iterable unpacking. Let's dissect this line:
*midd: The * operator is used to gather any remaining items in the iterable (in this case, the list num) into a list. So, midd will contain all elements of num except the last one.
, lst: This part assigns the last item of the num list to the variable lst. In this case, it's assigning 6 to lst.
Therefore, after this line executes, midd will be [5] and lst will be 6.
print(midd, lst): This line prints the variables midd and lst. So, it will output [5] 6.
So, overall, the code snippet initializes a list num with two elements [5, 6], then it unpacks this list into two variables: midd, which contains all elements of num except the last one ([5]), and lst, which contains the last element of num (6). Finally, it prints the values of midd and lst.
Python Coding May 13, 2024 Python Coding Challenge No comments
num = [7, 8, 9]
*mid, last = num[:-1]
print(mid, last)
Python Coding May 12, 2024 Python Coding Challenge No comments
num = [1, 2, 3]
*middle, last = num
print(middle, last)
Python Coding May 11, 2024 Python Coding Challenge No comments
Python Coding May 04, 2024 Python Coding Challenge No comments
class Powerizer(int):
def __pow__(self, other):
return super().__pow__(other ** 2)
p = Powerizer(2)
result = p ** 3
print(result)
Python Coding May 04, 2024 Python Coding Challenge No comments
class Decrementer(int):
def __sub__(self, other):
return super().__sub__(other - 1)
d = Decrementer(5)
result = d - 3
print(result)
Class Definition:
class Decrementer(int):
def __sub__(self, other):
return super().__sub__(other - 1)
Decrementer(int): This line creates a class called Decrementer which inherits from the int class. Instances of Decrementer will inherit all the properties and methods of integers.
def __sub__(self, other): This method overrides the subtraction behavior (__sub__) for instances of the Decrementer class. It is called when the - operator is used with instances of Decrementer.
return super().__sub__(other - 1): Inside the __sub__ method, it subtracts 1 from the other operand and then calls the __sub__ method of the superclass (which is int). It passes the modified other operand to the superclass method. Essentially, it performs subtraction of the Decrementer instance with the modified value of other.
Object Instantiation:
d = Decrementer(5)
This line creates an instance of the Decrementer class with the value 5.
Subtraction Operation:
result = d - 3
This line performs a subtraction operation using the - operator. Since d is an instance of Decrementer, the overridden __sub__ method is invoked. The value 3 is passed as other. Inside the overridden __sub__ method, 1 is subtracted from other, making it 2. Then, the superclass method (int.__sub__) is called with the modified other value. Essentially, it subtracts 2 from d, resulting in the final value.
print(result)
This line prints the value of result, which is the result of the subtraction operation performed in the previous step.
So, the output of this code will be 3, which is the result of subtracting 3 - 1 from 5.
Python Coding May 04, 2024 Python Coding Challenge No comments
class Incrementer(int):
def __add__(self, other):
return super().__add__(other + 1)
i = Incrementer(5)
result = i + 3
print(result)
Python Coding May 04, 2024 Python Coding Challenge No comments
Python Coding May 04, 2024 Python Coding Challenge No comments
Python Coding May 04, 2024 Python Coding Challenge No comments
class Doubler(int):
def __mul__(self, other):
return super().__mul__(other + 3)
d = Doubler(3)
result = d * 5
print(result)
Python Coding May 04, 2024 Python Coding Challenge No comments
Python Coding May 04, 2024 Python Coding Challenge No comments
Python Coding May 03, 2024 Python Coding Challenge No comments
class Doubler(int):
def __mul__(self, other):
return super().__mul__(other * 2)
# Create an instance of Doubler
d = Doubler(3)
# Multiply by another number
result = d * 5
print(result)
Python Coding May 02, 2024 Python Coding Challenge No comments
class MyClass:
def __init__(self, x):
self.x = x
def __call__(self, y):
return self.x * y
p1 = MyClass(2)
print(p1(3))
Python Coding May 01, 2024 Python Coding Challenge No comments
class MyClass:
def __init__(self, x):
self.x = x
p1 = MyClass(1)
p2 = MyClass(2)
p1.x = p2.x
del p2.x
print(p1.x)
Free Books Python Programming for Beginnershttps://t.co/uzyTwE2B9O
— Python Coding (@clcoding) September 11, 2023
Top 10 Python Data Science book
— Python Coding (@clcoding) July 9, 2023
🧵:
Top 4 free Mathematics course for Data Science ! pic.twitter.com/s5qYPLm2lY
— Python Coding (@clcoding) April 26, 2024
Web Development using Python
— Python Coding (@clcoding) December 2, 2023
🧵: