Python Articles

Page 855 of 855

Stack and Queue in Python using queue Module

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 744 Views

In Python, it is very easy to implement stack and queue data structures. Stack is called LIFO because Stack works on the principle of "Last-in, first-out" and Queue is called FIFO because Queue works on the principle of "First-in, first-out", and the inbuilt functions in Python make the code shorter and simple. The Queue module implements multi-producer, multi-consumer queues and It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics and it depends on the availability of thread support in Python. This ...

Read More

What is possible key/value delimiter in Python dictionary?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 542 Views

You can use any hashable object like int, string, etc as a key in a python dict. You need to separate it from the value using the ':' delimiter. The value can be any type of object. Consecutive key value pairs must be separated by a comma.

Read More

How do Python dictionary hash lookups work?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 509 Views

Dicts are hash tables. No tree searching is used. Looking up a key is a nearly constant time(Amortized constant) operation, regardless of the size of the dict. It creates the hash of the key, then proceeds to find the location associated with the hashed value. If a collision listed address is encountered, it starts the collision resolution algorithm to find the actual value.This causes dictionaries to take up more space as they are sparse.

Read More

How to create a Python dictionary from text file?

Jayashree
Jayashree
Updated on 30-Jul-2019 13K+ Views

Assuming a following text file (dict.txt) is present1 aaa2 bbb3 cccFollowing Python code reads the file using open() function. Each line as string is split at space character. First component is used as key and second as valued = {} with open("dict.txt") as f: for line in f:     (key, val) = line.split()     d[int(key)] = val print (d)The output shows contents of file in dictionary form{1: 'aaa', 2: 'bbb', 3: 'ccc'}

Read More

Why are numbers represented as objects in python?

snehal patel
snehal patel
Updated on 30-Jul-2019 573 Views

Everything in Python is an object, and that includes the numbers. There are no "primitive" types, only built-in types.Numbers, however, are immutable. When you perform an operation with a number, you are creating a new number object.

Read More

When are python objects candidates for garbage collection?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 30-Jul-2019 308 Views

A python object or variable will be eligible for garbage collection as soon as all references to it go out of scope or are manually deleted (del x). We would have to presume there were no references to the object anywhere else for it to be garbage collected.

Read More

How to exit from a Python if clause?

Malhar Lathkar
Malhar Lathkar
Updated on 30-Jul-2019 584 Views

It is not possible to exit from an if block of Python code. The break keyword does appear in if block but it has to inside a loop. It is however possible to exit from entire program from inside if block by sys.exit()

Read More

What is the difference between the != and <> operators in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 30-Jul-2019 351 Views

In Python 2.x, both != and operators are available to check if two operands are not equal. Both return true if operands are not equal and false if they are equal.In Python 3.x, operator has been deprecated.

Read More

Why there is not do...while loop in Python?

Pythonista
Pythonista
Updated on 30-Jul-2019 2K+ Views

PEP 315 (Python Enhancement Proposal) to include do..while statement has been rejected because it doen't fit in the general format of indented block statement: indented block used by every other Python compound statement. In words of Guido Van Rossum -  "Please reject the PEP. More variations along these lines won't make the language more elegant or easier to learn. They'd just save a few hasty folks some typing while making others who have to read/maintain their code wonder what it means".

Read More
Showing 8541–8549 of 8,549 articles
« Prev 1 851 852 853 854 855 Next »
Advertisements