Found 10417 Articles for Python

How we can create a dictionary from a given tuple in Python?

Pythonista
Updated on 25-Feb-2020 11:14:12

406 Views

We can use zip() function to produce an iterable from two tuple objects, each corresponding to key and value items and then use dict() function to form dictionary object>>> T1=('a','b','c','d') >>> T2=(1,2,3,4) >>> dict((x,y) for x,y in zip(t1,t2))Dictionary comprehension syntax can also be used to construct dictionary object from two tuples>>> d={k:v for (k,v) in zip(T1,T2)} >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4}

How to create a complex number in Python?

Malhar Lathkar
Updated on 24-Feb-2020 10:04:52

169 Views

Complex number is made up of real and imaginary parts. Real part is a float number, and imaginary part is any float number multiplied by square root of -1 which is defined as j.>>> no=5+6j >>> no.real 5.0 >>> no.imag 6.0 >>> type(no) The resulting object is of complex data type. Python library also has complex() function, which forms object from two float arguments>>> no=complex(5,6) >>> no (5+6j) >>> no.real 5.0 >>> no.imag 6.0 >>> type(no)

How do we evaluate a string and return an object in Python?

Gireesha Devara
Updated on 29-May-2025 11:13:15

1K+ Views

By using the eval() function in Python, we can evaluate a string and return a Python object. The eval() is a Python built-in function that evaluates a string argument by parsing the string as a code expression. eval(expression[, globals[, locals]]) Evaluating a String with Arithmetic Expression? If we pass a string containing an arithmetic expression to the eval() function. First, it parses the expression, then evaluates it, and finally returns an evaluated Python object. Example In the following example, the eval() function evaluates the string formed arithmetic expression and returns an integer object - var = 100 string_EXP_1 ... Read More

How can I convert Python strings into tuple?

Gireesha Devara
Updated on 29-May-2025 11:17:09

5K+ Views

Converting Python String into TupleWe can convert a Python string into tuples by simply mentioning a comma (, ) after the string. This will treat the string as a single element to the tuple. Example Here our string variable “s” is treated as one item in the tuple, which can be done by adding the comma after the string - s = "python" print("Input string :", s) t = s, print('Output tuple:', t) print(type(t)) Following is the output of the above code - Input string : python Output tuple: ('python', ) Using tuple() Function Also, ... Read More

What does the Double Star operator mean in Python?

Gireesha Devara
Updated on 09-Sep-2023 15:22:05

10K+ Views

The double star/asterisk (*) operator has more than one meaning in Python. We can use it as a exponential operator, used as function *kwargs, unpacking the iterables, and used to Merge the Dictionaries. Exponential operator For numeric data the double asterisk (**) is used as an exponential operator. Let's take an example and see how the double star operator works on numeric operands. Example The following example uses double asterisks/star (**) to calculate “a to the power b” and it works equivalent to the pow() function. a = 10 b = 2 result = a ** b print("a**b = ", ... Read More

What does the Star operator mean in Python?

Gireesha Devara
Updated on 30-Apr-2025 17:40:21

21K+ Views

The asterisk (*) operator in Python has more than one meaning attached to it. We can use it as a multiplication operator, a repetition operator, for unpacking the iterables, and as a function *args. A single asterisk, as used in a function declaration, allows a variable number of arguments to be passed from the calling environment. Inside the function, it behaves as a tuple. As the multiplication operator Generally, the start (*) operator is used for multiplication purposes. For numeric data, the asterisk (*) is used as a multiplication operator. Let’s take an example and see how the star operator works ... Read More

How to create a dictionary with list comprehension in Python?

Gireesha Devara
Updated on 29-May-2025 11:37:20

707 Views

By using the dict() method in Python, we can create a dictionary with the list comprehension. Following is the syntax of dict() method- dict(**kwarg) Keyword arguments. We can pass one or more keyword arguments. If no keyword argument is passed, then the dict() method will create an empty dictionary object. The syntax for creating a dictionary with list comprehension: dict(list_comprehension) Creating Dictionary using List Comprehension Instead of sending a number of keywords here, we need to send a list of tuples with key-value pairs to the dict() method. Let’s take an example and create a dictionary using a ... Read More

What does ** (double star) and * (star) do for parameters in Python?

Gireesha Devara
Updated on 09-Sep-2023 22:59:21

6K+ Views

While creating a function the single asterisk (*) defined to accept and allow users to pass any number of positional arguments. And in the same way the double asterisk (**) defined to accept any number of keyword arguments. The single asterisk (*) can be used when we are not sure how many arguments are going to be passed to a function and those arguments that are not keywords. The double asterisk (**kwargs) can be used to pass keywords, when we don't know how many keyword arguments will be passed to a function, which will be in a dict named ... Read More

How to pass Drop Down Box Data to Python CGI script?

Rajendra Dharmkar
Updated on 25-Jun-2020 21:12:50

534 Views

Passing Drop Down Box Data to CGI ProgramDrop Down Box is used when we have many options available but only one or two will be selected.Here is example HTML code for a form with one drop down box − Maths Physics The result of this code is the following form − SubmitBelow is dropdown.py script to handle input given by web browser.#!/usr/bin/python # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form ... Read More

How to pass Text Area Data to Python CGI script?

Rajendra Dharmkar
Updated on 16-Jun-2020 12:18:43

779 Views

Passing Text Area Data to CGI ProgramTEXTAREA element is used when multiline text has to be passed to the CGI Program.Here is example HTML code for a form with a TEXTAREA box − Type your text here... The result of this code is the following form −Type your text here...  SubmitBelow is textarea.cgi script to handle input given by web browser −#!/usr/bin/python # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields if form.getvalue('textcontent'):    text_content = form.getvalue('textcontent') else:    text_content = "Not entered" print ... Read More

Advertisements