Python Articles

Page 851 of 854

Why python for loops don't default to one iteration for single objects?

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

Python cannot iterate over an object that is not 'iterable'. The 'for' loop construct in python calls inbuilt functions within the iterable data-type which allow it to extract elements from the iterable.Since non-iterable data-types don't have these methods, there is no way to extract elements from them. And hence for loops ignore them.

Read More

What is possible key/value delimiter in Python dictionary?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 527 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 492 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

Is there a “not equal” operator in Python?

Pythonista
Pythonista
Updated on 30-Jul-2019 465 Views

In Python 2.x as well as != symbols are defined as 'not equal to' operators. In Python 3, operator is deprecated.

Read More

How to write Python CGI program to interact with MySQL?

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 1K+ Views

suppose you want to login into you account using Python CGi script, below is the details login.html email: password: login.py #!C:\Python27\python.exe import MySQLdb import cgi import Cookie # Open database connection db = MySQLdb.connect("localhost", "root", "", "student" ) # prepare a ...

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 560 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

What is @ operator in Python?

Jayashree
Jayashree
Updated on 30-Jul-2019 869 Views

@ symbol is used to define decorator in Python. Decorators provide a simple syntax for calling higher-order functions. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.we have two different kinds of decorators in Python:Function decoratorsClass decorators A decorator in Python is any callable Python object that is used to modify a function or a class. A reference to a function  or a class  is passed to a decorator and the decorator returns a modified function or class. The modified functions or classes usually contain calls to ...

Read More

What is Practical Use of Reversed Set Operators in Python?

Govinda Sai
Govinda Sai
Updated on 30-Jul-2019 190 Views

Reversed set operators are operators that are defined as:s & z corresponds to s.__and__(z) z & s corresponds to s.__rand__(z)These don't make much sense in normal operations like and, add, or, etc of simple objects. However in case of inheritence, reversed operations are particularly useful when dealing with subclasses because if the right operand is a subclass of the left operand the reversed operation is attempted first. You may have different implementations in parent and child classes.These reversed operations are also used if the first operand returns NotImplemented.

Read More

How to optimize nested if...elif...else in Python?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 1K+ Views

Here are some of the steps that you can follow to optimize a nested if...elif...else.1. Ensure that the path that'll be taken most is near the top. This ensures that not multiple conditions are needed to be checked on the most executed path.2. Similarly, sort the paths by most use and put the conditions accordingly.3. Use short-circuiting to your advantage. If you have a statement like:if heavy operation() and light operation():Then consider changing it toif lightOperation() and heavy operation():This will ensure that heavy operation is not even executed if the light operation is false. Same can be done with or ...

Read More
Showing 8501–8510 of 8,532 articles
Advertisements