Pythonic has Published 20 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 define a Python dictionary within dictionary?

Pythonic

Pythonic

Updated on 18-Feb-2020 07:38:26

220 Views

A dictionary object is mutable. Hence one dictionary object can be used as a value component of a key. So we can create a nested dictionary object another dictionary object is defined as value associated with key.>>>> students={"student1":{"name":"Raaj", "age":23, "subjects":["Phy", "Che", "maths"], "GPA":8.5}, "student2":{"name":"Kiran", "age":21, "subjects":["Phy", "Che", "bio"], "GPA":8.25}}Read More

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 iterate over dictionaries using 'for' loops in Python?

Pythonic

Pythonic

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

341 Views

Even though dictionary itself is not an iterable object, the items(), keys() and values methods return iterable view objects which can be used to iterate through dictionary. The items() method returns a list of tuples, each tuple being key and value pair. >>> d1={'name': 'Ravi', 'age': 23, 'marks': 56} ... Read More

How to create a Python dictionary from an object's fields?

Pythonic

Pythonic

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

318 Views

The __dict__ attribute returns a dictionary out of fields of any object. Let us define a class person >>> class person: def __init__(self): self.name='foo' self.age = 20 def show(self): print (self.name, self.age) We now declare an object of this ... Read More

How to access Python dictionary in simple way?

Pythonic

Pythonic

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

152 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 to map two lists into a dictionary in Python?

Pythonic

Pythonic

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

374 Views

Easiest way is to create a zip object that returns a generator of tuples, each having an item each from two lists. The zip object can then be transformed into a dictionary by using built-in dict() function >>> l1=['name', 'age', 'marks'] >>> l2=['Ravi', 23, 56] >>> z=zip(l1, l2) >>> ... Read More

How do I check whether a file exists using Python?

Pythonic

Pythonic

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

615 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

182 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

Advertisements