p

python tutorial - Python programming questions and answers pdf - learn python - python programming



python interview questions :191

Error thrown even if a line is commented ?

  • You want to add the following line at the top of your source file:
# -*- coding: utf-8 -*-
  • This tells python what is the encoding of your source file.
python error line

Learn python - python tutorial - python error line - python examples - python programs

  • Source: Working with utf-8 encoding in Python source

python interview questions :192

What is a global statement in python ?

The purpose of the global statement is to declare that a function or method intends to change the value of a name from the global scope, that is, a name from outside the function.

a = 1

def f():
    a = 2 # doesn't affect global a, this new definition hides it in local scope

a = 1

def f():
    global a
    a = 2 # affects global a
python global

Learn python - python tutorial - python global - python examples - python programs

python interview questions :193

What is a lazy property ?

  • It is a property decorator that gets out of the way after the first call. It allows you to auto-cache a computed value.
  • The standard library @property decorator is a data descriptor object and is always called, even if there is an attribute on the instance of the same name.
python models

Learn python - python tutorial - python models - python examples - python programs

  • The @cached_property decorator only has a __get__ method, which means that it is not called if there is an attribute with the same name already present. It makes use of this by setting an attribute with the same name on the instance on the first call. Given a @cached_property-decorated bar method on an instance named foo, this is what happens:
    • Python resolves foo.bar. No bar attribute is found on the instance.
    • Python finds the bar descriptor on the class, and calls __get__ on that.
    • The cached_property __get__ method calls the decorated bar method.

python interview questions :194

On Windows, what's the difference between the various Scripts folders ?

  • On a Windows installation of Python, what is the difference among the following.
    • "PythonInstallFolder"\Scripts\
    • %APPDATA%\Python\Scripts\
    • %APPDATA%\Python\Python36\Scripts\
 python scripting

Learn python - python tutorial - python scripting - python examples - python programs

  • I have multiple versions of python installed.
    • Python27 -> C:\Users\nahawk\Python\Python27\
    • Python36 -> C:\Users\nahawk\Python\Python36\

Output

C:\>where pip
C:\Users\nahawk\Python\Python27\Scripts\pip.exe
C:\Users\nahawk\Python\Python36\Scripts\pip.exe

C:\>where pip2
C:\Users\nahawk\Python\Python27\Scripts\pip2.exe

C:\>where pip3
C:\Users\nahawk\Python\Python36\Scripts\pip3.exe

C:\>where cookiecutter
C:\Users\nahawk\AppData\Roaming\Python\Scripts\cookiecutter.exe

C:\>where futurize
C:\Users\nahawk\AppData\Roaming\Python\Scripts\futurize.exe
C:\Users\nahawk\AppData\Roaming\Python\Python36\Scripts\futurize.exe

python interview questions :195

What is the `is` function in Python ?

  • is checks if the objects have the same identity. == only checks if they are equal.
>>> L1 = [1,2,3]
>>> L2 = [1,2,3]
>>> L1 is L2
False
>>> L1 == L2
True

python interview questions :196

convert a number enclosed in parentheses (string) to a negative integer (or float) using Python ?

my_str = "(4,301)"
num = -int(my_str.translate(None,"(),"))
  • Assuming just removing the , is safe enough, and you may wish to apply the same function to values that may contain negative numbers or not, then:
import re
print float(re.sub(r'^\((.*?)\)$', r'-\1', a).replace(',',''))
  • You could then couple that with using locale as other answers have shown, eg:
import locale, re

locale.setlocale(locale.LC_ALL, 'en_GB.UTF-8')
print locale.atof(re.sub('^\((.*?)\)$', r'-\1', a))

python interview questions :197

Creating a game in python ?

  • How to use the while loop and accepting input within the loop.
python game

Learn python - python tutorial - python game - python examples - python programs

Sample Code :

print "Type 3 to continue, anything else to quit."
someInput = raw_input()

while someInput == '3':
    print "Thank you for the 3. Very kind of you."
    print "Type 3 to continue, anything else to quit."
    someInput = raw_input()

print "That's not 3, so I'm quitting now."

python interview questions :198

only() method in Python ?

  • There is no only built-in function, as you'll see if you type help(only) into your Python interpreter.
  • It must be pulled into the namespace with a from <module> import <only|*> instruction in that module. When you find this, you could try importing the module in your Python interpreter and using the help function again to find out what it does.

python interview questions :199

Database Cursor ?

  • Probably it is most like a file handle.
  • That does not mean that it is a file handle, and a cursor is actually an object - an instance of a Cursor class (depending on the actual db driver in use).
python database

Learn python - python tutorial - python-database - python examples - python programs

  • The reason that it's similar to a file handle is that you can consume data from it, but (in general) you can't go back to previously consumed data. Consumption of data is therefore unidirectional. Reading from a file handle returns characters/bytes, reading from a cursor returns rows.

python interview questions :200

Create a list in python but how to iterate through this list in javascript ?

  • Add dataType:json to $.ajax script
 $.ajax({
         type:"GET",
         url:"/friends2/",  #the url /friends2/ points to friends2 in python
         data: {},
         dataType: json, //ADD THIS
         ...
         ...
  • If you don't want to add data type then you can use jQuery.parseJSON(b) to parse your json string,
success: function(b) {
    var data = jQuery.parseJSON(b); //parse JSON string here
    ...
    ...

}


Related Searches to Python programming questions and answers pdf