Found 27104 Articles for Server Side Programming

Python: Cannot understand why the error - cannot concatenate 'str' and 'int' object ?

Jayashree
Updated on 13-Mar-2020 05:15:39

110 Views

This can be corrected by putting n+1 in brackets i.e. (n+1)for num in range(5):     print ("%d" % (num+1))Using %d casts the object following % to string. Since a string object can't be concatnated with a number (1 in this case) interpreter displays typeerror.

How to make loops run faster using Python?

Samual Sam
Updated on 05-Mar-2020 09:55:17

822 Views

This is a language agnostic question. Loops are there in almost every language and the same principles apply everywhere. You need to realize that compilers do most heavy lifting when it comes to loop optimization, but you as a programmer also need to keep your loops optimized.It is important to realize that everything you put in a loop gets executed for every loop iteration. They key to optimizing loops is to minimize what they do. Even operations that appear to be very fast will take a long time if the repeated many times. Executing an operation that takes 1 microsecond ... Read More

What is python .. ("dot dot") notation syntax?

Samual Sam
Updated on 17-Jun-2020 11:58:35

328 Views

There is no special .. ("dot dot") notation syntax in python. You can, however, see this in case of floats accessing their properties. For example,f = 1..__truediv__ # or 1..__div__ for python 2 print(f(8))This will give the output:0.125What we have is a float literal without the trailing zero, which we then access the __truediv__ method of. It's not an operator in itself; the first dot is part of the float value, and the second is the dot operator to access the object's properties and methods. This can also be achieved using:>>> f = 1. >>> f 1.0 >>> f.__truediv__

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

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

778 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 ^ 65535) + 1 print(bin(b))OutputThis will give the output:0b1111111111111101

How to change the look of Python operators?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:22

104 Views

Python and most mainstream languages do not allow changing how operators look. If you're trying to replace something like a == b with a equals b, you can't do that. In Python the restriction is quite intentional — an expression such as a equals b would look ungrammatical to any reader familiar with Python.

What is correct name of * operator available in Python?

Vikram Chiluka
Updated on 09-Nov-2022 07:56:09

145 Views

In this article, we will explain the correct name of the * operator available in python. In Python, you'll encounter the symbols * and ** used frequently. Many Python programmers, especially those at the intermediate level, are confused by the asterisk (*) character in Python. The *args argument is called the "variable positional parameter" and **kwargs is the "variable keyword parameter". The * and ** arguments just unpack their respective data structures.Using Asterisk ( * ) Operator in Multiplication  Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Create two variables and ... Read More

How can I append a tuple into another tuple in Python?

Vikram Chiluka
Updated on 28-Oct-2022 08:11:33

9K+ Views

In this article, we will show you how to append a tuple into another in python. Below are the various methods to accomplish this task − Using + operator. Using sum() function. Using list() & extend() functions. Using the unpacking(*) operator. Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable. Using + operator Algorithm (Steps) Following are the Algorithm/steps to be followed to ... Read More

How can I represent python tuple in JSON format?

Vikram Chiluka
Updated on 28-Oct-2022 11:24:45

21K+ Views

In this article, we will show you how to represent a tuple in python in JSON format. We will see the below-mentioned methods in this article: Converting Python Tuple to JSON Converting Python Tuple with Different Datatypes to JSON String Parsing JSON string and accessing elements using json.loads() method. Convert the dictionary of tuples to JSON using json.dumps() What is JSON? JSON (JavaScript Object Notation) is a simple lightweight data-interchange format that humans can read and write. Computers can also easily parse and generate it. JSON is a computer language that is based on JavaScript. It is a ... Read More

How can I convert Python tuple to C array?

Vikram Chiluka
Updated on 09-Nov-2022 07:28:21

8K+ Views

In this article, we will show you how to convert a Python tuple to a C array in python. Python does not have a built-in array data type like other programming languages, but you can create an array by using a library like Numpy. Below are the various methods to convert a tuple into an array in Python − Using numpy.asarray() method Use numpy.array() method If you have not already installed NumPy on your system, run the following command to do so. pip install numpy Convert tuple to array using numpy.asarray() Use the np.asarray() function to convert ... Read More

How can I convert bytes to a Python string?

Vikram Chiluka
Updated on 28-Oct-2022 08:28:47

11K+ Views

In this article, we will show you how to convert bytes to a string in python. Below are the various methods to accomplish this task − Using decode() function Using str() function Using codecs.decode() function Using pandas library Using decode() function The built-in decode() method in python, is used to convert bytes to a string. Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task –. Create a variable to store the input byte string data. Print input data. Use the type() function(returns the data type of an object) to print the type ... Read More

Advertisements