Python Articles - Page 972 of 1048

What are important HTTP headers to be frequently used in Python CGI Programming?

Rajendra Dharmkar
Updated on 16-Jun-2020 12:25:28

358 Views

HTTP HeaderThe line Content-type:text/html\r\r is part of HTTP header which is sent to the browser to understand the content. All the HTTP header will be in the following form −HTTP Field Name − Field ContentFor ExampleContent-type − text/html\r\rThere are few other important HTTP headers, which we will use frequently in our CGI Programming.     Sr.No.HeaderDescription1Content-type:A MIME string defining the format of the file being returned. Example is Content-type:text/html2Expires: DateThe date the information becomes invalid. It is used by the browser to decide when a page needs to be refreshed. A valid date string is in the format 01 Jan 1998 ... Read More

What Content-type is required to write Python CGI program?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

368 Views

If we run simple scripts like hello.py, its output is written on the STDOUT file, i.e., screen. There is one important and extra feature available which is the first line to be printed Content-type:text/html\r\r. This line is sent back to the browser and it specifies the content type to be displayed on the browser screen.We can write advanced CGI programs using Python. This script can interact with any other external system and even exchange information with RDBMS.

What are the modules required for CGI programming in Python?

Rajendra Dharmkar
Updated on 16-Jun-2020 12:21:40

287 Views

Python's cgi module is usually the starting place in writing CGI programs in Python. The main purpose of the cgi module is to extract the values passed to a CGI program from an HTML form. Mostly one interacts with CGI applications through an HTML form. One fills out some values in the form that specify details of the action to be performed, then call on the CGI to perform its action using your specifications.You may include many input fields within an HTML form which can be of a number of different types (text, checkboxes, picklists, radio buttons, etc.).Your Python script ... Read More

How to flatten a shallow list in Python?

Gireesha Devara
Updated on 29-May-2025 10:37:14

2K+ Views

Flattening a shallow list means converting a nested list into a simple, single-dimensional list. In other words, converting a multidimensional list into a one-dimensional list. Flattening can be performed using different techniques like nested for loops, list comprehensions, list concatenation, and using built-in functions. In this article, we will discuss a few techniques to flatten a shallow Python list. Flattening a shallow list using a nested for loop By using a nested for loop and a list.append() method, we can flatten the shallow list. Let’s have a look and see how this can be done in a program. Example This simple example ... Read More

How to clone or copy a list in Python?

Gireesha Devara
Updated on 29-May-2025 10:40:20

2K+ Views

The list in Python is a sequence data type that is used to store various types of data. A list is created by placing each data element inside square brackets "[]" and these are separated by commas. In Python, the assignment operator doesn’t create a new object; rather, it gives another name to an already existing object. This can be verified by id() function >>> L1 = [1, 2, 3, 4] >>> L2 = L1 >>> id(L1) 185117137928 >>> id(L2) 185117137928 There are various ways of cloning/copying a list in Python. In this article, we will discuss some ... Read More

How can I convert a Python tuple to string?

Gireesha Devara
Updated on 29-May-2025 10:44:05

11K+ Views

A tuple is a collection of objects that is ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are that tuples cannot be changed, unlike lists, and tuples use parentheses, whereas lists use square brackets. Converting a Python tuple to a StringThere are three different ways we can convert a Python tuple to a string. Using a for loop. Using the Python join() method ... Read More

What is the difference between dict.items() and dict.iteritems() in Python?

Akshitha Mote
Updated on 30-Apr-2025 14:24:04

506 Views

In Python, the dict.items() and dict.iteritems() methods are used to return a dictionary. The only difference is that the dict.items() returns the keys and value pairs in the form of a list of tuple pairs, whereas the dict.iteritems() returns an iterator over the dictionary’s (key, value) tuple pairs. The dict.items() and dict.iteritems() functions are present in Python 2.x versions. The dict.iteritems() function is omitted from the Python 3.x version. Example - 'dict.items()' in Python 2.x version Following is an execution of the dict.items() in Python 2.x version - my_dict={1:'one', 2:'two', 3:'three', 4:'four'} print(my_dict.items()) Output Following is the output ... Read More

How can I iterate through two lists in parallel in Python?

Gireesha Devara
Updated on 29-May-2025 10:56:53

873 Views

In Python, using a for loop is common to iterate a single data structure like a list, but if we need to iterate two/multiple lists in parallel, we need to use the range() function. Iterating two Lists Using for Loop Assuming that both lists have the same length, here we are using the len() method to get the length of the list object. Example In the following example, we have iterated through the lists l1 and l2 of same size using for loop - l1 = ['a', 'b', 'c', 'd', 'e'] l2 = [97, 98, 99, 100, 101] length = ... Read More

How to convert a String representation of a Dictionary to a dictionary in Python?

Gireesha Devara
Updated on 29-May-2025 11:00:02

8K+ Views

To convert a string represented dictionary to an original dictionary, we can use built-in functions like eval(), json.load(), and ast.literal_eval(). Initially, we must ensure that the string contains a valid representation of the dictionary. Using the eval() function The eval() is a Python built-In function that takes a string argument, parses it as a code expression, and evaluates the expression. If the input expression contains a string representation of a dictionary, then the eval() method returns it as a normal Python dictionary. Syntax Following is the syntax of the eval() function in Python - eval(expression[, globals[, locals]]) Example ... Read More

What is best way to check if a list is empty in Python?

Akshitha Mote
Updated on 29-May-2025 14:05:10

476 Views

A List in Python is a mutable sequence of elements separated by commas ", ". It is represented by square braces "[ ]".An empty list contains no elements, meaning its length is 0. It is indicated by the square braces with no elements in it.In this article, we will explore various methods to check whether a given list in Python is empty.Checking for an Empty List using Not Operator In Python, the not operator is one of the logical operators. If the given condition is True, it will return False and vice versa. This operator evaluates the empty list as False. The 'not' ... Read More

Advertisements