Karthikeya Boyini has Published 2383 Articles

How to add binary numbers using Python?

karthikeya Boyini

karthikeya Boyini

Updated on 17-Jun-2020 14:39:15

437 Views

If you have binary numbers as strings, you can convert them to ints first using int(str, base) by providing the base as 2. Then add the numbers like you'd normally do. Finally convert it back to a string using the bin function. For example, a = '001' b = '011' ... Read More

How to use single statement suite with Loops in Python?

karthikeya Boyini

karthikeya Boyini

Updated on 17-Jun-2020 12:29:42

239 Views

Similar to the if statement syntax, if your while clause consists only of a single statement, it may be placed on the same line as the while header. Here are the syntax and example of a one-line for loop:for i in range(5): print(i)This will give the output:0 1 2 3 ... Read More

Where to put comments in an if...elif..else construct?

karthikeya Boyini

karthikeya Boyini

Updated on 17-Jun-2020 12:02:42

681 Views

You can put comments anywhere in an if...elif...else statement, ie before each of these blocks or within each of these blocks. Note that you cannot put multiline comments before elif and else blocks though, as these comments are actually strings which imply a break in the whole construct. For example, ... Read More

How to do twos complement on a 16-bit signal using Python?

karthikeya Boyini

karthikeya Boyini

Updated on 17-Jun-2020 11:53:54

784 Views

If you want to get an inversion of only first 16 bits of a number, you can take a xor of that number with 65535(16 1s in binary). Forgetting a 2s complement, just add one to the result. For example, Examplea = 3 # 11 in binary b = (a ... Read More

How to convert Javascript dictionary to Python dictionary?

karthikeya Boyini

karthikeya Boyini

Updated on 17-Jun-2020 11:09:37

1K+ Views

Python and javascript both have different representations for a dictionary. So you need an intermediate representation in order to pass data between them. The most commonly used intermediate representation is JSON, which is a simple lightweight data-interchange format.ExampleThe dumps function converts the dict to a string. For example, import json ... Read More

How to sort a nested Python dictionary?

karthikeya Boyini

karthikeya Boyini

Updated on 17-Jun-2020 10:20:50

2K+ Views

If you have a dictionary of the following format:{    'KEY1':{'name':'foo', 'data':1351, 'completed':100},    'KEY2':{'name':'bar', 'data':1541, 'completed':12},    'KEY3':{'name':'baz', 'data':58413, 'completed':18} }And you want to sort by the key, completed within each entry, in a ascending order, you can use the sorted function with a lambda that specifies which key ... Read More

Can you explain what is metaclass and inheritance in Python?

karthikeya Boyini

karthikeya Boyini

Updated on 17-Jun-2020 09:53:05

533 Views

Every class is an object. It's an instance of something called a metaclass. The default metaclass is typed. You can check this using the is instance function. For example, class Foo:    pass foo = Foo() isinstance(foo, Foo) isinstance(Foo, type)This will give the output:True TrueA metaclass is not part ... Read More

Simpson's 1/3 Rule for definite integral

karthikeya Boyini

karthikeya Boyini

Updated on 17-Jun-2020 08:45:38

821 Views

Like the Trapezoidal Rule, Simpson’s 1/3rd rule is also used to find the integral value from the range a to b. The main difference between trapezoidal and the Simpson’s 1/3rd rule is, in the trapezoidal rule, the whole sections are divided into some trapezoids, but in this case, each trapezoid ... Read More

Lucky Numbers

karthikeya Boyini

karthikeya Boyini

Updated on 17-Jun-2020 08:23:49

2K+ Views

Lucky numbers are some special integer numbers. From basic numbers, some special numbers are eliminated by their position. Instead of their value, for their position, the numbers are eliminated. The numbers which are not deleted, they are the lucky numbers.The number deletion follows some rule. At first, every second number ... Read More

Word Wrap Problem

karthikeya Boyini

karthikeya Boyini

Updated on 17-Jun-2020 07:54:57

1K+ Views

A sequence of words is given, there is a limit on the number of characters for each line. By putting line breaks, in such a way that lines are printed clearly.The lines must be balanced, when some lines have lots of extra spaces and some lines are containing a small ... Read More

Advertisements