PySpark, BigData, SQL, Hive, AWS, Python, Unix/Linux, Shortcuts, Examples, Scripts, Perl
Feb 13, 2013
Python Interview Questions - Part 1
1) Get Unique Items in an array :
Set : Another data type in Python, where copies are not allowed.
List: Sequence of elements just like array
>>> a = [1, 2, 2, 3]
>>> set(a)
{1, 2, 3} #Flower brackets
>>> list(set(a))
[1, 2, 3]
>>> langs = ['Perl', 'PHP', 'Python', 'Java', 'C', 'Ruby', 'perl', 'Perl', 'PERL']
>>> for each_lang in sorted(set(langs)):
... print each_lang
O/P:
C
Java
PERL
PHP
Perl
Python
Ruby
perl
2) What is a docstring in Python?
Docstring is the documentation string for a function.
It prints the comments given inside the function
It can be accessed by <function_name>.__doc__
>>>def func_test():
""" Test Comments 1
Test Comments 2
Test Comments 3 """
>>> func_test.__doc__
3) How to loop/iterate through dict in Python
>>> Countries = {'India': 'New Delhi', 'USA': 'Washington', 'China' : 'Beijing', 'Japan' : 'Tokyo'}
>>> for country, capital in Countries.iteritems(): #Old Version
... print country, capital
...
>>> for country, capital in Countries.items(): #New Version
... print(country, capital)
...
India New Delhi
China Beijing
USA Washington
Japan Tokyo
4) How to Strip End of Line chars in Python (e.g., \n)
rstrip()
C:\Prabhath\Technical\Python\Python_Scripts\read.txt
Gandhi
Abraham Lincoln
Winston Churchill
>>> file = open("C:\\Prabhath\\Technical\\Python\\Python_Scripts\\read.txt", "r")
>>> text = file.readlines()
>>> file.close()
>>> for line in text:
>>> print (len(line.rstrip()))
rstrip() is an inbuilt function which strips the string from the right end of spaces or tabs (special chars like \n etc.,)
Output:
6
15
17
5) Sorting an Array in Python
>>>arr_sort = [10, 50, 90, 80, 35, 56, 45, 98]
>>>arr_sort.sort() #It will sort and keep the result in arr_sort
>>>arr_sort #When U print this, it shows the result [10, 35, 45, 50, 56, 80, 90, 98]
sorted(x) #prints the result [10, 35, 45, 50, 56, 80, 90, 98]
list.sort vs sorted()
list.sort() #sortls only lists
sorted() function accepts any iterable
e.g.,
>>> sorted("Python in demand NOW".split(), key=str.lower)
['demand', 'in', 'NOW', 'Python']
6) Convert Strings to Integers
>>>num_strs = ['11','121','153','184','150','166','17','138','19']
>>>num_int = [int(i) for in in num_strs] #[11, 121, 153, 184, 150, 166, 17, 138, 19]
(or)
>>>num_strs = ['11','121','153','184','150','166','17','138','19']
>>>list(map(int, num_strs)) #[11, 121, 153, 184, 150, 166, 17, 138, 19]
>>>map(int, num_strs) #<map object at 0x0000000002A7AF28>
7) Program to swap two numbers (Python)
a = 500
b = 900
>>>a,b = b,a
8) How to use join in Python
>>> ss = 'abc def ghi'
>>> ss.split() #splits based on space, equivalent to ss.split("\s")
['abc', 'def', 'ghi']
>>> ''.join(ss.split())
'abcdefghi'
Labels:
python,
python_basics,
python_interview_questions
Subscribe to:
Post Comments (Atom)
ReplyDeleteGreat post!
Python Interview Questions - http://www.hub4tech.com/interview/python
Python Online Quiz- http://www.hub4tech.com/Python-online-quiz-tes
Online Training Python- http://www.hub4tech.com/training/python-online-training.php?id=71
Machine Learning Tutorial-http://www.hub4tech.com/machine-learning-tutorial
Python Interview Questions and Answers
ReplyDeletehttp://allinterviewquestionsandanswerspdf.blogspot.in/2016/06/top-25-python-interview-questions-and.html
Really very useful Thank you
ReplyDeleteit's very helpful to me
ReplyDeleteintresting blog
ReplyDeletehelpful to everyone
ReplyDeletehelpful blog
ReplyDeleteintresting blog and very useful
ReplyDeleteThanks for sharing. You can find seo interview questions for fresher which will assist you to have better preparation.
ReplyDeleteThank you for sharing an excellent information python online training
ReplyDeleteVery nice and wonderful post It is very useful for freshers to learn python. Thanks for sharing .Any one want to learn Python Online Training
ReplyDeletePython Online Training
I have read your blog and I got very useful and knowledgeable information from your blog. If anyone wants Python Training in Delhi, Please visit our page Python certification in Delhi
ReplyDeleteThis blog will help the aspirants a lot. I got so many basic idea.keep sharing
ReplyDeletePython Training and placement
Nice information. keep sharing
ReplyDeleteMore info. on best Python Institute in Delhi
Hi,
ReplyDeleteThis post is really nice and informative. The explanation given is really comprehensive and informative... kepp updating us with latest python interview questions
Thank You
Hariprasad
Thanks a lot for sharing marvellous information on sap course. Thanks for sharing this valuable post with us.
ReplyDeletePython Training in Gurgaon
Its a wonderful post and very helpful, thanks for all this information. You are including better information.
ReplyDeletePython Training in Noida
Hi, thanks for your blog, if you want to learn about programming languages like java, php, android app, embedded system etc. I think this training institute is the best one.
ReplyDeletebest python training in coimbatore
Android training in coimbatore
Networking training in coimbatore
Nice post..Thanks for sharing useful information..
ReplyDeletePython training in Chennai/
Python training in OMR/
Python training in Velachery/
Python certification training in Chennai/
Python training fees in Chennai/
Python training with placement in Chennai/
Python training in Chennai with Placement/
Python course in Chennai/
Python Certification course in Chennai/
Python online training in Chennai/
Python training in Chennai Quora/
Best Python Training in Chennai/
Best Python training in OMR/
Best Python training in Velachery/
Best Python course in Chennai/
Python is a user-friendly programming language that helps you get surefire success with the programs you create. What's more, that is most likely the reason why it is considered one of the best devices for prototype development and other assignments linked to impromptu programming. This language is object-oriented and is extremely powerful. Besides, python brags of a huge standard library.
ReplyDeleteFor More Info: Python Course in Delhi
Thank you for sharing this post such a good.
ReplyDeleteVisit https://www.pythontraining.net/