Wednesday, 30 April 2014

Python interview questions


  1. Python assignments and get user inputs
•a = b = c = 1
•a, b, c = 1, 2, "john“
•counter = 100 # An integer assignment
•miles = 1000.0 # A floating point
•name = "John" # A string
•To get input
Raw_input()
Name = Raw_input(“enter some name”).


2. Quotation in python


•Python allows ‘, “,”’
•Triple quotes are used to enter a string in multiple lines
•# is used for comment
•; is used to enter multiple statement in a single line
•import sys; x = 'foo'; sys.stdout.write(x + '\n')

•Print r’C:\\local\hema.txt’
•Is will allows to print the characters as such without processing the escape sequence


3. python datatypes

•string
•Number (int, long, float, complex) immutable
•Array is basic python are list of values that contain mixed datatype
•List []
•Tuple ()- immutable
•Dictionary {}
•Deleting obj
•del var
•del var_a, var_b
•Sequence => list, tuple, string are eg of sequences


4. File Operations


•File1 = open(filename,’r’)
•‘r’,’w’,’a’ => read, write, append
•Read -> for reading a long string
•Readlines -> for reading list of lines
•Write -> writing a string
•Writelines -> writing a list to a file
•F.tell() -> tells current position in file
•F.seek() -> moves to other position in a file

5. Concatenation and String operations

•Concatenation = +
•Name =“hema”
•Age = 27
•Eg: Name +” “ + age
•Result: Hema 27
•Subsets of strings can be taken using the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.
•str = 'Hello World!'
•print str # Prints complete string
•print str[0] # Prints first character of the string
•print str[2:5] # Prints characters starting from 3rd to 5th
•print str[2:] # Prints string starting from 3rd character
•print str * 2 # Prints string two times
•print str + "TEST" # Prints concatenated string

6. List handling

•list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
• tinylist = [123, 'john']
•print list # Prints complete list
•print list[0] # Prints first element of the list print list[1:3] # Prints elements starting from 2nd till 3rd
•print list[2:] # Prints elements starting from 3rd element
•print tinylist * 2 # Prints list two times
•print list + tinylist # Prints concatenated lists
•List accessing
•List[startposition:stopspoition:steps]

7.  List operations
•Append -> adds one element
•Extend -> adds a list of element
•Insert -> adds a item at a specific index position
•Index(element) -> the position of the element
•Remove -> specific element from the list
•Pop -> removes last element from the list
•>>> li = ['a', 'b', 'mpilgrim']
•>>> li = li + ['example', 'new']
•>>> li
•['a', 'b', 'mpilgrim', 'example', 'new']
•>>> li += ['two']
•>>> li
•['a', 'b', 'mpilgrim', 'example', 'new', 'two']
•>>> li = [1, 2] * 3
•>>> li
•[1, 2, 1, 2, 1, 2]
•Assign multiple values at a time
•v = ('a', 'b', 'e')
•>>> (x, y, z) = v
•>>> x
•'a'
•>>> y
•'b' >>> z

8. Tuple usage

•#!/usr/bin/python
•tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
•tinytuple = (123, 'john')
•print tuple # Prints complete list
•print tuple[0] # Prints first element of the list print tuple[1:3] # Prints elements starting from 2nd till 3rd
•print tuple[2:] # Prints elements starting from 3rd element
•print tinytuple * 2 # Prints list two times
•print tuple + tinytuple # Prints concatenated lists

9. Dictionary usage  •dict = {}
•dict['one'] = "This is one"
•dict[2] = "This is two“
• tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
•print dict['one'] # Prints value for 'one' key
•print dict[2] # Prints value for 2 key
•print tinydict # Prints complete dictionary print
•tinydict.keys() # Prints all the keys print
•tinydict.values() # Prints all the values
•Tinydict.items() # print key and value pair

10. How to reverse a string?

•Str1 = str1[::-1]
•List1.reverse()

11. Type Conversion

•List(s) # converts s to list
•Tuple(s) #converts s to tuple
•Dict(s) #converts s to dictionary s must to be a key, value pair
•EVAL(x) # evaluate a string returns a obj
•Chr(x) #integer to a obj  

12. Frequently used python modules

•Sys
•os
•Re
•Datetime
•Time 
13. Python regular expression

•re.Match – checks at the begining
•re.match(pattern, string, flags=0)
•Re.search method –checks the whole string
•re.search(pattern, string, flags=0)
•Returns a object
•We can access the matched element by group method
•Re.sub -re.sub(pattern, repl, string, max=0)
•Re.findall
•Re.search ->search whole text
•Re.match ->anchored at the begining
•Re.findall -> found all occurence
•re.sub() is re.sub(pattern,repl,string)
•Re.subn() returns tuple and number of replacement
•Re.finditer()
•+ 1 or more
•? 0 or 1
•* 0 or more
•search ⇒ find something anywhere in the string and return a match object.
•match ⇒ find something at the beginning of the string and return a match object.

 14. Python iterators

•Immutable
•Tuple, string, file, numbers
•Mutable
•List
•Iterator
•List, generator


useful tutorials for python  candidates to refer during interviews

No comments:

Post a Comment