How to create an empty tuple in Python?

Pythonic
Published on 08-Feb-2018 23:46:43
You can create empty tuple object by giving no elements in parentheses in assignment statement. Empty tuple object is also created by tuple() built-in function without any arguments >>> T1 = () >>> T1 () >>> T1 = tuple() >>> T1 ()

How to create an empty dictionary in Python?

Pythonic
Published on 08-Feb-2018 23:45:34
You can create empty dictionary object by giving no elements in curly brackets in assignment statement. Empty dictionary object is also created by dict() built-in function without any arguments >>> L1 [] >>> d1 = {} >>> d1 {} >>> d1 = dict() >>> d1 {} Read More

How to create an empty list in Python?

Pythonic
Published on 08-Feb-2018 23:44:03
You can create empty list object by giving no elements in square brackets in assignment statement. Empty list object is also created by list() built-in function without any arguments >>> L1 = [] >>> L1 [] >>> L1 = list() >>> L1 []

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

Pythonic
Published on 08-Feb-2018 23:41:15
If L1 and L2 are list objects containing keys and respective values, following methods can be used to construct dictionary object. Zip two lists and convert to dictionary using dict() function >>> L1 = ['a', 'b', 'c', 'd'] >>> L2 = [1, 2, 3, 4] >>> d = dict(zip(L1, ... Read More

In Python how to create dictionary from two lists?

Pythonic
Published on 08-Feb-2018 23:39:54
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, ... Read More

How do we define dictionary in Python?

Pythonista
Published on 08-Feb-2018 23:36:53
Dictionary object is an unordered collection of key-value pairs, separated by comma and enclosed in curly brackets. Association of value with key is marked by : symbol between them.>>> D1={'a':1, 'b':2, 'c':3}Key can appear in a dictionary object only once, whereas single value can be assigned to multiple keys. Key ... Read More

How can I parse a numeric string to its corresponding float value?

Pythonista
Published on 08-Feb-2018 23:35:41
Python’s number conversion function float() converts an integer to float with fractional part as 0. It also parses a string with valid representation of float number to a float object.>>> float('1.11') 1.11 >>> float(1) 1.0 >>> float('1') 1.0 >>> float('1.1e-2') 0.011

How to convert an integer to a octal string in Python?

Pythonista
Published on 08-Feb-2018 23:34:00
We have use oct() function from Python’s library to convert any integer to its octal equivalent number. We get a string having octal representation>>> oct(100) '0o144' >>> oct(0x10) '0o20' >>> oct(10) '0o12'

How to convert an integer to a hexadecimal string in Python?

Pythonista
Published on 08-Feb-2018 23:32:24
We can use built in hex() function to convert any integer to its hexadecimal representation.>>> hex(100) '0x64' >>> hex(4095) '0xfff' >>> hex(31) '0x1f'

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

Pythonista
Published on 08-Feb-2018 23:31:28
ASCII character associated to an integer is obtained by chr() function. The argument for this function can be any number between 0 to 0xffff>>> chr(0xaa) 'ª' >>> chr(0xff) 'ÿ' >>> chr(200) 'È' >>> chr(122) 'z'
Advertisements