Found 10476 Articles for Python

Boolean Indexing in Python

Pradeep Elance
Updated on 20-Dec-2019 11:26:46

2K+ Views

The Boolean values like True & false and 1&0 can be used as indexes in panda dataframe. They can help us filter out the required records. In the below exampels we will see different methods that can be used to carry out the Boolean indexing operations.Creating Boolean IndexLet’s consider a data frame desciribing the data from a game. The various points scored on different days are mentioned in a dictionary. Then we can create an index on the dataframe using True and False as the indexing values. Then we can print the final dataframe.Example Live Demoimport pandas as pd # dictionary ... Read More

html5lib and lxml parsers in Python

Pradeep Elance
Updated on 20-Dec-2019 11:19:19

743 Views

html5lib is a pure-python library for parsing HTML. It is designed to conform to the WHATWG HTML specification, as is implemented by all major web browsers. It can parse almost all the elements of an HTML doc, breaking it down into different tags and pieces which can be filtered out for various use cases. It parses the text the same way as done by the major browsers. It can also tackle broken HTML tags and add some necessary tags to complete the structure. Also it is written in pure python code.lxml is also a similar parser but driven by XML ... Read More

How to assign values to variables in Python

Pradeep Elance
Updated on 20-Dec-2019 11:07:16

4K+ Views

Variable assignment is a very basic requirement in any computer programming language. In python there are multiple ways we can declare a variable and assign value to it. Below we see each of them.Direct InitialisationIn this method, we directly declare the variable and assign a value using the = sign. If the variable is declare multiple times, then the last declaration’s value will be used by the program.Examplex = 5 x = 9 print(a)Running the above code gives us the following result:Output9Using if-elseWe can initialize value of a variable using some conditions. The evaluation of the result of the condition ... Read More

Generating hash ids using uuid3() and uuid5() in Python

Pradeep Elance
Updated on 20-Dec-2019 10:40:51

2K+ Views

The universally unique identifier is a 32 bit hexadecimal number that can guarantee a unique value in a given namespace. This helps in tracking down objects created by a program or where ever python needs to handle object or data that needs large value of identifier. The UUID class defines functions that can create these values.Syntaxuuid3(namespace, string) uuid3 usesMD5 hash value to create the identifier. Uuid5(namespace, string) Uuid5 uses SHA-1 hash value to create the identifier. The namespace can be – NAMESPACE_DNS : Used when name string is fully qualified domain name. NAMESPACE_URL : Used when name string is ... Read More

Python Program to print all permutations of a given string

Pavitra
Updated on 20-Dec-2019 07:34:07

614 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string we need to display all the possible permutations of the string.Now let’s observe the solution in the implementation below −Example Live Demo# conversion def toString(List):    return ''.join(List) # permutations def permute(a, l, r):    if l == r:       print (toString(a))    else:       for i in range(l, r + 1):          a[l], a[i] = a[i], a[l]          permute(a, l + 1, r)          a[l], ... Read More

Python program to find uncommon words from two Strings

Pavitra
Updated on 20-Dec-2019 07:31:43

685 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given two strings, we need to get the uncommon words from the given strings.Now let’s observe the solution in the implementation below −Example Live Demo# uncommon words def find(A, B):    # count    count = {}    # insert in A    for word in A.split():       count[word] = count.get(word, 0) + 1    # insert in B    for word in B.split():       count[word] = count.get(word, 0) + 1    # return ans    return [word ... Read More

Python program to find number of local variables in a function

Pavitra
Updated on 20-Dec-2019 07:29:18

942 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a function, we need to display the number of local variables in the function.Now let’s observe the solution in the implementation below −Example Live Demo# checking locals def scope():    a = 25.5    b = 5    str_ = 'Tutorialspoint' # main print("Number of local varibales available:", scope.__code__.co_nlocals)OutputNumber of local varibales available: 3Example Live Demo# checking locals def empty():    pass def scope():    a, b, c = 9, 23.4, True    str = 'Tutiorialspoint' # main print("Number of local varibales ... Read More

Python Program to Count trailing zeroes in factorial of a number

Pavitra
Updated on 20-Dec-2019 07:24:23

825 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an integer n, we need to count the number of trailing zeros in the factorial.Now let’s observe the solution in the implementation below −Example Live Demo# trailing zero def find(n):    # Initialize count    count = 0    # update Count    i = 5    while (n / i>= 1):       count += int(n / i)       i *= 5    return int(count) # Driver program n = 79 print("Count of trailing 0s "+"in", n, ... Read More

Python Program to Count set bits in an integer

Pavitra
Updated on 20-Dec-2019 07:22:44

454 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an integer n, we need to count the number of 1’s in the binary representation of the numberNow let’s observe the solution in the implementation below −#naive approachExample Live Demo# count the bits def count(n):    count = 0    while (n):       count += n & 1       n >>= 1    return count # main n = 15 print("The number of bits :", count(n))OutputThe number of bits : 4#recursive approachExample Live Demo# recursive way def count( ... Read More

Python Program to Count number of binary strings without consecutive 1’

Pavitra
Updated on 20-Dec-2019 07:20:06

291 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a positive integer N, we need to count all possible distinct binary strings available with length N such that no consecutive 1’s exist in the string.Now let’s observe the solution in the implementation below −Example Live Demo# count the number of strings def countStrings(n):    a=[0 for i in range(n)]    b=[0 for i in range(n)]    a[0] = b[0] = 1    for i in range(1, n):       a[i] = a[i-1] + b[i-1]       b[i] = ... Read More

Advertisements