A dictionary is a fundamental data structure in Python that stores data as key-value pairs. Dictionaries are also known as associative arrays and are represented by curly braces {}. Unlike lists that use numeric indexes, dictionaries use unique keys to access their values. Keys must be immutable objects like strings, numbers, or tuples, while values can be of any data type. Python provides two simple methods to create an empty dictionary. Using Curly Braces {} The most common way to create an empty dictionary is using empty curly braces. Syntax variable_name = {} ... Read More
In Python, a list is one of the built-in data types. A Python list is a sequence of items separated by commas and enclosed in square brackets [ ]. The items in a Python list need not be of the same data type. In this article, we will discuss different ways to create an empty list in Python. Using Square Brackets This is the simplest and most common way to create an empty list using square brackets []. An empty list means the list has no elements at the time of creation, but we can add items ... Read More
In Python, the ord() function converts a single character to its corresponding ASCII (American Standard Code for Information Interchange) or Unicode integer value. The ord() function raises a TypeError if you pass a string with more than one character. Syntax ord(character) Parameters: A single character (string of length 1) Return Value: Integer representing the Unicode code point of the character Converting a Single Character to Integer Here's how to convert a character 'A' to its Unicode integer value ? my_char = 'A' result = ord(my_char) print("Unicode of 'A':", result) # ... Read More
Converting Python strings into tuples is a common operation with multiple approaches depending on your needs. You can create a tuple containing the whole string, split the string into individual characters, or parse delimited strings into separate elements. Using Comma to Create Single-Element Tuple The simplest way is to add a comma after the string variable to treat the entire string as a single tuple element ? s = "python" print("Input string:", s) t = s, print("Output tuple:", t) print("Type:", type(t)) Input string: python Output tuple: ('python', ) Type: ... Read More
Python provides several ways to create dictionaries using list comprehension. The dict() method combined with list comprehension offers an elegant approach to generate key-value pairs dynamically. Syntax The basic syntax for creating a dictionary with list comprehension ? # Using dict() with list comprehension dict([(key, value) for item in iterable]) # Direct dictionary comprehension (alternative) {key: value for item in iterable} Using Unicode Characters as Keys Create a dictionary where keys are Unicode characters and values are their corresponding integers ? dict_obj = dict([(chr(i), i) for i in range(100, 105)]) ... Read More
While creating a function, the single asterisk (*) is used to accept any number of positional arguments, and the double asterisk (**) is used to accept any number of keyword arguments. These operators provide flexibility when you don't know in advance how many arguments will be passed to your function. Using * (Single Asterisk) for Positional Arguments The single asterisk (*) collects extra positional arguments into a tuple. By convention, this parameter is named *args. Example Create a function that accepts an arbitrary number of positional arguments ? def sum_numbers(*args): ... Read More
Drop down boxes are useful HTML form elements when you have multiple options but only want to allow selection of one or two items. Here's how to pass dropdown data to a Python CGI script and process it server-side. HTML Form with Dropdown First, create an HTML form with a dropdown (select) element ? Maths Physics Chemistry This creates a dropdown with ... Read More
The TEXTAREA element allows users to input multiline text data that can be processed by a Python CGI script. This is useful for forms that need to collect longer text content like comments, descriptions, or messages. HTML Form with TEXTAREA Here is the HTML code for creating a form with a TEXTAREA element ? Type your text here... The result of this code is the following form ? Type your text here... Submit Python CGI Script Below is the textarea.py script to handle ... Read More
Radio buttons allow users to select exactly one option from a group of choices. When working with CGI (Common Gateway Interface) in Python, you can easily retrieve and process radio button data using the cgi module. HTML Form with Radio Buttons 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 Subject] Python CGI Script Below is radiobutton.py script to handle input given by web ... Read More
Python CGI (Common Gateway Interface) allows web servers to execute Python scripts and process form data. When a user submits an HTML form, the CGI script can retrieve and process the submitted data. HTML Form Setup First, create an HTML form that sends data to a Python CGI script ? FirstName: LastName: This form collects first and last names, then sends the data to getData.py using the POST method. Python CGI Script Create the getData.py script to process ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance