Python Training

Python Training

+ Follow 1,834 Followers

Share

Showing 26 to 50 of 71
Sort By: 
M

Madhu 18/04/2016 in  Python Training classes

How can I pass optional or keyword parameters from one function to another?

0 0

Gaurav replied | 04/06/2016

Can you be more specific which function have optional/keyword parameter, caller or called one?

0 0

Sarfraaz replied | 08/09/2016

You can pass optional or keywords parameters from one function to another, by specifying the name of parameter. If we define a function def foo(start=10): The keyword parameter is start. When you invoke this function, you have to specify the same parameter name, start. Ex : result = foo(start=20)

0 0

Answer

You can add upto 6 Images

M

Mukesh 18/04/2016 in  Python Training classes

What is the difference between arguments and parameters?

0 0
S

Shubhadeep replied | 18/04/2016

The values passed during calling a function is known as argument. The values mentioned during defining function called parameters...

0 0
M

Manish replied | 02/09/2016

When you define a function and the values needed by the function and mentioned in the definition are parameters where as when you call a function and pass some values as defined by the function parameters, those are the arguments.

0 0

Answer

You can add upto 6 Images

N

Nimesh 18/04/2016 in  Python Training classes

Why did changing list ‘y’ also change list ‘x’?

0 0

Vimalanathan replied | 19/04/2016

Please do elaborate your question...
I assume you used an assignment operation y=x
If that is the case then this does not mean that the list x gets copied into list y. What this means is that the variable y starts pointing to list x.. this means that both variables x and y point to the same list. That is why when you change list y the change is also seen in x.

0 0

Rohit replied | 19/04/2016

Example : L1=[1,2,3,4] >>> L2 =L1 (So L2 is referencing L1 only, in memory , so if you change any element of L1 , L2 will also change), If you dont want this , Copy the list using : L2=L1[:]

0 0

Answer

You can add upto 6 Images

U

User 18/04/2016 in  Python Training classes

How do I write a function with output parameters (call by reference)?

0 0

Gaurav replied | 04/06/2016

You can past that parameter in the argument of the function like

def myfun(list) :
list[1] = 'hello'
Calling:

list = []
my fun(list)
print list

Output will be : ['hello']

0 0

Prasad Yamijala replied | 13/07/2016

Python has call by reference only no call by value

0 0

Answer

You can add upto 6 Images

S

Srinitha 17/04/2016 in  Python Training classes

What are the rules for local and global variables in Python?

0 0

Vishal Kumar replied | 15/06/2016

global variables need to be defined with global keyword and then can be used anywhere in the module. But local variables are limited to that function and can't be used outside that scope.

0 0

Sanjeev replied | 26/06/2016

By default all values are checked in local scope, but if you had defined global variable, and also have local variable by same name
then in the local function you need to call global variable_name if you need to refer to global variable..

0 0

Answer

You can add upto 6 Images

P

Pratik 17/04/2016 in  Python Training classes

Why do lambdas defined in a loop with different values all return the same result?

0 0

Parbat replied | 18/06/2016

This happens because x is not local to the lambdas, but is defined in the outer scope, and it is accessed when the lambda is called — not when it is defined.

0 0

Answer

You can add upto 6 Images

N

Neeta 17/04/2016 in  Python Training classes

How do I share global variables across modules?

0 0

Vimalanathan replied | 19/04/2016

Use the keyword "global"

0 0

Answer

You can add upto 6 Images

S

Sonal 17/04/2016 in  Python Training classes

What are the “best practices” for using import in a module?

0 0

Chitra replied | 28/04/2016

It s good practice if you import modules in the following order:

standard library modules e.g. sys, os, getopt, re)
third-party library modules (anything installed in Python s site-packages directory) e.g. mx.DateTime, ZODB, PIL.Image, etc.
locally-developed modules
Avoid relative package imports. If you re writing code that s in the package.sub.m1...  more»
It’s good practice if you import modules in the following order:

standard library modules — e.g. sys, os, getopt, re)
third-party library modules (anything installed in Python’s site-packages directory) — e.g. mx.DateTime, ZODB, PIL.Image, etc.
locally-developed modules
Avoid relative package imports. If you’re writing code that’s in the package.sub.m1 module and want to import package.sub.m2, do not just write import m2, even though it’s legal. Write from package.sub import m2 instead. Relative imports can lead to a module being initialized twice, leading to confusing bugs.

It is sometimes necessary to move imports to a function or class to avoid problems with circular imports. Gordon McMillan says:

Circular imports are fine where both modules use the “import (module)” form of import. They fail when the 2nd module wants to grab a name out of the first (“from module import name”) and the import is at the top level. That’s because names in the 1st are not yet available, because the first module is busy importing the 2nd.

In this case, if the second module is only used in one function, then the import can easily be moved into that function. By the time the import is called, the first module will have finished initializing, and the second module can do its import.

It may also be necessary to move imports out of the top level of code if some of the modules are platform-specific. In that case, it may not even be possible to import all of the modules at the top of the file. In this case, importing the correct modules in the corresponding platform-specific code is a good option.

Only move imports into a local scope, such as inside a function definition, if it’s necessary to solve a problem such as avoiding a circular import or are trying to reduce the initialization time of a module. This technique is especially helpful if many of the imports are unnecessary depending on how the program executes. You may also want to move imports into a function if the modules are only ever used in that function. Note that loading a module the first time may be expensive because of the one time initialization of the module, but loading a module multiple times is virtually free, costing only a couple of dictionary lookups. Even if the module name has gone out of scope, the module is probably available in sys.modules.

If only instances of a specific class use a module, then it is reasonable to import the module in the class’s __init__ method and then assign the module to an instance variable so that the module is always available (via that instance variable) during the life of the object. Note that to delay an import until the class is instantiated, the import must be inside a method. Putting the import inside the class but outside of any method still causes the import to occur when the module is initialized. «less

0 0

Answer

You can add upto 6 Images

S

Shreya 17/04/2016 in  Python Training classes

Why are default values shared between objects?

0 0

Chitra replied | 28/04/2016

By definition, immutable objects such as numbers, strings, tuples, and None, are safe from change. Changes to mutable objects such as dictionaries, lists, and class instances can lead to confusion.

Because of this feature, it is good programming practice to not use mutable objects as default values. Instead, use None as the default value and inside the function, check...  more»
By definition, immutable objects such as numbers, strings, tuples, and None, are safe from change. Changes to mutable objects such as dictionaries, lists, and class instances can lead to confusion.

Because of this feature, it is good programming practice to not use mutable objects as default values. Instead, use None as the default value and inside the function, check if the parameter is None and create a new list/dictionary/whatever if it is.
Default values are created by the def statement, not when the function is called. Consider this function:


def foo(D={}): # Danger: shared reference to one dict for all calls
... compute something ...
D[key] = value
return D
The first time you call this function, D contains a single item. The second time, D contains two items because when foo() begins executing, D starts out with an item already in it.

It is often expected that a function call creates new objects for default values. This is not what happens. Default values are created exactly once, when the function is defined (by executing the def statement). If that object is changed, like the dictionary in this example, subsequent calls to the function will refer to this changed object.
This feature can be useful. When you have a function that’s time-consuming to compute, a common technique is to cache the parameters and the resulting value of each call to the function, and return the cached value if the same value is requested again. This is called memoizing, and can be implemented like this:


# Callers will never provide a third parameter for this function.
def expensive(arg1, arg2, _cache={}):
if _cache.has_key((arg1, arg2)):
return _cache[(arg1, arg2)]

# Calculate the value
result = ... expensive computation ...
_cache[(arg1, arg2)] = result # Store result in the cache
return result
You could use a global variable containing a dictionary instead of the default value; it’s a matter of taste.

You can also use default arguments to bind local variables to objects rather than names. The first loop in the following example creates a new callback for each slot, but all callbacks end up with reference to the outer variable slot. In the second loop, explicit object binding is used to bind to the current value instead:


for slot in range(10):
def callback():
print "called from slot", slot # This always prints "9"
register_callback(slot, callback)

for slot in range(10):
def callback(slot=slot):
print "called from slot", slot
register_callback(slot, callback) # This does the right thing «less

0 0

Answer

You can add upto 6 Images

A

A 16/04/2016 in  Python Training classes

Show previous answers

Prabhakar replied | 22/06/2016

PEP8

0 0

Sanjeev replied | 25/06/2016

install pylint, which follows PEP8, and will help you how to correct the code as per standards
make a habit of using pylint, it also has plugins for popular IDEs

usage:
$ pylint python_program.py

0 0

Answer

You can add upto 6 Images

P

Pinaki 16/04/2016 in  Python Training classes

Why am I getting an UnboundLocalError when the variable has a value?

0 0 3

Show previous answers

Prasad Yamijala replied | 22/06/2016

You are trying to alter the value of a variable which is not created locally. unless previous assignment to the variable is done, this exception will not go

0 0

Prabhakar replied | 22/06/2016

Unbound error may come with different reasons...May the variable assigned block didnt executed or global variable declaration issue...

0 0

Answer

You can add upto 6 Images

S

Shiv 16/04/2016 in  Python Training classes

Is there a tool to help find bugs or perform static analysis?

0 0

Prasad Yamijala replied | 25/06/2016

PyChecker and PyLint

0 0

Answer

You can add upto 6 Images

T

Tanvi 16/04/2016 in  Python Training classes

How can I create a stand-alone binary from a Python script?

0 0

Prasad Yamijala replied | 22/06/2016

use pyinstaller or py2exe to convert to executables

0 0

Simanto replied | 24/06/2016

Use PyInstaller. It lets you create exeutables.

0 0

Answer

You can add upto 6 Images

S

Shiv Kohli 16/04/2016 in  Python Training classes

My program is too slow. How do I speed it up?

0 0

Satish replied | 17/04/2016

design better algorithm, use iteration over recursion, apply better logic. go through data structure and algorithm design course if you haven't already.

0 0

Sarfraaz replied | 12/09/2016

Find out the code where your program spends most of the time. Then reduce this time and your program will run faster than earlier.

0 0

Answer

You can add upto 6 Images

A

Show previous answers

G

Guruswamy replied | 24/06/2016

Use Pyscripter..it is like as good as Visual studio..It comes install able as well as Portable

0 0

Krishnasai replied | 18/07/2016

You can go either pdb debugger or pycharm or eclipse for python or pyscripter..any of them can suits to your requirements

0 0

Answer

You can add upto 6 Images

S

Srinika 15/04/2016 in  Python Training classes

How stable is Python?

0 0 3

Show previous answers

Sanjeev replied | 25/06/2016

Python is programming language you can either have complete software development done, or use just as scripting.
It depends on how you implement solution, how improvements are added and how effective you are testing, its not Python, but how the application development is done is stable.

0 0

Sanjeev replied | 26/06/2016

Python is programming language
The stability is concerned on the framework or application you write using Python.
Python is been used effectively and efficiently in Google, e-bay, Amazon, LinkedIn..
Python is open-source so if there is any issues in efficiency, community is very proactive to address the issues.

0 0

Answer

You can add upto 6 Images

C

Chitra 15/04/2016 in  Python Training classes

Is Python a good language for beginning programmers?

0 0 3

Show previous answers

Rajib replied | 16/04/2016

It is better if u start with C language .

0 0

Sarfraaz replied | 12/09/2016

I second the comments made by Vimalanathan. Undoubtedly, Python is the goto language for beginners.

0 0

Answer

You can add upto 6 Images

M

Mukund 15/04/2016 in  Python Training classes

Why is it called Python?

0 0

Kandasamy replied | 15/04/2016

Python is a type of programming language. For controlling both hardware and software

0 0

Answer

You can add upto 6 Images

M

Mangesh 15/04/2016 in  Python Training classes

What is “Monty Python’s Flying Circus”?

0 0

Kandasamy replied | 15/04/2016

It is not any way related to python programming, Please search in web for more detail.(it is a name of comedy troop)

0 0

Vimalanathan replied | 16/04/2016

It is a UK based comedy drama series.! :p

0 0

Answer

You can add upto 6 Images

S

Sajjan 14/04/2016 in  Python Training classes

How do I get a beta test version of Python?

0 0

Jatin replied | 29/06/2016

Hi Sajjan, Python has the stable version Python2 and Python3, what is the need of Beta Version.

0 0

Prasad Yamijala replied | 30/06/2016

All releases, including alphas, betas and release candidates, are announced on the comp.lang.python and comp.lang.python.announce newsgroups.

0 0

Answer

You can add upto 6 Images

S

Sreedhar 14/04/2016 in  Python Training classes

How do I submit bug reports and patches for Python?

0 0

Prasad Yamijala replied | 28/06/2016

use python bug tracker

0 0

Answer

You can add upto 6 Images

V

Vathsala 14/04/2016 in  Python Training classes

Are there any published articles about Python that I can reference?

0 0

Prasad Yamijala replied | 28/06/2016

Everybody gets info from python documentation only. please read the documentation with regard to any topic.
That would be of great help

0 0

Answer

You can add upto 6 Images

A

Aneesh 14/04/2016 in  Python Training classes

Are there any books on Python?

0 0

Sanjeev replied | 27/06/2016

There are lot, check, as its programming language make sure what you want to use it

- Learning Python - by Lutz M
- Python: The Complete Reference - by Martin C. Brown
- Automate the Boring Stuff with Python - by Albert Sweigart

0 0

Prasad Yamijala replied | 28/06/2016

download python tutorial from python.org. It has complete info.

0 0

Answer

You can add upto 6 Images

M

Manisha 14/04/2016 in  Python Training classes

Where in the world is http://www.python.org located?

0 0

Prasad Yamijala replied | 25/06/2016

The Python project’s infrastructure is located all over the world. http://www.python.org is hosted by Rackspace. Upfront Systems hosts bugs.python.org. Many other Python services are hosted by Oregon State University Open Source Lab

0 0

Answer

You can add upto 6 Images

S

Shobhitha 13/04/2016 in  Python Training classes

How do I get documentation on Python?

0 0 3

Show previous answers

Prasad Yamijala replied | 04/07/2016

go to http://www.python.org

0 0

Sarfraaz replied | 12/09/2016

On your Python shell use the "help()" function to get all the documentation you want on Python.

0 0

Answer

You can add upto 6 Images

Previous123Next

Have a Question?

Thousands of expert tutors are available to answer your question

Looking for Python Training classes?

Find best Python Training classes in your locality on UrbanPro.

FIND NOW

Do you offer Python Training classes?

Create Free Profile Now »

Top Contributors

Prasad Yamijala

21 Answers

Jatin

15 Answers

Manish

12 Answers

Vimalanathan

11 Answers

Sarfraaz

10 Answers

Sanjeev

8 Answers

Sudheer

8 Answers

Smartnub

8 Answers

Madhu

8 Answers

Sponsored

About UrbanPro

UrbanPro.com helps you to connect with the best Python Training classes in India. Post Your Requirement today and get connected.

Find Best Python Training classes?

Find Now »