Found 27104 Articles for Server Side Programming

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

Gireesha Devara
Updated on 23-Aug-2023 18:19:54

782 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. Syntax eval(expression[, globals[, locals]]) Parameters expression: It’s a string that will be evaluated as a Python expression. globals: An optional parameter, which is a dictionary containing global parameters. locals: An optional parameter, which is a dictionary containing local parameters. Return: Returns the result evaluated from the expression. If the string containing arithmetic expression If we pass a string ... Read More

How can I convert Python strings into tuple?

Gireesha Devara
Updated on 23-Aug-2023 18:08:29

3K+ Views

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

What does the Double Star operator mean in Python?

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

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

How to get the size of a list in Python?

Vikram Chiluka
Updated on 09-Sep-2023 15:32:03

5K+ Views

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. In this article, we will show you how to get the size/length of a list in different ways using Python. Here we see 4 methods − Using len() function Using For Loop (Naïve Method) Using length_hint() function Using __len__() function Assume we have taken a list containing some elements. We will return the length/size of the given input list using different methods as specified above. Method 1: ... Read More

What does the Star operator mean in Python?

Gireesha Devara
Updated on 09-Sep-2023 22:56:14

18K+ Views

The asterisk (*) operator in Python has more than one meaning attached to it. We can use it as a Multiplication operator, Repetition operator, used for Unpacking the iterables, and Used as function *args. Single asterisk as used in function declaration allows variable number of arguments passed from 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 on numeric operands. Example a ... Read More

How to create a dictionary with list comprehension in Python?

Gireesha Devara
Updated on 24-Aug-2023 14:30:19

411 Views

By using the dict() method in python we can create a python dictionary with the list comprehension. The syntax of dict() method is given below. Following is the syntax for this 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 ... Read More

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

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

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

449 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

604 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

How to pass Radio Button Data to Python CGI script?

Rajendra Dharmkar
Updated on 16-Jun-2020 12:16:41

790 Views

Passing Radio Button Data to CGI ProgramRadio Buttons are used when only one option is required to be selected.Here is example HTML code for a form with two radio buttons − Maths Physics The result of this code is the following form − Maths  Physics Select SubjectBelow is radiobutton.py script to handle input given by web browser for radio button −#!/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('subject'):    subject = form.getvalue('subject') else:    subject = "Not set" print "Content-type:text/html\r\r" print ... Read More

Advertisements