Found 10476 Articles for Python

Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?

AmitDiwan
Updated on 12-Aug-2022 12:29:20

554 Views

In this example, we will count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix. At first, we will create a matrix − mat = [ [-1, 3, 5, 7], [-6, -3, 1, 4], [-5, -1, -10, 12] ] Pass the matrix to a custom function and use nested for loop − def negativeFunc(mat, r, c): count = 0 for i in range(r): for j in range(c): if mat[i][j]

Logical Operators on String in Python?

Yaswanth Varma
Updated on 28-Aug-2025 13:03:49

3K+ Views

In this article, we are going to learn about the logical operators on strings in Python. The logical operators such as and, or, and not are used to combine the conditional statements and control the flow of logic. While they are commonly used with the Boolean values, they can also be used with strings and other non-Boolean types.Logical Operators on String in Python? In Python the non-empty strings are considered as true, and the Empty strings ("") are considered as false. When the logical operators are applied to the strings, they do not return Boolean values directly; instead, they return one ... Read More

Generate all permutation of a set in Python?

SaiKrishna Tavva
Updated on 17-Dec-2024 13:06:16

5K+ Views

Permutations refer to arranging the members of a set into different sequences. if the set is already ordered, rearranging (reordering) its elements. If a set has n elements, the total number of permutations is n! (factorial of n) Some common approaches we can use to generate all permutations of a set in Python are as follows. Using a for loop Using itertools.permutations() function. ... Read More

Class or Static Variables in Python?

Yaswanth Varma
Updated on 28-Aug-2025 13:08:36

6K+ Views

In this article we are going to learn about class or static variables on python. The class variables or static variables that are used to share across all the instances of a class. Unlike the instance variables which are specific to each object, Class variables maintain the common value for all the objects of the class. They are defined within the class but outside any instance methods, making them accessible to all the instances. These variables are used when we want to have a single shared state or value among all instance. Example 1 Let's look ... Read More

Division Operators in Python?

Akshitha Mote
Updated on 22-Jan-2025 13:41:32

1K+ Views

The Python division operator is used to divide two integer values and return a quotient. To perform a division operation, we use the / symbol. For example, 16 divided by 4 is written as 16 / 4, which returns 4 as the quotient. Here, 16 is known as divident and 4 is known as divisor. The division operator is one of the arithmetic operator. When we divide an integer by zero, it will raise a ZeroDivisionError exception in Python. For example, if we try to divide 55 by 0 using 55 / 0, it will raise a ZeroDivisionError exception. Types ... Read More

str() vs repr() in Python?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

3K+ Views

Both str() and repr() methods in python are used for string representation of a string. Though they both seem to serve the same puppose, there is a little difference between them.Have you ever noticed what happens when you call a python built-in function str(x) where x is any object you want? The return value of str(x) depends on two methods: __str__ being the default choice and __repr__ as a fallback.Let’s first see what python docs says about them −>>> help(str) Help on class str in module builtins: class str(object) | str(object='') -> str | str(bytes_or_buffer[, encoding[, errors]]) -> str ... Read More

How to input multiple values from user in one line in Python?

AmitDiwan
Updated on 12-Aug-2022 12:44:02

5K+ Views

In Python, to get values from users, use the input(). This works the same as scanf() in C language. Input multiple values from a user in a single line using input() To input multiple values in one line, use the input() method − x, y, z = input(), input(), input() Let’s say the user entered 5, 10, 15. Now, you can display them one by one − print(x) print(y) print(z) Output 5 10 15 From above output, you can see, we are able to give values to three variables in one line. To avoid using multiple input() methods(depends on ... Read More

Differences between Python 2.x and Python 3.x?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

There is always a debate in the coding community on which python version was the best one to learn: Python 2.x or Python 3.x.Below are key differences between pyton 2.x and python 3.x1. The print functionIn python 2.x, “print” is treated as a statement and python 3.x explicitly treats “print” as a function. This means we need to pass the items inside your print to the function parentheses in the standard way otherwise you will get a syntax error.#Python 2.7 print 'Python', python_version() print 'Hello, World!' print('Hello, World!') print "text", ; print 'some more text here'OutputPython 2.7.6 Hello, World! ... Read More

Packing and Unpacking Arguments in Python?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

1K+ Views

If you have done little-bit of programming in python, you have seen the word “**args” and “**kwargs” in python functions. But what exactly they are?The * and ** operators did different operations which are complementary to each other depending on where we are using them.So when we are using them in method definition, like -def __init__(self, *args, **kwargs): passAbove operation is called ‘packing” as it pack all the arguments into one single variable that this method call receives into a tuple called args. We can use name other than args, but args is the most common and pythonic way of ... Read More

Mathematical Functions in Python?

George John
Updated on 30-Jul-2019 22:30:26

3K+ Views

From doing simple to complex mathematical operations(like trigonometric, logarithmic operations etc) in python we may need to use the math() module.The python math module is used to access mathematical functions. All methods of math() function are used for integer or real type objects but not for complex numbers.To use this function, we need to import it in our codeimport mathConstantsWe use these constants for calculation in python -ConstantsDescriptionsPiReturn the value of pi: 3.141592EReturn the value of natural base e. e is 0.718282tauReturns the value of tau. tau = 6.283185infReturns the infinitenanNot a number typeNumbers and Numeric RepresentationPython provides different functions ... Read More

Advertisements