Found 10784 Articles for Python

Does Python have a ternary conditional operator?

Malhar Lathkar
Updated on 26-Feb-2020 07:28:43

160 Views

Ternary operator was added in Python 2.5. Its syntax is:Syntaxx if expr==True else yExampleFollowing example illustrates usage>>> percent=59 >>> 'pass' if percent>=50 else 'fail' 'pass' >>> percent=45 >>> 'pass' if percent>=50 else 'fail' 'fail'

How does Python dictionary keys() Method work?

Jayashree
Updated on 25-Feb-2020 11:24:09

277 Views

The keys() method of Python dictionary class returns a view object consisting of keys used in the dictionary.>>> d1 = {'name': 'Ravi', 'age': 21, 'marks': 60, 'course': 'Computer Engg'} >>>d1.keys() dict_keys(['name', 'age', 'marks', 'course'])It can be stored as a list object. If new key-value pair is added, the view object is automatically updated.>>> l1=d1.keys() >>> l1 dict_keys(['name', 'age', 'marks', 'course']) >>>d1.update({"college":"IITB"}) >>> l1 dict_keys(['name', 'age', 'marks', 'course', 'college'])

What is usage of update method in Python dictionary?

Niharika Aitam
Updated on 15-May-2023 13:26:57

295 Views

The update method is one of the methods of dictionary data structure. It is used to update the values in the already created dictionary which means it adds a new key value pair to the dictionary. The updated key and value will be updated at the last. The dictionary is represented by curly braces{}.The dictionary contains key and value pairs, combinedly called as items which can accept any data type of elements as values. It is mutable which means once the dictionary is created we can apply the changes. It has key, value pairs separated by colon and the key ... Read More

How do we use double quotation in Python?

Niharika Aitam
Updated on 15-May-2023 13:24:16

468 Views

Double quotes are used to represent a string or a character as well as can be used in the print function to print the defined statement as the output. The functionality of the single quotes and double quotes is same. We can use any type quotes as per our requirement. The following is the syntax to create a string using the double quotes in the python programming language. str = “text” Where, “ ” are the double quotations text is the data to be in string format Example Let’s see an example to understand the functionality and ... Read More

How I can dump a Python tuple in JSON format?

Niharika Aitam
Updated on 15-May-2023 12:46:09

796 Views

JSON can be abbreviated as JavaScript Object Notation. It means a script of a text file in a programming language to transfer and store the data. It is supported by the python programming language using a built-in package named json. It’s text is given in the quoted string format which contains a key and value within the curly braces{} which looks like a dictionary. To access a JSON document using python we have to import the json package. In this package we have a method named dumps() which is used to convert the python tuple objects into the Java ... Read More

List vs tuple vs dictionary in Python

Niharika Aitam
Updated on 15-May-2023 11:12:02

24K+ Views

In python there are different types of data structures. Among them list, tuple, dictionary. All these three are used to store the data in the python programming language. What is a List? List is one of the data structures available in python which is used to store multiple values in a single data structure. We can create a list in Python using the square braces[ ]. It is mutable which means that we can modify a list once it is created. It takes multiple elements of different data types such as int, float, str etc which are separated by ... Read More

What are different types of quotes in Python?

Niharika Aitam
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?

Niharika Aitam
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?

Niharikaa Aitam
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?

Pythonic
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 []

Advertisements