Found 10805 Articles for Python

Partial Functions in Python?

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

1K+ Views

Everybody loves to write reusable code, right? Then partial function is a cool thing to learn. Partial functions allows us to derive a function with x parameters to a function with fewer parameters and constant values set for the more limited function.We can write partial functional application in python through functools library. Below is a simple example of partial function from the functools library with the add function from the operator library.>>> from functools import * >>> from operator import * >>> add(1, 2) 3 >>> add1 = partial(add, 4) >>> add1(6) 10 >>> add1(10) 14Partial is a higher order ... Read More

Changing Class Members in Python?

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

432 Views

Python object oriented programming allows variables to be used at the class level or the instance level where variables are simply the symbols denoting value you’re using in the program. At the class level, variables are referred to as class variables whereas variables at the instance level are referred to as instance variables. Let’s understand the class variable and instance variable through a simple example −# Class Shark class Shark: animal_type= 'fish' # Class Variable def __init__(self, name, age): self.name = name self.age = age # Creating objects of Shark class obj1 = Shark("Jeeva", 54) obj2 = Shark("Roli", 45) ... Read More

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

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

397 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?

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

3K+ Views

Python logical operator “and” and “or” can be applied on strings. An empty string returns a Boolean value of False. Let’s first understand the behaviour of these two logical operator “and” and “or”.And operator Return the first falsey value if there are any, else return the last value in the expression or operator: Return the first truthly value if there are any, else return the last value in the expression.OperationResultX and yIf x is false, then y else xX and yIf x is false, then x, else yNot xIf x is false, then true, else falseBelow is is the program to ... Read More

String Formatting in Python using %?

AmitDiwan
Updated on 11-Aug-2022 12:00:10

471 Views

We can easily format a string in Python using %. Following are the Formatters − Integer − %d Float − %f String − %s Hexadecimal − %x Octal − %o Formatting String Variable as integer To format string variable as an integer, use the int() method and set the string as a parameter − Example # string var = '12' #%i - Integer print('Variable as integer = %i' %(int(var))) Output Variable as integer = 12 Formatting String Variable as float To format string variable as a float, use the float() method and set the string ... Read More

Generate all permutation of a set in Python?

AmitDiwan
Updated on 12-Aug-2022 12:24:43

3K+ Views

Arranging all the members of a set into some order or sequence and if the set is already ordered, rearranging (reordering) its elements is called permutation. Generate all permutations using for loop We will generate permutations using for loop − Example def permutFunc(myList): # No permutations for empty list if len(myList) == 0: return [] # Single permutation for only one element if len(myList) == 1: return [myList] # Permutations for more than 1 characters k = [] # Looping for i in range(len(myList)): m = myList[i] res = myList[:i] + myList[i+1:] for p in permutFunc(res): ... Read More

Class or Static Variables in Python?

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

6K+ Views

When we declare a variable inside a class but outside any method, it is called as class or static variable in python. Class or static variable can be referred through a class but not directly through an instance.Class or static variable are quite distinct from and does not conflict with any other member variable with the same name. Below is a program to demonstrate the use of class or static variable −Exampleclass Fruits(object): count = 0 def __init__(self, name, count): self.name = name self.count = count Fruits.count = Fruits.count + count def main(): apples = Fruits("apples", 3); pears = ... Read More

Division Operators in Python?

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

907 Views

Generally, the data type of an expression depends on the types of the arguments. This rule is applied to most of the operators: like when we add two integers ,the result should be an integer. However, in case of division this doesn’t work out well because there are two different expectations. Sometimes we expect division to generate create precise floating point number and other times we want a rounded-down integer result.In general, the python definition of division(/) depended solely on the arguments. For example in python 2.7, dividing 20/7 was 2 because both arguments where integers. However, 20./7 will generate ... Read More

str() vs repr() in Python?

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

2K+ 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

Advertisements