Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Jayashree
19 articles
Python: Cannot understand why the error - cannot concatenate 'str' and 'int' object ?
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 MoreHow will you explain Python Operator Overloading?
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 MoreHow do I find the largest integer less than x in Python?
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 MoreWhat is @ operator in Python?
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 MoreHow to know Maximum and Minimum values for ints in Python?
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 MoreHow to print a value for a given key for Python dictionary?
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 MoreHow to Find Factors of Number using Python?
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 MoreHow to create Python dictionary by enumerate function?
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 MoreHow to find the value closest to negative infinity in Python?
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 MoreHow to find the value closest to positive infinity in Python?
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