Python Articles - Page 969 of 1048

How to create an empty list in Python?

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

751 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

443 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

920 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

439 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

197 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

Advertisements