Pythonic has Published 7 Articles

What is "not in" operator in Python?

Pythonic

Pythonic

Updated on 09-Sep-2023 15:12:50

5K+ Views

In Python, 'not in' membership operator evaluates to true if it does not finds a variable in the specified sequence and false otherwise. For example >>> a = 10 >>> b = 4 >>> l1 = [1, 2, 3, 4, 5] >>> a not in l1 True >>> b not ... Read More

How to pop-up the first element from a Python tuple?

Pythonic

Pythonic

Updated on 20-Feb-2020 11:27:15

4K+ Views

By definition, tuple object is immutable. Hence it is not possible to remove element from it. However, a workaround would be convert tuple to a list, remove desired element from list and convert it back to a tuple.>>> T1=(1,2,3,4) >>> L1=list(T1) >>> L1.pop(0) 1 >>> L1 [2, 3, 4] >>> T1=tuple(L1) >>> T1 (2, 3, 4)

How to iterate through a tuple in Python?

Pythonic

Pythonic

Updated on 19-Dec-2019 09:06:37

5K+ Views

There are different ways to iterate through a tuple object. The for statement in Python has a variant which traverses a tuple till it is exhausted. It is equivalent to foreach statement in Java. Its syntax is −for var in tuple: stmt1 stmt2ExampleFollowing script will print all items in the ... Read More

How to access Python dictionary in simple way?

Pythonic

Pythonic

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

164 Views

There are two ways available to access value associated with a key in a dictionary collection object. The dictionary class method get() takes key as argument and returns value. >>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56} >>> d1.get('age') 23 Another way is to use key inside ... Read More

How do I check whether a file exists using Python?

Pythonic

Pythonic

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

634 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

209 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

409 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}

1
Advertisements