Pythonic has Published 35 Articles

How to create an empty list in Python?

Pythonic

Pythonic

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

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

What are the key differences between Python 2.7.x and Python 3.x?

Pythonic

Pythonic

Updated on 30-Jul-2019 22:30:20

Python 3.0 was released in Dec. 2008. It was designed to rectify certain flaws in earlier version. The guiding principle of Python 3 was: "reduce feature duplication by removing old ways of doing things". Python 3.0 doesn’t provide backward compatibility. That means a Python program written using version 2.x syntax ... Read More

How do we create multiline comments in Python?

Pythonic

Pythonic

Updated on 30-Jul-2019 22:30:20

Comment is a piece of text in a computer program that is meant to be a programmer-readable explanation or annotation in the source code and not ignored by compiler/interpreter. In Python script, the symbol # indicates start of comment line. C like block comment (/* .. */) is not available ... Read More

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

Pythonic

Pythonic

Updated on 30-Jul-2019 22:30:20

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

How to print concatenated string in Python?

Pythonic

Pythonic

Updated on 30-Jul-2019 22:30:20

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 change any data type into a string in Python?

Pythonic

Pythonic

Updated on 30-Jul-2019 22:30:20

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

How to merge two Python dictionaries in a single expression?

Pythonic

Pythonic

Updated on 30-Jul-2019 22:30:20

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 add new keys to a dictionary in Python?

Pythonic

Pythonic

Updated on 30-Jul-2019 22:30:20

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 you will create your first program in Python?

Pythonic

Pythonic

Updated on 30-Jul-2019 22:30:20

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

What is correct syntax to create Python tuples?

Pythonic

Pythonic

Updated on 30-Jul-2019 22:30:20

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

Advertisements