Jayashree

Jayashree

19 Articles Published

Articles by Jayashree

19 articles

Python: Cannot understand why the error - cannot concatenate 'str' and 'int' object ?

Jayashree
Jayashree
Updated on 24-Mar-2026 274 Views

The error "cannot concatenate 'str' and 'int' object" occurs when you try to combine a string and an integer without proper type conversion. This happens because Python doesn't automatically convert between string and integer types during concatenation. Understanding the Error When you use string formatting with %d, Python expects an integer value. If you try to perform arithmetic operations directly in the format string without proper grouping, it can cause concatenation issues. Incorrect Approach # This might cause issues with operator precedence for num in range(5): print("%d" % num+1) # ...

Read More

How will you explain Python Operator Overloading?

Jayashree
Jayashree
Updated on 24-Mar-2026 324 Views

Python operator overloading allows you to define custom behavior for built-in operators when used with user-defined classes. Every class in Python inherits from the object class, which contains special methods (also called magic methods or dunder methods) that correspond to various operators. These special methods have names surrounded by double underscores, like __add__(), __sub__(), __eq__(), etc. By overriding these methods in your class, you can define how operators work with your objects. Common Operator Overloading Methods Here are the most frequently used magic methods for operator overloading − Operator Magic Method Description ...

Read More

How do I find the largest integer less than x in Python?

Jayashree
Jayashree
Updated on 24-Mar-2026 3K+ Views

In this article, we will show you how to find the largest integer less than or equal to x in Python using the floor function. The Greatest Integer Function [x] denotes the integral part of a real number x that is the closest and smallest integer to x. It's also called the floor function. [x] = the largest integer less than or equal to x Understanding the Floor Function If n ≤ x < n+1 where n is an integer, then [x] = n. This means if x lies in the interval [n, n+1), ...

Read More

What is @ operator in Python?

Jayashree
Jayashree
Updated on 24-Mar-2026 902 Views

The @ operator in Python serves multiple purposes. It's primarily used to define decorators, but it also functions as the matrix multiplication operator (introduced in Python 3.5). Let's explore both uses with practical examples. @ Operator as Decorator A decorator is a function that takes another function and extends its behavior without explicitly modifying it. The @ symbol provides clean syntax for applying decorators ? Basic Decorator Example def my_decorator(func): def wrapper(): print("Before function call") ...

Read More

How to know Maximum and Minimum values for ints in Python?

Jayashree
Jayashree
Updated on 24-Mar-2026 193 Views

Python's core library provides built-in functions max() and min() to find maximum and minimum values from sequences like lists, tuples, or multiple arguments. Additionally, Python offers ways to discover the system limits for integer values. Using max() and min() Functions With Multiple Arguments You can pass multiple numbers directly to max() and min() − print(max(23, 21, 45, 43)) print(min(23, 21, 45, 43)) 45 21 With Lists and Tuples These functions also work with sequence objects like lists and tuples − numbers_list = [20, 50, 40, 30] numbers_tuple ...

Read More

How to print a value for a given key for Python dictionary?

Jayashree
Jayashree
Updated on 24-Mar-2026 8K+ Views

Python dictionary is a collection of key-value pairs. There are several ways to print or access the value associated with a specific key. Using the get() Method The get() method returns the value for a given key. If the key doesn't exist, it returns None by default ? D1 = {'a': 11, 'b': 22, 'c': 33} print(D1.get('b')) print(D1.get('d')) # Key doesn't exist 22 None Using get() with Default Value You can provide a default value to return when the key is not found ? D1 = {'a': ...

Read More

How to Find Factors of Number using Python?

Jayashree
Jayashree
Updated on 09-Sep-2023 12K+ Views

In order to find factors of a number, we have to run a loop over all numbers from 1 to itself and see if it is divisible.Examplenum=int(input("enter a number")) factors=[] for i in range(1,num+1):     if num%i==0:        factors.append(i) print ("Factors of {} = {}".format(num,factors))If i is able to divide num completely, it is added in the list. Finally the list is displayed as the factors of given numberOutputenter a number75 Factors of 75 = [3, 5, 15, 25, 75]

Read More

How to create Python dictionary by enumerate function?

Jayashree
Jayashree
Updated on 09-Sep-2023 5K+ Views

Python enumerate() function takes any iterable as argument and returns enumerate object using which the iterable can be traversed. It contains index and corresponding item in the iterable object like list, tuple, or string.Such enumerate object with index and value is then converted to a dictionary using dictionary comprehension.>>> l1=['aa','bb','cc','dd'] >>> enum=enumerate(l1) >>> enum >>> d=dict((i,j) for i,j in enum) >>> d {0: 'aa', 1: 'bb', 2: 'cc', 3: 'dd'} Learn Python with our step-by-step Python Tutorial.

Read More

How to find the value closest to negative infinity in Python?

Jayashree
Jayashree
Updated on 02-Mar-2020 246 Views

Although infinity doesn't have a concrete representation, the closest number to negative infinity is represented as return value of float() function with '-inf' as argument>>> a=float('-inf') >>> -inf

Read More

How to find the value closest to positive infinity in Python?

Jayashree
Jayashree
Updated on 02-Mar-2020 283 Views

Although infinity doesn't have a concrete representation, the closest number to infinity is represented as return value of float() function with 'inf' as argument>>> a=float('inf') >>> a inf

Read More
Showing 1–10 of 19 articles
« Prev 1 2 Next »
Advertisements