Server Side Programming Articles - Page 2548 of 2650

How to find cosine of an angle using Python?

Priya Pallavi
Updated on 17-Jun-2020 13:19:23

2K+ Views

Python comes with an amazing standard math library that provides all trigonometric functions like sin, cos, etc. You can import that library and use these functions. Note that these functions expect angles in radians. For example,import math angle1 = math.radians(90) angle2 = math.radians(60) print(math.cos(angle1)) print(math.cos(angle2))This will give the output:6.123233995736766e-17 0.5The first value is very close to zero. Such errors come due to computation limits.

How to generate sequences in Python?

SaiKrishna Tavva
Updated on 27-Feb-2025 16:38:42

2K+ Views

A sequence is a positionally ordered collection of items, where each item can be accessed using its index number. The first element's index starts at 0. We use square brackets [] with the desired index to access an element in a sequence. If the sequence contains n items, the last item is accessed using the index n-1. In Python, there are built-in sequence types such as lists, strings, tuples, ranges, and bytes. These sequence types are classified into mutable and immutable. The mutable sequence types are those whose data can be changed after creation such, as list and byte arrays. ... Read More

How to use single statement suite with Loops in Python?

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

401 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 4

Python: Can not understand why I am getting the error: Can not concatenate 'int' and 'str' object

Ayush Gupta
Updated on 12-Mar-2020 12:31:58

123 Views

This error is coming because the interpreter is replacing the %d with i and then trying to add 1 to a str, ie, adding a str and an int. In order to correct this, just surround the i+1 in parentheses. exampleprint("\ Num %d" % (i+1))

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

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

221 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 do twos complement on a 16-bit signal using Python?

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

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

178 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.

How can I represent python tuple in JSON format?

Sindhura Repala
Updated on 17-Apr-2025 11:23:57

27K+ Views

In this article, we will demonstrate how to represent a tuple in Python using JSON format. We will explore the following methods throughout the 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. ... Read More

How can I convert Python tuple to C array?

Vikram Chiluka
Updated on 14-Apr-2025 15:21:18

10K+ Views

In this article, we will show you how to convert a Python tuple to a C array. Python does not have a built-in array data type like other programming languages, but you can create an array using a library like Numpy. Tuple to Array Conversion Using Python The Python NumPy library provides various methods to create manipulate and modify arrays in Python Following are the two important methods that helps us to convert a tuple into an array − Using numpy.asarray() method Use numpy.array() method If you haven't already installed NumPy on your system, run the following ... Read More

How can I convert bytes to a Python string?

Vikram Chiluka
Updated on 01-Sep-2025 14:48:51

11K+ Views

In this article, we will show you how to convert bytes into a string in Python. Strings are sequences of characters, while bytes are 8-bit values. To convert a sequence of characters to 8-bit values, we can utilize Python's built-in methods, which we will explore in this article. Converting Bytes to Strings in Python The following methods can be efficiently used to convert bytes to Python string. Using decode() function Using str() function Using codecs.decode() function Using pandas library ... Read More

Advertisements