Found 34489 Articles for Programming

What are different types of quotes in Python?

Naveen Singh
Updated on 09-Sep-2023 09:50:36

5K+ Views

There are different types of quotes in Python. Each quotation is used in different scenarios as per the requirement. The following are the different types of quotations which we can use in the python programming language. Single quotes Double quotes Triple quotes What are Single quotes? Single quotes (' ') are used to create strings and characters in Python programming. Since the print() function accepts string values we can directly pass values to it wrapping them within the single quotes. Example In this example, we will create a string using the single quotes and assign it to the ... Read More

How to create an empty tuple in Python?

Naveen Singh
Updated on 09-Sep-2023 15:17:25

7K+ 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, 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 tuple.Indexing is the concept used for accessing the elements from particular data structures. There are two ways of performing ... Read More

How to create an empty dictionary in Python?

Naveen Singh
Updated on 15-May-2023 10:27:54

3K+ 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?

Naveen Singh
Updated on 30-Jul-2019 22:30:21

498 Views

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?

Naveen Singh
Updated on 08-Nov-2023 00:37:00

22K+ Views

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,L2)) >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4} Using dictionary comprehension syntax >>> L1 = ['a','b','c','d'] >>> L2 = [1,2,3,4] >>> d = {k:v for k,v in zip(L1,L2)} >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4}

In Python how to create dictionary from two lists?

Naveen Singh
Updated on 30-Jul-2019 22:30:21

316 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 do we define dictionary in Python?

Naveen Singh
Updated on 25-Feb-2020 11:23:19

135 Views

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 should be of immutable data type i.e. number, string or tuple.>>> D2={1:'aaa', 2:'bbb', 3:'ccc'} >>> D3={(10,10):'x1', (20,20):'x2'} >>> D4={'Microsoft':['MS Office', 'Windows', 'C#'], 'Oracle':['Oracle', 'Java', 'MySQL']}

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

Naveen Singh
Updated on 25-Feb-2020 11:22:35

81 Views

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?

Naveen Singh
Updated on 25-Feb-2020 11:22:01

344 Views

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?

Naveen Singh
Updated on 25-Feb-2020 11:21:28

528 Views

We can use built in hex() function to convert any integer to its hexadecimal representation.>>> hex(100) '0x64' >>> hex(4095) '0xfff' >>> hex(31) '0x1f'

Advertisements