Found 10476 Articles for Python

How to create an empty tuple in Python?

Niharika Aitam
Updated on 29-May-2025 11:36:02

9K+ Views

Tuple is one of the data structures of the Python programming language. It is used to store multiple values separated by commas in an ordered manner. It is immutable in the sense that once the tuple is created cannot perform any operations like deleting, appending, etc. The elements in the tuple can be int, float, string, or binary data types, and it allows duplicates of the elements. It uses indexing for accessing the elements. It allows a single element to be in a tuple. There are different ways to create the empty tuple. Let’s see each way in detail. ... Read More

How to create an empty dictionary in Python?

Niharikaa Aitam
Updated on 15-May-2023 10:27:54

4K+ Views

The dictionary is the data structure available in python. Dictionaries are also known as associative memories or associative arrays. It is represented by curly braces {}. It stores the data in the form of key and value pairs. Data from a dictionary can be accessed by its key, unlike other data structures which use indexes. To retrieve the value associated with a particular key, one must use index values. As keys in dictionaries are unique, any immutable object such as tuples or strings can be used to identify them. However, values stored in dictionaries do not need to be ... Read More

How to create an empty list in Python?

Akshitha Mote
Updated on 17-Apr-2025 16:42:25

736 Views

In Python, list is one of the built-in data types. A Python list is a sequence of items separated by commas, 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 one of the simplest way to create an empty list to using square brackets[]. An empty list means the list has no elements at the time of creation, but we can add items to it later when needed. my_list=[] ... Read More

How to create Python dictionary from list of keys and values?

Akshitha Mote
Updated on 17-Apr-2025 16:41:25

24K+ Views

In Python, the dictionary is one of the built-in data types that stores the data in the form of key-value pairs. The pair of key-value is separated by a comma and enclosed within curly braces {}. The key and value within each pair are separated by a colon (:). Each key in a dictionary is unique and maps to a value. It is an unordered, mutable data. Creating dictionary from lists In Python, we can create a dictionary by using two separate lists. One list is considered as the keys, and the second one is considered as values. We ... Read More

In Python how to create dictionary from two lists?

Pythonic
Updated on 30-Jul-2019 22:30:21

434 Views

If L1 and L2 are list objects containing keys and respective values, following list comprehension syntax can be used to construct dictionary object. >>> L1 = ['a','b','c','d'] >>> L2 = [1,2,3,4] >>> d = {L1[k]:L2[k] for k in range(len(L1))} >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4}

How to convert an integer to an ASCII value in Python?

Akshitha Mote
Updated on 15-Apr-2025 14:44:38

2K+ Views

Converting Integer to ASCII Using chr() Function In Python, the chr() function converts an integer value to the corresponding ASCII (American Standard Code for Information Interchange) character only if the integer range lies between 0 and 127. The chr() function accepts Unicode values within the range of 0 to 1114111. If a value outside this range is provided, the function raises a ValueError. Example In the following example, we have converted integer values to ASCII values using chr() - print(chr(85)) print(chr(100)) print(chr(97)) Following is the output of the above code - U 100 a Example We can ... Read More

How to convert an integer to a unicode character in Python?

Akshitha Mote
Updated on 22-Jan-2025 14:30:09

4K+ Views

Unicode is a standardized character encoding that assigns a unique number to each character in most of the world's writing systems. Unicode separates the code points from the details of the encoding system. This permits a much wider range of characters up to four bytes. The Unicode character set incorporates the entirety of the ASCII character set as the first 127 characters. All ASCII characters have the same code points in both encodings. Techniques to Convert an Integer to a Character Following are the various techniques to convert an integer to a character in Python − ... Read More

How to convert a single character to its integer value in Python?

Sakshi Rayu
Updated on 02-May-2025 19:46:40

881 Views

In Python, the ord() function converts a given character to the corresponding ASCII (American Standard Code for Information Interchange) integer. The ord() function raises a TypeError if you pass a string value as a parameter. Converting a single alphabet to its integer In the following example, we have converted a character 'A' into its Unicode using the ord() function - my_str='A' result=ord(my_str) print("Unicode of 'A'-", result) Following is an output of the above code - Unicode of 'A'- 65 Printing Integer values of all the alphabets We can use the ord() function to print all the Unicode characters ... Read More

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

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

428 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

191 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)

Advertisements