Pages

Saturday, March 18, 2017

Python interview questions and answers


What is Python?
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.

What is the purpose of PYTHONPATH environment variable?
PYTHONPATH - It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.

What is the purpose of PYTHONSTARTUP environment variable?
PYTHONSTARTUP - It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.

Is python a case-sensitive language?
Yes! Python is a case sensitive programming language.

What are the supported data types in Python?
Python has five standard data types −
Numbers
String
List
Tuple
Dictionary

What are tuples in Python?
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

What is the difference between tuples and lists in Python?
The main differences between lists and tuples are − Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.

What are Python's dictionaries?
Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

How will you create a dictionary in python?
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).

dict = {}
dict['one'] = "This is one"
dict[2]     = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}

How will you get all the keys from the dictionary?
Using dictionary.keys() function, we can get all the keys from the dictionary object.
print dict.keys()   # Prints all the keys

How will you get all the values from the dictionary?
Using dictionary.values() function, we can get all the values from the dictionary object.
print dict.values()   # Prints all the values

How will you convert a string to an int in python?
int(x [,base]) - Converts x to an integer. base specifies the base if x is a string.

How will you convert a string to a long in python?
long(x [,base] ) - Converts x to a long integer. base specifies the base if x is a string.

How will you convert a string to a float in python?
float(x) − Converts x to a floating-point number.

How will you convert a object to a string in python?
str(x) − Converts object x to a string representation.

How will you convert a object to a regular expression in python?
repr(x) − Converts object x to an expression string.

How will you convert a String to an object in python?
eval(str) − Evaluates a string and returns an object.

How will you convert a string to a tuple in python?
tuple(s) − Converts s to a tuple.

How will you convert a string to a list in python?
list(s) − Converts s to a list.

How will you convert a string to a set in python?
set(s) − Converts s to a set.

How will you create a dictionary using tuples in python?
dict(d) − Creates a dictionary. d must be a sequence of (key,value) tuples.

How will you convert an integer to a character in python?
chr(x) − Converts an integer to a character.

How will you convert a single character to its integer value in python?
ord(x) − Converts a single character to its integer value.

How will you convert an integer to hexadecimal string in python?
hex(x) − Converts an integer to a hexadecimal string.

What is the purpose of // operator?
// Floor Division − The division of operands where the result is the quotient in which the digits after the decimal point are removed.

What is the purpose of is operator?
is − Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. x is y, here is results in 1 if id(x) equals id(y).

What is the purpose break statement in python?
break statement − Terminates the loop statement and transfers execution to the statement immediately following the loop.

What is the purpose continue statement in python?
continue statement − Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

What is the purpose pass statement in python?
pass statement − The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

How can you get a random number in python?
random() − returns a random float r, such that 0 is less than or equal to r and r is less than 1.

How will you capitalizes first letter of string?
capitalize() − Capitalizes first letter of string.

How will you check in a string that all characters are digits?
isdigit() − Returns true if string contains only digits and false otherwise.

How will you check in a string that all characters are alphanumeric?
isalnum() − Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise.

How will you check in a string that all characters are in lowercase?
islower() − Returns true if string has at least 1 cased character and all cased characters are in lowercase and false otherwise.

How will you check in a string that all characters are numerics?
isnumeric() − Returns true if a unicode string contains only numeric characters and false otherwise.

How will you check in a string that all characters are whitespaces?
isspace() − Returns true if string contains only whitespace characters and false otherwise.

How will you check in a string that it is properly titlecased?
istitle() − Returns true if string is properly "titlecased" and false otherwise.

How will you check in a string that all characters are in uppercase?
isupper() − Returns true if string has at least one cased character and all cased characters are in uppercase and false otherwise.

How will you merge elements in a sequence?
join(seq) − Merges (concatenates) the string representations of elements in sequence seq into a string, with separator string.

How will you get the length of the string?
len(string) − Returns the length of the string.

How will you remove all leading whitespace in string?
lstrip() − Removes all leading whitespace in string.

How will you replace all occurrences of old substring in string with new string?
replace(old, new [, max]) − Replaces all occurrences of old in string with new or at most max occurrences if max given.

How will you remove all leading and trailing whitespace in string?
strip([chars]) − Performs both lstrip() and rstrip() on string.

How will you get titlecased version of string?
title() − Returns "titlecased" version of string, that is, all words begin with uppercase and the rest are lowercase.

What is the output of len([1, 2, 3])?
3.

What is the output of [1, 2, 3] + [4, 5, 6]?
[1, 2, 3, 4, 5, 6]

What is the output of ['Hi!'] * 4?
['Hi!', 'Hi!', 'Hi!', 'Hi!']

What is the output of 3 in [1, 2, 3]?
True

What is the output of for x in [1, 2, 3]: print x?
1 2 3

What is the output of L[2] if L = [1,2,3]?
3, Offsets start at zero.

What is the output of L[-2] if L = [1,2,3]?
L[-1] = 3, L[-2]=2, L[-3]=1

What is the output of L[1:] if L = [1,2,3]?
2, 3, Slicing fetches sections.

How will you compare two lists?
cmp(list1, list2) − Compares elements of both lists.

How will you get the length of a list?
len(list) − Gives the total length of the list.

How will you get the max valued item of a list?
max(list) − Returns item from the list with max value.

How will you get the min valued item of a list?
min(list) − Returns item from the list with min value.

How will you get the index of an object in a list?
list.index(obj) − Returns the lowest index in list that obj appears.

How will you remove last object from a list?
list.pop(obj=list[-1]) − Removes and returns last object or obj from list.

How will you remove last object from a list?
list.pop(obj=list[-1]) − Removes and returns last object or obj from list.

How will you remove an object from a list?
list.remove(obj) − Removes object obj from list.

How will you reverse a list?
list.reverse() − Reverses objects of list in place.

How will you sort a list?
list.sort([func]) − Sorts objects of list, use compare func if given.

## multiple assignment
>>> target_color_name = first_color_name = 'FireBrick'
>>> id(target_color_name) == id(first_color_name)
True
>>> print(target_color_name)
FireBrick
>>> print(first_color_name)
FireBrick
>>>

## word count
import sys

def count_words(filename):
    results = dict()
    with open(filename, 'r') as f:
        for line in f:
            for word in line.split():
                results[word] = results.setdefault(word, 0) + 1

    for word, count in sorted(results.items(), key=lambda x: x[1]):
        print('{} {}'.format(count, word))

count_words(sys.argv[1])

or

def count_words(file_path):
    try:
        with open(file_path, 'r') as file:
            text = file.read()
            word_count = len(text.split())
            print("Number of words:", word_count)
    except FileNotFoundError:
        print("File not found!")
    except Exception as e:
        print("An error occurred:", e)

# Example usage:
file_path = 'sample.txt'  # Replace 'sample.txt' with the path to your file
count_words(file_path)


## word list
from urllib.request import urlopen

with urlopen('http://sixty-north.com/c/t.txt') as story:
    story_words = []
    for line in story:
        line_words = line.split()
        for word in line_words:
            story_words.append(word)

print(story_words)

"""Demonstrate scoping."""

count = 0

def show_count():
    print("count = ", count)

def set_count(c):
    global count
    count = c
 
"""Module for demonstrating files."""
import sys
def main(filename):
  f = open(filename, mode='rt', encoding='utf-8')
  for line in f:
      sys.stdout.write(line)
  f.close()

if __name__ == '__main__':
  main(sys.argv[1])

## Write a function to add two numbers
def add_numbers(*args):
    total = 0
    for a in args:
        total += a
    print (total)

add_numbers(3)
add_numbers(3, 42)


or

def add_numbers(num1, num2):
    return num1 + num2

# Example usage:
result = add_numbers(5, 7)
print("Result:", result)  # Output: 12



### How to un-pack arguments
def health_calculator(age, apples_ate, cigs_smoked):
    answer = (100-age) + (apples_ate * 3.5) - (cigs_smoked * 2)
    print(answer)

data = [27,20,0]
health_calculator(data[0],data[1],data[2])
health_calculator(*data)

# Program to find a magic number using break
magicNumber = 26
for n in range(101):
    if n is magicNumber:
        print(n,"is the magic number!")
        break
    else:
        print(n)
     
# Example of continue
numbersTaken = [2,5,12,33,17]
print ("Here are the numbers that are still available:")
for n in range(1,20):
    if n in numbersTaken:
        continue
    print(n)
 
## Example of default arg
def get_gender(sex='Unknown'):
    if sex is "m":
        sex = "Male"
    elif sex is "f":
        sex = "Female"
    print(sex)

get_gender
get_gender('f')
get_gender()

## Example of for
foods = ['apple', 'banana', 'grapes', 'mango']
for f in foods:
    print(f)
    print(len(f))
 
## function example
def cisco():
    print("Python, fucntions are cool!")
cisco()

def bitcoin_to_usd(btc):
    amount = btc*527
    print(amount)

bitcoin_to_usd(3.85)

## Example of if else
name = "Lucy"
if name is "nawraj":
    print("hey there Nawraj")
elif name is "Lucy":
    print("What's up Lucy")
elif name is "Sammy":
    print("What's up Sammy")
else:
    print("Please sign up for the Site!")
 
## Example of key argument
def dumb_sentence(name='nawraj',action='ate',item='tuna'):
    print(name,action,item)

dumb_sentence()
dumb_sentence("Sally", "plays", "gently")
dumb_sentence(item ='awesome', action= 'is')

## Example of range
for x in range(10):
    print("Hello nawraj")
 
for x in range(5, 12):
    print(x)
for x in range(10, 40, 5):
    print(x)

## Example of return
def allowed_dating_age(my_age):
    girls_age = my_age/2 + 7
    return girls_age

Age_limit = allowed_dating_age(34)
print("You can date girls", Age_limit, "or older") 
allowed_dating_age(34)

## Example of set
groceries = {'cereal','milk','starcrunch','beer','duct tape','lotion','beer'}
print(groceries)

if 'milk' in groceries:
    print("You already have milk hoss!")
else:
    print("Oh yes you need milk!")
 
## Example of variable scope
#a = 1514
def test1():
    a = 1514
    print(a)

def test2():
    print(a)

test1()
test2()

## Example of dictionary
myDict = {'nawraj': 'lekhak','gayatri': 'Paneru'}
print(myDict)
print(myDict['nawraj'])
print(myDict["nawraj"])

varstr = "this is nawraj " \
         "from cisco systems India " \
         "private limited."
      
      
## Example for prime number
for num in range(2,20):
    if num%2 == 0:
        print ('num is not prime')
        #break
    else:
        print ('num is prime number')

x = range(2,9)
print(x)

## Example of key value
names = {'a': 1, 'b': 2, 'c': 3}
print(names)
x = names.keys()
print(x)
print(names['c'])

## Program for fibonacci
def fib(n):
    a,b = 0,1
    while b < n:
        print(b)
        a,b = b , a+b

fib(3)

or

Certainly! Below is a Python program to generate the Fibonacci sequence up to a specified number of terms:

```python
def fibonacci(n):
    fib_sequence = []
    if n <= 0:
        return fib_sequence
    elif n == 1:
        fib_sequence.append(0)
        return fib_sequence
    else:
        fib_sequence = [0, 1]
        while len(fib_sequence) < n:
            next_num = fib_sequence[-1] + fib_sequence[-2]
            fib_sequence.append(next_num)
        return fib_sequence

# Example usage:
num_terms = 10
fib_sequence = fibonacci(num_terms)
print("Fibonacci sequence with", num_terms, "terms:", fib_sequence)
```

This program defines a function `fibonacci` that takes an integer `n` as input and returns a list containing the Fibonacci sequence up to the `n`th term. The sequence starts with 0 and 1, and each subsequent term is the sum of the two preceding terms.

You can change the value of `num_terms` to generate the Fibonacci sequence with a different number of terms.


## Program to check vowel
letter = 'o'
if letter == 'a' or letter == 'e' or letter == 'i' \
or letter == 'o' or letter == 'u':
    print (letter, 'is a vowel')
else:
    print(letter, 'is not a vowel')
     
### Example of multi-dimensional array
rows = range(1,4)
cols = range(1,3)

cells = [(row,col) for row in rows for col in cols]
for cell in cells:
    print(cell)
 
## decorator example
def document_it(func):
    def new_function(*args, **kwargs):
        print('Running function:', func.__name__)
        print('Positional arguments:', args)
        print('keyword arguments:', kwargs)
        result = func(*args, **kwargs)
        print('Result:', result)
        return result
    return new_function

def add_ints(a,b):
    return a + b

add_ints(3,5)

cooler_add_ints = document_it(add_ints)
cooler_add_ints(3,5)
#or
@document_it
def add_ints(a,b):
    return a + b

add_ints(3,5)

## you can have more than one decorator for a function

def square_it(func):
    def new_function(*args, **kwargs):
        result = func(*args, **kwargs)
        return result*result
    return new_function

@document_it
@square_it
def add_ints(a,b):
    return a+b
add_ints(3,5)

###Inheritance
class Car():
    pass

##sub class
class Yugo(Car):
    pass

##create object for each class
give_me_a_car = Car()
give_me_a_yugo = Yugo()

class Car():
    def exclaim(self):
        print('I m a Car')

class Yugo(Car):
    pass

## make one object from each class and call the exclaim method
give_me_a_car = Car()
give_me_a_yugo = Yugo()
give_me_a_car.exclaim()
give_me_a_yugo.exclaim()

##over-ride a method
class Car():
    def exclaim(self):
        print('I am a Car!')

class Yugo(Car):
    def exclaim(self):
        print("I am a Yugo much like car, but more Yugo-ish")

# now make two object from these class
give_me_a_car = Car()
give_me_a_yugo = Yugo()

give_me_a_car.exclaim()
give_me_a_yugo.exclaim()

#super class
class Person():
    def __init__(self, name):
        self.name = name

class EmailPerson(Person):
    def __init__(self, name, email):
        super().__init__(name)
        self.email = email

nawraj = EmailPerson('Nawraj Lekhak','lekhak123@hotmail.com')

print(nawraj.name)
print(nawraj.email)

## regular expression
import re
result = re.match('You', 'Young Frankenstein')
print(result)
#or
source = 'Young Frankenstein'
m = re.match('You', source)
if m:
    print(m.group())  ### print matched value

m = re.match('^You', source)
if m:
    print(m.group()) 

m = re.match('Frank', source)
if m:
    print(m.group())

m = re.search('Frank', source)
if m:
    print(m.group())

m = re.search('.*Frank', source)
if m:
    print(m.group())

m = re.findall('n', source)
print(m)

m = re.findall('n.', source)
print(m)

m = re.findall('n.?', source)
print(m)

m = re.split('n', source)
print(m)

m = re.sub('n', '?', source)
print(m)

## greatest of three
n1 = '902'
n2 = '100'
n3 = '666'

if (n1 > n2) and (n1 > n3):
    print("The greatest number is %s " % n1)
elif (n2 > n1) and (n2 > n3):
    print("The greatest number is %s " % n2)
else:
    print("The greatest number is %s " % n3)

''' odd or even number'''
num = input("Enter a number: ")
mod = num % 2
if mod > 0:
    print (num, "is an odd number")
else:
    print (num, "is an even number")

#### print all elements less than 5
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
num = int(raw_input("Choose a number:"))
new_list = []

for i in a:
    if i < num:
        new_list.append(i)

print(new_list)'''

## current date  and time

import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))

### check whether file exists
import os.path
open('abc.txt', 'w')
print(os.path.isfile('abc.txt'))

###Verify python shell is 32 bit or 64 bit
import struct
print(struct.calcsize("P")*8)

### path and name of file currently executing
import os
print("Current file name: ", os.path.realpath(__file__))

## list all files in dir
from os import listdir
from os.path import isfile, join
files_list = [f for f in listdir('/Users/nawlekha/Desktop/pyATS') if isfile(join('/Users/nawlekha/Desktop/pyATS',f))]
print(files_list)

### python program to access environment variables
import os
print('**********************')
print(os.environ)
print(os.environ['HOME'])
print(os.environ['PATH'])

## get current username
import getpass
print(getpass.getuser())

### convert decimal to hexadecimal
x = 30
print(format(x, '02x'))
x = 4
print(format(x, '02x'))

### program to check valid ip address
import socket
addr = '127.0.0.2561'
try:
    socket.inet_aton(addr)
    print("Valid IP")
except socket.error:
    print("Invalid IP")

## factorial
def fact(x):
    if x == 0:
        return 1
    return x * fact(x - 1)

x = int(input())
print(fact(x))'''

'''
Question:
Define a class which has at least two methods:
getString: to get a string from console input
printString: to print the string in upper case.
Also please include simple test function to test the class methods.
'''

class InputOutString(object):
    def __init__(self):
        self.s = ""

    def getString(self):
        self.s = input()

    def printString(self):
        print(self.s.upper())

strObj = InputOutString()
strObj.getString()
strObj.printString()

##Write a program that accepts a sentence and calculate the number of letters and digits.
s = input()
d = {"DIGITS": 0, "LETTERS": 0}
for c in s:
    if c.isdigit():
        d["DIGITS"]+=1
    elif c.isalpha():
        d["LETTERS"]+=1
    else:
        pass
print("LETTERS", d["LETTERS"])
print("DIGITS", d["DIGITS"])
'''

###Define a class, which have a class parameter and have the same instance parameter.
class Person:
    # Define the class parameter "name"
    name = "Person"

    def __init__(self, name = None):
        # self.name is the instance parameter
        self.name = name

nawraj = Person("nawraj")
print ("%s name is %s" % (Person.name, nawraj.name))

lekhak = Person()
lekhak.name = "lekhak"
print ("%s name is %s" % (Person.name, lekhak.name))

### sum of two numbers
def Sum(n1, n2):
    return n1+n2
print(Sum(1,2))

### Define a class named American and its subclass NewYorker.
class American(object):
    pass
class NewYorker(American):
    pass

anAmerican = American()
aNewYorker = NewYorker()
print(anAmerican)
print(aNewYorker)

###Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area.
class Circle(object):
    def __init__(self,r):
        self.radius = r

    def area(self):
        return self.radius**2*3.14

aCircle = Circle(2)
print(aCircle.area())

#Write a Python program to check that a string contains only a certain set of characters (in this case a-z, A-Z and 0-9).
import re
def check_specific_char(string):
    charRe = re.compile(r'[^a-zA-Z0-9]')
    string = charRe.search(string)
    return not bool(string)

print(check_specific_char("ABCDEFabcdef123456780"))
print(check_specific_char("*&%#!}{"))

#Write a Python program that matches a word containing 'z'.
import re
def text_match(txt):
    pattern = '\w*z. \w*'
    if re.search(pattern, txt):
        return 'match found!'
    else:
        return 'match not found'

print(text_match("the quick brown fox jumps over the lazy dog."))
print(text_match('python exercise'))

'''
## Write a Python program to reverse a string word by word.
class str_reverse:
    def reverse_words(self):
        return ''.join(reversed(s.split()))

print(str_reverse().reverse_words('hello.py'))
'''

or

Sure, here's a Python program to reverse a string word by word:

```python
def reverse_words(sentence):
    # Split the sentence into words
    words = sentence.split()
    # Reverse the order of words
    reversed_words = words[::-1]
    # Join the words back into a sentence
    reversed_sentence = ' '.join(reversed_words)
    return reversed_sentence

# Example usage:
sentence = "Hello World"
reversed_sentence = reverse_words(sentence)
print("Original sentence:", sentence)
print("Reversed sentence:", reversed_sentence)
```

Output:
```
Original sentence: Hello World
Reversed sentence: World Hello
```

In this program:

- The `reverse_words` function takes a string `sentence` as input.
- The `split` method is used to split the sentence into individual words, which are stored in the `words` list.
- The order of words in the `words` list is reversed using slicing with `[::-1]`.
- The `join` method is used to join the reversed words back into a string, with space as the separator.
- The reversed sentence is returned.

You can replace the value of `sentence` with any other string to reverse it word by word.


## strings are immutable sequence of unicode codepoints

''' this is
...a
...multiline
...string.'''

m = 'this string\nspans multiple\n lines.'
print(m)

n = 'this is a \" and a \' in a string.'
print(n)


## manipulate list
b = []
b.append(1.64)
print(b)
b.append(3)
print(b)

def square(x):
    return x*x
print(square(5))

''' __name__ is a special python attribute used to evaluates to "__main__" or the actual
...mddule name depending on how the enclosing module is being used'''

## number of blank line between function should be 2

## shebang #!/usr/bin/ is used to determine which interpreter is used to run program

## Example on variable
varString = "This is a string variable.  It can have " \
            "any combination of letters, numbers, or " \
            "special characters."

varInteger = 32

varLong = 12345

varFloat = 1290.60

varList = [1, 2, 3.5, "Ben", "Alice", "Sam"]

varTuple = (4, 5, 6.5, "Alex", "Barbara", "Amy")

varDictionary = { 'First': 'The first item in the dictionary',
                  'Second' : 'The second item in the dictionary' }

print(varString)
print(varInteger)
print(varLong)
print(varFloat)
print(varList)
print(varTuple)
print(varDictionary)

## input/output
#from __future__ import print_function
userName = input('Please enter your name:' )
finalName = userName + '!'
print('Hello', finalName)

##Functions
def exp(x,y):
    z = x ** y
    print(z)
 
exp(1,2)

def printFib(n):
    a, b = 0,1
    while b < n:
        print(b)
        a, b = b, a+b
     
printFib(4)

def byValExample(x):
    x = "This value is overridden!"
    print(x)
 
byValExample(7)

## Example of OOP
def calcCircumference(radius):
    return math.pi * 2 * radius

class Circle:
    pass

circle1 = Circle()

circle1.radius = 4.2

print(circle1.radius)

circle2 = Circle()
circle2.radius = 3.9
print(circle2.radius)

### Inheritance
class Shape():
    def __init__(self):
        self.color = "Red"
        self.sides = 0

class Square(Shape):
    def __init__(self, w, c):
        Shape.__init__(self)
        self.width = w
        self.color = c

sq1 = Square(5, "Blue")
sq2 = Square(9, "Green")

print("Square Sizes:",sq1.width,"x",sq1.sides,sq1.color,
      ",",sq2.width,"x",sq2.sides,sq2.color)

words = "why sometimes i have believed as many six impossible things before".split()
print(words)

print([len(word) for word in words])

#from math import factorial
#f = [len(str(factorial(x))) for x in range(20)]
#print(f)

## Example of dictionary comprehension
from pprint import pprint as pp
country_to_capital = {'United kingdom': 'London','Brazil': 'Brazilia','Morocco': 'Rabat','Sweden': 'Stockholm'}
pp(country_to_capital.items())
capital_to_country = {capital: country for country, capital in country_to_capital.items()}
pp(capital_to_country)

## filtering predicates
from math import sqrt
def is_prime(x):
    if x < 2:
        return False
    for i in range(2, int(sqrt(x)) + 1):
        if x % i == 0:
            return False
    return True

print([x for x in range(101) if is_prime(x)])

## Generators. It is kind of iterator
def gen123():
    yield 1
    yield 2
    yield 3

g = gen123()
print(next(g))
print(next(g))
print(next(g))

def gen246():
    print("About to yield 2")
    yield 2
    print("About to yield 4")
    yield 4
    print("About to yield 6")
    yield 6
    print("About to return")

h = gen246()
print(next(h))
print(next(h))
print(next(h))


import sys
print(sys.getdefaultencoding())

### word count
from urllib.request import urlopen

with urlopen('http://sixty-north.com/c/t.txt') as story:
    story_words = []
    for line in story:
        line_words = line.decode('utf-8').split()
        for word in line_words:
            story_words.append(word)
print(story_words)

## word count
from urllib.request import urlopen
with urlopen('http://sixty-north.com/c/t.txt') as story:
    story_words = []
    for line in story:
        line_words = line.split()
        for word in line_words:
            story_words.append(word)

print(story_words)

## multiple assignment

target_color_name = first_color_name = 'FireBrick'
id(target_color_name) == id(first_color_name)

## How are the functions help() and dir() different?
These are the two functions that are accessible from the Python Interpreter. These two functions are used for viewing a consolidated dump of built-in functions.

help() - it will display the documentation string. It is used to see the help related to modules, keywords, attributes, etc.

 dir() - will display the defined symbols.
 Eg: >>>dir(str) – will only display the defined symbols.

>>> help(str)

>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>>

## Explain Python's zip() function.?
zip() function- it will take multiple lists say list1, list2, etc and transform them into a single list of tuples by taking the corresponding elements of the lists that are passed as parameters. Eg:

>>> list1 = ['A',
... 'B','C']
>>> list2 = [10,20,30]
>>> list(zip(list1, list2))
[('A', 10), ('B', 20), ('C', 30)]
whenever the given lists are of different lengths, zip stops generating tuples when the first list ends.

## Explain Python's pass by references Vs pass by value. (or) Explain about Python's parameter passing mechanism?
In Python, by default, all the parameters (arguments) are passed “by reference” to the functions. Thus, if you change the value of the parameter within a function, the change is reflected in the calling function.We can even observe the pass “by value” kind of a behavior whenever we pass the arguments to functions that are of type say numbers, strings, tuples. This is because of the immutable nature of them.

## As Everything in Python is an Object, Explain the characteristics of Python's Objects.
As Python’s Objects are instances of classes, they are created at the time of instantiation. Eg: object-name = class-name(arguments)
one or more variables can reference the same object in Python
Every object holds unique id and it can be obtained by using id() method. Eg: id(obj-name) will return unique id of the given object.
every object can be either mutable or immutable based on the type of data they hold.
Whenever an object is not being used in the code, it gets destroyed automatically garbage collected or destroyed
contents of objects can be converted into string representation using a method

## Explain how to overload constructors or methods in Python.
Python’s constructor – _init__ () is a first method of a class. Whenever we try to instantiate a object __init__() is automatically invoked by python to initialize members of an object.

## Which statement of Python is used whenever a statement is required syntactically but the program needs no action?
Pass – is no-operation / action statement in Python
If we want to load a module or open a file, and even if the requested module/file does not exist, we want to continue with other tasks. In such a scenario, use try-except block with pass statement in the except block.
Eg:
 try:import mymodulemyfile = open(“C:\myfile.csv”)except:pass

## Explain the use of with
In python generally “with” statement is used to open a file, process the data present in the file, and also to close the file without calling a close() method. “with” statement makes the exception handling simpler by providing cleanup activities.
General form of with:
with open(“file name”, “mode”) as file-var:
processing statements
note: no need to close the file by calling close() upon file-var.close()

## Explain all the file processing modes supported by Python?
Python allows you to open files in one of the three modes. They are:
read-only mode, write-only mode, read-write mode, and append mode by specifying the flags “r”, “w”, “rw”, “a” respectively.
A text file can be opened in any one of the above said modes by specifying the option “t” along with
“r”, “w”, “rw”, and “a”, so that the preceding modes become “rt”, “wt”, “rwt”, and “at”.A binary file can be opened in any one of the above said modes by specifying the option “b” along with “r”, “w”, “rw”, and “a” so that the preceding modes become “rb”, “wb”, “rwb”, “ab”.

## When does a dictionary is used instead of a list?
Dictionaries – are best suited when the data is labelled, i.e., the data is a record with field names.
lists – are better option to store collections of un-labelled items say all the files and sub directories in a folder.
Generally Search operation on dictionary object is faster than searching a list object.

## What is the use of enumerate() in Python?
Using enumerate() function you can iterate through the sequence and retrieve the index position and its corresponding value at the same time.
>>> for i,v in enumerate([‘Python’,’Java’,’C++’]):
print(i,v)
0 Python
1 Java
2 C++

## How many kinds of sequences are supported by Python? What are they?
Python supports 7 sequence types. They are str, list, tuple, unicode, bytearray, xrange, and buffer. where xrange is deprecated in python 3.5.X.

## Name few methods for matching and searching the occurrences of a pattern in a given text String ?
There are 4 different methods in “re” module to perform pattern matching. They are:
match() – matches the pattern only to the beginning of the String. search() – scan the string and look for a location the pattern matches findall() – finds all the occurrences of match and return them as a list
finditer() – finds all the occurrences of match and return them as an iterator.

## Explain split(), sub(), subn() methods of
To modify the strings, Python’s “re” module is providing 3 methods. They are:
split() – uses a regex pattern to “split” a given string into a list.
sub() – finds all substrings where the regex pattern matches and then replace them with a different string
subn() – it is similar to sub() and also returns the new string along with the no. of
replacements.

## How to display the contents of text file in reverse order?
convert the given file into a list.
 reverse the list by using reversed()
Eg: for line in reversed(list(open(“file-name”,”r”))):
print(line)

## What is TkInter?
TkInter is Python library. It is a toolkit for GUI development. It provides support for various GUI tools or widgets (such as buttons, labels, text boxes, radio buttons, etc) that are used in GUI applications. The common attributes of them include Dimensions, Colors, Fonts, Cursors, etc.

## Is Python object oriented? what is object oriented programming?
Yes. Python is Object Oriented Programming language. OOP is the programming paradigm based on classes and instances of those classes called objects. The features of OOP are:
Encapsulation, Data Abstraction, Inheritance, Polymorphism.

## What is a Class? How do you create it in Python?
A class is a blue print/ template of code /collection of objects that has same set of attributes and behaviour. To create a class use the keyword class followed by class name beginning with an uppercase letter. For example, a person belongs to class called Person class and can have the attributes (say first-name and last-name) and behaviours / methods (say showFullName()). A Person class can be defined as:
class Person():
#method
def inputName(self,fname,lname): self.fname=fname self.lastname=lastname
#method
def showFullName() (self):
print(self.fname+" "+self.lname)person1 = Person() #object instantiation person1.inputName("Ratan","Tata") #calling a method inputName person1. showFullName() #calling a method showFullName()
Note: whenever you define a method inside a class, the first argument to the method must be self (where self – is a pointer to the class instance). self must be passed as an argument to the method, though the method does not take any arguments

## Explain Inheritance in Python with an example.
Inheritance allows One class to gain all the members(say attributes and methods) of another class. Inheritance provides code reusability, makes it easier to create and maintain an application. They are different types of inheritance supported by Python. They are: single, multi-level, hierarchical and multiple inheritance. The class from which we are inheriting is called super-class and the class that is inherited is called a derived / child class.
Single Inheritance – where a derived class acquires the members of a single super class.
multi-level inheritance – a derived class d1 in inherited from base class base1, and d2 is inherited from base2.
hierarchical inheritance – from one base class you can inherit any number of child classes
multiple inheritance – a derived class is inherited from more than one base class.
ex:
class ParentClass:
v1 = "from ParentClass - v1"
v2 = "from ParentClass - v2"class ChildClass(ParentClass):
passc = ChildClass() print(c.v1) print(c.v2)

## How instance variables are different from class variables?
Instance variables: are the variables in an object that have values that are local to that object. Two objects of the same class maintain distinct values for their variables. These variables are accessed with “object-name.instancevariable-name”.

class variables: these are the variables of class. All the objects of the same class will share value of “Class variables. They are accessed with their class name alone as “class- name.classvariable-name”. If you change the value of a class variable in one object, its new value is visible among all other objects of the same class. In the Java world, a variable that is declared as static is a class variable.

## How is Inheritance and Overriding methods are related?
If class A is a sub class of class B, then everything in B is accessible in /by class A. In addition, class A can define methods that are unavailable in B, and also it is able to override methods in B. For Instance, If class B and class A both contain a method called func(), then func() in class B can override func() in class A. Similarly, a method of class A can call another method defined in A that can invoke a method of B that overrides it.

## Differentiate between append() and extend() methods. ?
Both append() and extend() methods are the methods of list. These methods a re used to add the elements at the end of the list.
append(element) – adds the given element at the end of the list which has called this method.
extend(another-list) – adds the elements of another-list at the end of the list which is called the extend method.

## Looking at the below code, write down the final values of A0, A1, ...An.
A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
A1 = range(10)
A2 = sorted([i for i in A1 if i in A0])
A3 = sorted([A0[s] for s in A0])
A4 = [i for i in A1 if i in A3]
A5 = {i:i*i for i in A1}
A6 = [[i,i*i] for i in A1]

Answer
A0 = {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}  # the order may vary
A1 = range(0, 10) # or [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] in python 2
A2 = []
A3 = [1, 2, 3, 4, 5]
A4 = [1, 2, 3, 4, 5]
A5 = {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
A6 = [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]

## What does this code output:
def f(x,l=[]):
    for i in range(x):
        l.append(i*i)
    print(l)

f(2)
f(3,[3,2,1])
f(3)

Answer
[0, 1]
[3, 2, 1, 0, 1, 4]
[0, 1, 0, 1, 4]

Hint
The first function call should be fairly obvious, the loop appends 0 and then 1 to the empty list, l. l is a name for a variable that points to a list stored in memory. The second call starts off by creating a new list in a new block of memory. l then refers to this new list. It then appends 0, 1 and 4 to this new list. So that's great. The third function call is the weird one. It uses the original list stored in the original memory block. That is why it starts off with 0 and 1.

Try this out if you don't understand:

l_mem = []

l = l_mem           # the first call
for i in range(2):
    l.append(i*i)

print(l)            # [0, 1]

l = [3,2,1]         # the second call
for i in range(3):
    l.append(i*i)

print(l)            # [3, 2, 1, 0, 1, 4]

l = l_mem           # the third call
for i in range(3):
    l.append(i*i)

print(l)            # [0, 1, 0, 1, 4]

## What does this stuff mean: *args, **kwargs? And why would we use it?
Answer
Use *args when we aren't sure how many arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function. **kwargs is used when we dont know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments.

Here is a little illustration:

def f(*args,**kwargs): print(args, kwargs)

l = [1,2,3]
t = (4,5,6)
d = {'a':7,'b':8,'c':9}

f()
f(1,2,3)                    # (1, 2, 3) {}
f(1,2,3,"groovy")           # (1, 2, 3, 'groovy') {}
f(a=1,b=2,c=3)              # () {'a': 1, 'c': 3, 'b': 2}
f(a=1,b=2,c=3,zzz="hi")     # () {'a': 1, 'c': 3, 'b': 2, 'zzz': 'hi'}
f(1,2,3,a=1,b=2,c=3)        # (1, 2, 3) {'a': 1, 'c': 3, 'b': 2}

f(*l,**d)                   # (1, 2, 3) {'a': 7, 'c': 9, 'b': 8}
f(*t,**d)                   # (4, 5, 6) {'a': 7, 'c': 9, 'b': 8}
f(1,2,*t)                   # (1, 2, 4, 5, 6) {}
f(q="winning",**d)          # () {'a': 7, 'q': 'winning', 'c': 9, 'b': 8}
f(1,2,*t,q="winning",**d)   # (1, 2, 4, 5, 6) {'a': 7, 'q': 'winning', 'c': 9, 'b': 8}

def f2(arg1,arg2,*args,**kwargs): print(arg1,arg2, args, kwargs)

f2(1,2,3)                       # 1 2 (3,) {}
f2(1,2,3,"groovy")              # 1 2 (3, 'groovy') {}
f2(arg1=1,arg2=2,c=3)           # 1 2 () {'c': 3}
f2(arg1=1,arg2=2,c=3,zzz="hi")  # 1 2 () {'c': 3, 'zzz': 'hi'}
f2(1,2,3,a=1,b=2,c=3)           # 1 2 (3,) {'a': 1, 'c': 3, 'b': 2}

f2(*l,**d)                   # 1 2 (3,) {'a': 7, 'c': 9, 'b': 8}
f2(*t,**d)                   # 4 5 (6,) {'a': 7, 'c': 9, 'b': 8}
f2(1,2,*t)                   # 1 2 (4, 5, 6) {}
f2(1,1,q="winning",**d)      # 1 1 () {'a': 7, 'q': 'winning', 'c': 9, 'b': 8}
f2(1,2,*t,q="winning",**d)   # 1 2 (4, 5, 6) {'a': 7, 'q': 'winning', 'c': 9, 'b': 8}

## What do these mean to you: @classmethod, @staticmethod, @property?
These are decorators. A decorator is a special kind of function that either takes a function and returns a function, or takes a class and returns a class. The @ symbol is just syntactic sugar that allows you to decorate something in a way that's easy to read.

@my_decorator
def my_func(stuff):
    do_things

Is equivalent to

def my_func(stuff):
    do_things

my_func = my_decorator(my_func)

## Consider the following code, what will it output?
class A(object):
    def go(self):
        print("go A go!")
    def stop(self):
        print("stop A stop!")
    def pause(self):
        raise Exception("Not Implemented")

class B(A):
    def go(self):
        super(B, self).go()
        print("go B go!")

class C(A):
    def go(self):
        super(C, self).go()
        print("go C go!")
    def stop(self):
        super(C, self).stop()
        print("stop C stop!")

class D(B,C):
    def go(self):
        super(D, self).go()
        print("go D go!")
    def stop(self):
        super(D, self).stop()
        print("stop D stop!")
    def pause(self):
        print("wait D wait!")

class E(B,C): pass

a = A()
b = B()
c = C()
d = D()
e = E()

# specify output from here onwards

a.go()
b.go()
c.go()
d.go()
e.go()

a.stop()
b.stop()
c.stop()
d.stop()
e.stop()

a.pause()
b.pause()
c.pause()
d.pause()
e.pause()

Answer

The output is specified in the comments in the segment below:

a.go()
# go A go!

b.go()
# go A go!
# go B go!

c.go()
# go A go!
# go C go!

d.go()
# go A go!
# go C go!
# go B go!
# go D go!

e.go()
# go A go!
# go C go!
# go B go!

a.stop()
# stop A stop!

b.stop()
# stop A stop!

c.stop()
# stop A stop!
# stop C stop!

d.stop()
# stop A stop!
# stop C stop!
# stop D stop!

e.stop()
# stop A stop!

a.pause()
# ... Exception: Not Implemented

b.pause()
# ... Exception: Not Implemented

c.pause()
# ... Exception: Not Implemented

d.pause()
# wait D wait!

e.pause()
# ...Exception: Not Implemented

## What is lambda in Python?
It is a single expression anonymous function often used as inline function.

## In Python what is slicing?
A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing.

## What are generators in Python?
The way of implementing iterators are known as generators.  It is a normal function except that it yields expression in the function.

## What is docstring in Python?
A Python documentation string is known as docstring, it is a way of documenting Python functions, modules and classes.

## How can you copy an object in Python?
To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.

## What is the difference between Xrange and range?
Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is.

## What will be the output of the code below? 
def extendList(val, list=[]):
list.append(val)
return list

list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList('a')

print ("list1 = %s" % list1)
print ("list2 = %s" % list2)
print ("list3 = %s" % list3)

'''
Output:

list1 = [10, 'a']
list2 = [123]
list3 = [10, 'a']
'''

## What will be the output of the code below? 

def multipliers():
  return [lambda x : i * x for i in range(4)]

print ([m(2) for m in multipliers()])

'''
The output of the above code will be [6, 6, 6, 6] (not [0, 2, 4, 6]).
'''

## What will be the output of the code below? 

class Parent(object):
    x = 1

class Child1(Parent):
    pass

class Child2(Parent):
    pass

print (Parent.x, Child1.x, Child2.x)
Child1.x = 2
print (Parent.x, Child1.x, Child2.x)
Parent.x = 3
print (Parent.x, Child1.x, Child2.x)

'''
The output of the above code will be:

1 1 1
1 2 1
3 2 3
'''

## What will be the output of the code below in Python
def div1(x,y):
    print ("%s/%s = %s" % (x, y, x/y))

def div2(x,y):
    print ("%s//%s = %s" % (x, y, x//y))

div1(5,2)
div1(5.,2)
div2(5,2)
div2(5.,2.)

'''
5/2 = 2.5
5.0/2 = 2.5
5//2 = 2
5.0//2.0 = 2.0
'''

## What will be the output of the code below?

list = ['a', 'b', 'c', 'd', 'e']
print (list[10:])

'''
The above code will output []
'''

## What will be the output of lines ?
list = [ [ ] ] * 5
print(list)
list[0].append(10)
print(list)
list[1].append(20)
print(list)
list.append(30)
print(list)

'''
The output will be as follows:

[[], [], [], [], []]
[[10], [10], [10], [10], [10]]
[[10, 20], [10, 20], [10, 20], [10, 20], [10, 20]]
[[10, 20], [10, 20], [10, 20], [10, 20], [10, 20], 30]
'''

'''
Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are:
(a) even numbers, and
(b) from elements in the original list that had even indices
'''

list = [ 1 , 3 , 5 , 8 , 10 , 13 , 18 , 36 , 78 ]
print([x for x in list[::2] if x%2 == 0])

'''
Output is : [10, 18, 78]
'''

## Name five modules that are included in python by default?
datetime           (used to manipulate date and time)
re                         (regular expressions)
urllib, urllib2  (handles many HTTP things)
string                  (a collection of different groups of strings for example all lower_case letters etc)
itertools            (permutations, combinations and other useful iterables)
ctypes                (from python docs: create and manipulate C data types in Python)
email                  (from python docs: A package for parsing, handling, and generating email messages)

## What is a docstring?
docstring is the documentation string for a function. It can be accessed by

function_name.__doc__

it is declared as:
def function_name():
"""your docstring"""
Writing documentation for your progams is a good habit and makes the code more understandable and reusable.

What is list comprehension?
Creating a list by doing some operation over data that can be accessed using an iterator. For eg:
>>>[ord(i) for i in string.ascii_uppercase]
     [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
 >>>

 What would the following code yield?
word = 'abcdefghij'
print word[:3] + word[3:]
Ans: ‘abcdefghij’ will be printed.
This is called string slicing. Since here the indices of the two slices are colliding, the string slices are ‘abc’ and ‘defghij’. The ‘+’ operator on strings concatenates them. Thus, the two slices formed are concatenated to give the answer ‘abcdefghij’.

Optimize these statements as a python programmer.
word = 'word'
print word.__len__()
Ans:

word = 'word'
print(len(word))

Write a program to print all the contents of a file
try:
    with open('sample.py','r') as f:
        print (f.read())
except IOError:
    print ("no such file exists")

Print the length of each line in the file ‘file.txt’ not including any whitespaces at the end of the lines.
with open("sample.py", "r") as f1:
  print(len(f1.readline().rstrip()))

rstrip() is an inbuilt function which strips the string from the right end of spaces or tabs (whitespace characters).

Print the sum of digits of numbers starting from 1 to 100 (inclusive of both)
print sum(range(1,101))
range() returns a list to the sum function containing all the numbers from 1 to 100. Please see that the range function does not include the end given (101 here).

Create a new list that converts the following list of number strings to a list of numbers.
>>> num_strings = ['1','21','53','84','50','66','7','38','9']
>>> [int(i) for i in num_strings]
[1, 21, 53, 84, 50, 66, 7, 38, 9]

Create two new lists one with odd numbers and other with even numbers
num_strings = [1,21,53,84,50,66,7,38,9]
>>> even = [i for i in num_strings if i%2==0]
>>> print(even)
[84, 50, 66, 38]
>>> odd = [i for i in num_strings if i%2==1]
>>> print(odd)
[1, 21, 53, 7, 9]

The following code is supposed to remove numbers less than 5 from list n, but there is a bug. Fix the bug.
n = [1,2,5,10,3,100,9,24]
for e in n:
  if e<5: p="">    n.remove(e)
  print n

## after e is removed, the index position gets disturbed. Instead it should be:
a=[]
for e in n:
  if e >= 5:
    a.append(e)
n = a

OR again a list comprehension
print([i for i in n if i >= 5])


What will be the output of the following
def func(x,*y,**z):
....    print z

func(1,2,3)

Here the output is :

{}  #Empty Dictionay

x is a normal value, so it takes 1..
y is a list of numbers, so it takes 2,3..
z wants named parameters, so it can not take any value here.
Thus the given answer.

Write a program to swap two numbers.
a = 5
b = 9
>>> a = 5
>>> b = 4
>>> a,b = b,a
>>> print(a)
4
>>> print(b)
5

What will be the output of the following code
class C(object):
....    def__init__(self):
....        self.x =1

c=C()
print c.x
print c.x
print c.x
print c.x
Ans.

All the outputs will be 1, since the value of the the object’s attribute(x) is never changed.

1
1
1
1

x is now a part of the public members of the class C.
Thus it can be accessed directly

What all options will work?
a.
n = 1
print n++   ## no such operator in python (++)

b.
n = 1
print ++n   ## no such operator in python (++)

c.
n = 1
print n += 1  ## will work

d.
int n = 1
print n = n+1 ##will not work as assignment can not be done in print command like this

e.
n =1
n = n+1      ## will work

Remove the whitespaces from the string.
s = ‘aaa bbb ccc ddd eee’
''.join(s.split())

What does the below mean?
s = a + ‘[‘ + b + ‘:’ + c + ‘]’
seems like a string is being concatenated. Nothing much can be said without knowing types of variables a, b, c. Also, if all of the a, b, c are not of type string, TypeError would be raised. This is because of the string constants (‘[‘ , ‘]’) used in the statement.

2 comments:

  1. Really good information to show through this blog. I really appreciate you for all the valuable information that you are providing us through your blog. python online course

    ReplyDelete