What is correct syntax to create Python tuples?

Published on 29-Dec-2017 10:42:31
In Python, tuple object is an immutable collection of items, not necessarily of same type. Items are separated by comma and placed inside parentheses, although they are optional. >>> T1 = (1, 'one', 3.45, [1, 2, 3]) >>> T2 = 1, 2, 3, 4 Empty tuple object is ... Read More

How you will create your first program in Python?

Published on 29-Dec-2017 10:39:17
You can use any Python aware editor to write Python script. Standard distribution of Python comes with IDLE module which is an integrated development and learning environment. Start IDLE and open a new file from file menu. In the editor page enter print (“Hello World!”) Save the script ... Read More

How to remove a key from a python dictionary?

Published on 29-Dec-2017 10:36:39
Python’s del keyword is used pretty much any object. In order to delete a particular item from dictionary, provide key clause to del statement >>> D1 = {1: 'a', 2: 'b', 3: 'c', 'x': 1, 'y': 2, 'z': 3} >>> del D1['x'] >>> D1 {1: 'a', 2: 'b', 3: ... Read More

How to add new keys to a dictionary in Python?

Published on 29-Dec-2017 10:35:18
Dictionary is an unordered collection of key-value pairs. Each element is not identified by positional index. Moreover, the fact that key can’t be repeated, we simply use a new key and assign a value to it so that a new pair will be added to dictionary. >>> D1 = ... Read More

How to merge two Python dictionaries in a single expression?

Published on 29-Dec-2017 10:33:37
Built-in dictionary class has update() method which merges elements of argument dictionary object with calling dictionary object. >>> a = {1:'a', 2:'b', 3:'c'} >>> b = {'x':1, 'y':2, 'z':3} >>> a.update(b) >>> a {1: 'a', 2: 'b', 3: 'c', 'x': 1, 'y': 2, 'z': 3} From Python 3.5 ... Read More

How to sort a Python dictionary by value?

Published on 29-Dec-2017 10:31:37
Standard distribution of Python contains collections module. It has definitions of high performance container data types. OrderedDict is a sub class of dictionary which remembers the order of entries added in dictionary object. When iterating over an ordered dictionary, the items are returned in the order their keys were first ... Read More

How to change any data type into a string in Python?

Published on 29-Dec-2017 10:29:17
Any built-in data type converted into its string representation by str() function >>> str(10) '10' >>> str(11.11) '11.11' >>> str(3+4j) '(3+4j)' >>> str([1, 2, 3]) '[1, 2, 3]' >>> str((1, 2, 3)) '(1, 2, 3)' >>> str({1:11, 2:22, 3:33}) '{1: 11, 2: 22, 3: 33}' For a user ... Read More

What are different data conversion methods in Python?

Published on 29-Dec-2017 10:27:02
Numeric data conversion functions − int() − converts a floating point number or a string with integer representation to integer object. When converting a string, parameter of base of number system to convert hexadecimal or octal number to integer >>> int('11') 11 >>> int(11.15) 11 >>> int('20', 8) 16 ... Read More

How to print concatenated string in Python?

Published on 29-Dec-2017 10:22:29
When used with strings, plus (+) is defined as concatenation operator. It appends second string to the first string. >>> s1 = 'TutorialsPoint ' >>> s2 = 'Hyderabad' >>> print (s1+s2) TutorialsPoint Hyderabad

How to print a string two times with single statement in Python?

Published on 29-Dec-2017 10:21:09
When used with strings, asterisk (*) is defined as repetition operator. It concatenates given string as many times as number followed by asterisk. >>> string = 'abcdefghij' >>> print (string*2) abcdefghijabcdefghij