What are different types of quotes in Python?

Jayashree
Published on 09-Feb-2018 00:36:12
Quotation symbols are used to create string object in Python. Python recognizes single, double and triple quoted strings. String literals are written by enclosing a sequence of characters in single quotes ('hello'), double quotes ("hello") or triple quotes ('''hello''' or """hello""").>>> var1='hello' >>> var1 'hello' >>> var2="hello" >>> var2 'hello' ... Read More

List vs tuple vs dictionary in Python

Jayashree
Published on 09-Feb-2018 00:34:00
List and Tuple objects are sequences. A dictionary is a hash table of key-value pairs. List and tuple is an ordered collection of items. Dictionary is unordered collection.List and dictionary objects are mutable i.e. it is possible to add new item or delete and item from it. Tuple is an ... Read More

How to select Python Tuple/Dictionary Values for a given Index?

Jayashree
Published on 09-Feb-2018 00:32:06
Items in tuple are indexed. Slice operator allows item of certain index to be accessed>>> T1=(12, "Ravi", "B.Com FY", 78.50) >>> print (T1[2]) B.Com FYItems in dictionary are not indexed. Value associated with a certain key is obtained by putting in square bracket. The get() method of dictionary also returns ... Read More

How I can dump a Python tuple in JSON format?

Jayashree
Published on 09-Feb-2018 00:30:10
JSON stands for Javascript object notation. It is a lightweight, language-independent data interchange format. Python library has json module that provides functions for json encoding and decoding.The dumps() function converts a Python object to JSON format>>> T1=(12, "Ravi", "B.Com FY", 78.50) >>> import json >>> str1=json.dumps(T1) >>> str1 '[12, "Ravi", ... Read More

How do we use double quotation in Python?

Jayashree
Published on 09-Feb-2018 00:28:44
Quotation symbols are used to create string object in Python. Python recognizes single, double and triple quoted strings. String literals are written by enclosing a sequence of characters in single quotes ('hello'), or double quotes ("hello")>>> var1='hello' >>> var1 'hello' >>> var2="hello" >>> var2 'hello'If single quoted text is to ... Read More

What are multiple statement groups as suites in python?

Jayashree
Published on 09-Feb-2018 00:27:01
A group of individual statements, which make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class require a header line and a suite.Header lines begin the statement (with the keyword) and terminate with a colon (: ) and are ... Read More

How to serialize Python dictionary to XML?

Jayashree
Published on 09-Feb-2018 00:24:03
Use dicttoxml package to convert Python dictionary to xml representation.To start, install dicttoxml packagepip3 install dicttoxmlCreate a dictionary object>>> D1={"name":"Ravi", "age":21, "marks":55}Now import dicttoxml() function from dicttoxml package and use D1 as argument. The function returns encoded string as xml representation of dictionary>>>fromdicttoxml import dicttoxml >>>xml=dicttoxml(D1)Obtain decode string by decode() functionThe ... Read More

How to declare a multi dimensional dictionary in Python?

Jayashree
Published on 09-Feb-2018 00:20:57
Let us declare three dictionary objects as below −>>> d1={"name":"Ravi", "age":25, "marks":60} >>> d2={"name":"Anil", "age":23, "marks":75} >>> d3={"name":"Asha", "age":20, "marks":70}Now we shall use them as values in a parent dictionary>>>twodimdict={1:d1, 2:d2, 3:d3} >>>twodimdict {1: {'name': 'Ravi', 'age': 25, 'marks': 60}, 2: {'name': 'Anil', 'age': 23, 'marks': 75}, 3: {'name': 'Asha', ... Read More

What is usage of update method in Python dictionary?

Jayashree
Published on 09-Feb-2018 00:18:25
The update() method of Python’s dictionary class serves two purposes.It adds a new key-value pair if dictionary doesn’t already contain the key.>>> d1={'name': 'Ravi', 'age': 25, 'marks': 60}The update() method a dictionary object as argumentd1.update({"course":"ComputerEngg"})The updated dictionary shows that a new key-value pair is added>>> d1 {'name': 'Ravi', 'age': 25, ... Read More

How does Python dictionary keys() Method work?

Jayashree
Published on 09-Feb-2018 00:15:54
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 ... Read More
Advertisements