**Problems Related to Lists in Python ** .
- Designed by Gaurav Chauhan
Dear Students, these Python Programming Language Interview Questions
have been designed specially for u to get you acquainted with the nature of questions you may encounter during your interview for the subject of Python Programming Language.
Note:-
The Following Questions Covers the concepts which are discussed in class. You all students have to submit it as a assignment for ur practice.
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.
Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program doesn't catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python's introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.
*A variety of basic data types are available: numbers (floating point, complex, and unlimited-length long integers), strings (both ASCII and Unicode), lists, and dictionaries.
*Python supports object-oriented programming with classes and multiple inheritance. Code can be grouped into modules and packages.
*The language supports raising and catching exceptions, resulting in cleaner error handling. Data types are strongly and dynamically typed. Mixing incompatible types (e.g. attempting to add a string and a number) causes an exception to be raised, so errors are caught sooner.
*Python contains advanced programming features such as generators and list comprehensions.
*Python's automatic memory management frees you from having to manually allocate and free memory in your code.
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.
Yes, Like most of the widely used programming languages like Java, C, C++, etc, Python is also a case sensitive language.
Languages like Pascal, Basic, Fortran, SQL, Lisp, etc are case in-sensitive.
Python has five standard data types −
Numbers String List Tuple Dictionary
Hello World!
str ='Hello world!' print(str[0]) output :- H
str ='Hello world!' print(str[2:5]) output :- llo
str ='Hello world!' print(str[2:]) output :- llo world!
str ='Hello world!' print(str*2) output :- Hello world!Hello world!
str ='Hello world!' print(str+"Test") output :- Hello world!Test
It will print concatenated lists. Output would be [ 'abcd', 786 , 2.23, 'john', 70.2 ].
It will print first element of the list. Output would be abcd.
It will print elements starting from 2nd till 3rd. Output would be [786, 2.23].
It will print elements starting from 3rd element. Output would be [2.23, 'john', 70.200000000000003].
It will print list two times. Output would be [123, 'john', 123, 'john'].
What is the output of print list + tinylist * 2 if list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] and tinylist = [123, 'john']?
It will print concatenated lists. Output would be ['abcd', 786, 2.23, 'john', 70.2, 123, 'john', 123, 'john'].
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.
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.
It will print complete tuple. Output would be ('abcd', 786, 2.23, 'john', 70.200000000000003).
It will print first element of the tuple. Output would be abcd.
It will print elements starting from 2nd till 3rd. Output would be (786, 2.23).
It will print elements starting from 3rd element. Output would be (2.23, 'john', 70.200000000000003).
It will print tuple two times. Output would be (123, 'john', 123, 'john').
What is the output of print tuple + tinytuple if tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) and tinytuple = (123, 'john')?
It will print concatenated tuples. Output would be ('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john').
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.
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'}
Using dictionary.keys() function, we can get all the keys from the dictionary object.
print dict.keys() # Prints all the keys
Using dictionary.values() function, we can get all the values from the dictionary object.
print dict.values() # Prints all the values
int(x [,base]) − Converts x to an integer. base specifies the base if x is a string.
long(x [,base] ) − Converts x to a long integer. base specifies the base if x is a string.
float(x) − Converts x to a floating-point number.
str(x) − Converts object x to a string representation.
eval(str) − Evaluates a string and returns an object.
list(s) − Converts s to a list.
set(s) − Converts s to a set.
dict(d) − Creates a dictionary. d must be a sequence of (key,value) tuples.
chr(x) − Converts an integer to a character.
** Exponent − Performs exponential (power) calculation on operators. a**b = 10 to the power 20 if a = 10 and b = 20.
// Floor Division` − The division of operands where the result is the quotient in which the digits after the decimal point are removed.
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).
not in − Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. x not in y, here not in results in a 1 if x is not a member of sequence y.
break statement − Terminates the loop statement and transfers execution to the statement immediately following the loop.
continue statement − Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
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.
capitalize() − Capitalizes first letter of string.
isalnum() − Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise.
isdigit() − Returns true if string contains only digits and false otherwise.
islower() − Returns true if string has at least 1 cased character and all cased characters are in lowercase and false otherwise.
isnumeric() − Returns true if a unicode string contains only numeric characters and false otherwise.
isspace() − Returns true if string contains only whitespace characters and false otherwise.
istitle() − Returns true if string is properly "titlecased" and false otherwise
isupper() − Returns true if string has at least one cased character and all cased characters are in uppercase and false otherwise.
len(string) − Returns the length of the string.
lower() − Converts all uppercase letters in string to lowercase.
lstrip() − Removes all leading whitespace in string.
max(str) − Returns the max alphabetical character from the string str.
min(str) − Returns the min alphabetical character from the string str.
swapcase() − Inverts case for all letters in string.
upper() − Converts all lowercase letters in string to uppercase.
To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know.
3
[1, 2, 3, 4, 5, 6]
['Hi!', 'Hi!', 'Hi!', 'Hi!']
True
1 2 3
3, Offsets start at zero.
L[-1] = 3, L[-2]=2, L[-3]=1
2, 3, Slicing fetches sections.
cmp(list1, list2) − Compares elements of both lists.
len(list) − Gives the total length of the list.
max(list) − Returns item from the list with max value.
min(list) − Returns item from the list with min value.
list.index(obj) − Returns the lowest index in list that obj appears.
list.insert(index, obj) − Inserts object obj into list at offset index.
list.pop(obj=list[-1]) − Removes and returns last object or obj from list.
ist.remove(obj) − Removes object obj from list.
list.reverse() − Reverses objects of list in place.
list.sort([func]) − Sorts objects of list, use compare func if given.