Tuesday, 30 August 2016

Python Question & Answers SET-02

Ques 21 : consider the following function and tell What does the * in *args do?
def print_two(*args):
    arg1, arg2 = args
    print "arg1: %r, arg2: %r" % (arg1, arg2)

    That tells Python to take all the arguments to the function and then put them in args as a list. It's like argv that you've been using, but for functions. It's not normally used too often unless specifically needed.

Ques 22 : what does seek(0) ?
 seek() function is dealing in bytes, not lines. The code seek(0) moves the file to the 0 byte (first byte) in the file.

Ques 23 : How does readline() know where each line is?
Inside readline() is code that scans each byte of the file until it finds a \n character, then stops reading the file to return what it found so far. The file f is responsible for maintaining the current position in the file after each readline() call, so that it will keep reading each line.

Ques 24 : How can the words.pop function change the words variable?
That's a complicated question, but in this case words is a list, and because of that you can give it commands and it'll retain the results of those commands. This is similar to how files and many other things worked when you were working with them.
def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word
def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print word
def sort_words(words):
    """Sorts the words."""
    return sorted(words)

Ques 25 : Why does "test" and "test" return "test" or 1 and 1 return 1 instead of True?
Python and many languages like to return one of the operands to their boolean expressions rather than just True or False. This means that if you did False and 1 you get the first operand (False) but if you do True and 1 your get the second (1). Play with this a bit.

Ques 26 : Is there any difference between != and <>?
Python has deprecated <> in favor of !=, so use !=. Other than that there should be no difference.

Ques 27 : What does += mean?
The code x += 1 is the same as doing x = x + 1 but involves less typing. You can call this the "increment by" operator. The same goes for -= and many other expressions you'll learn later.'

Ques 28 : What happens if multiple elif blocks are True?
Python starts and the top runs the first block that is True, so it will run only the first one.

Ques 29 : How do I tell if a number is between a range of numbers?
You have two options: Use 0 < x < 10 or 1 <= x < 10, which is classic notation, or use x in range(1, 10).

Ques 30 : How do you make a 2-dimensional (2D) list?
That's a list in a list like this: [[1,2,3],[4,5,6]]

Ques 31 : Why does for i in range(1, 3): only loop two times instead of three times?'
The range() function only does numbers from the first to the last, not including the last. So it stops at two, not three in the above. This turns out to be the most common way to do this kind of loop.

Ques 32 : What's' the difference between a for-loop and a while-loop?
A for-loop can only iterate (loop) "over" collections of things. A while-loop can do any kind of iteration (looping) you want. However, while-loops are harder to get right and you normally can get many things done with for-loops.

Ques 33 : What does exit(0) do?
On many operating systems a program can abort with exit(0), and the number passed in will indicate an error or not. If you do exit(1) then it will be an error, but exit(0) will be a good exit. The reason its backward from normal boolean logic (with 0==False is that you can use different numbers to indicate different error results. You can do exit(100) for a different error result than exit(2) or exit(1).

Ques 34 : What does stuff[3:5] do?
That extracts a "slice" from the stuff list that is from element 3 to element 4, meaning it does not include element 5. It's similar to how range(3,5) would work.

Ques 35 : What is the difference between a list and a dictionary?'
A list is for an ordered list of items. A dictionary (or dict) is for matching some items (called "keys") to other items (called "values").

Ques 36 : What would I use a dictionary for?
When you have to take one value and "look up" another value. In fact you could call dictionaries "look up tables."

Ques 37 : What would I use a list for?
Use a list for any sequence of things that need to be in order, and you only need to look them up by a numeric index.

Ques 38 : What if I need a dictionary, but I need it to be in order?
Take a look at the collections.OrderedDict data structure in Python. Search for it online to find the documentation.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...