Pythonic has Published 26 Articles

How do I check whether a file exists using Python?

Pythonic

Pythonic

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

491 Views

Presence of a certain file in the computer can be verified by two ways using Python code. One way is using isfile() function of os.path module. The function returns true if file at specified path exists, otherwise it returns false. >>> import os >>> os.path.isfile("d:\Package1\package1\fibo.py") True >>> os.path.isfile("d:/Package1/package1/fibo.py") True ... Read More

What is the difference between __str__ and __repr__ in Python?

Pythonic

Pythonic

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

124 Views

The built-in functions repr() and str() respectively call object.__repr__(self) and object.__str__(self) methods. First function computes official representation of the object, while second returns informal representation of the object. Result of both functions is same for integer object. >>> x = 1 >>> repr(x) '1' >>> str(x) '1' ... Read More

In Python how to create dictionary from two lists?

Pythonic

Pythonic

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

290 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 to create an empty list in Python?

Pythonic

Pythonic

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

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

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

Pythonic

Pythonic

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

119 Views

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 to print a string two times with single statement in Python?

Pythonic

Pythonic

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

163 Views

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

328 Views

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

167 Views

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

102 Views

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

728 Views

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

Advertisements