Python is one of the most popular programming languages used by data scientists and AIML professionals. This popularity is due to the following key features of Python:
Python is easy to learn due to its clear syntax and readability Python is easy to interpret, making debugging easy Python is free and Open-source It can be used across different languages It is an object-oriented language which supports concepts of classes It can be easily integrated with other languages like C++, Java and more
Keywords in Python are reserved words which are used as identifiers, function name or variable name. They help define the structure and syntax of the language.
There are a total of 33 keywords in Python 3.7 which can change in the next version, i.e., Python 3.8. A list of all the keywords is provided below:
Keywords in Python
False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except
Literals in Python refer to the data that is given in a variable or constant. Python has various kinds of literals including:
String Literals: It is a sequence of characters enclosed in codes. There can be single, double and triple strings based on the number of quotes used. Character literals are single characters surrounded by single or double-quotes. Numeric Literals: These are unchangeable kind and belong to three different types – integer, float and complex. Boolean Literals: They can have either of the two values- True or False which represents ‘1’ and ‘0’ respectively. Special Literals: Special literals are sued to classify fields that are not created. It is represented by the value ‘none’. Python Interview Questions PDF
Solution ->
Let’s say we have two tuples like this ->
tup1 = (1,”a”,True)
tup2 = (4,5,6)
Concatenation of tuples means that we are adding the elements of one tuple at the end of another tuple.
Now, let’s go ahead and concatenate tuple2 with tuple1:
Code
tup1=(1,"a",True)
tup2=(4,5,6)
tup1+tup2
Output
All you have to do is, use the ‘+’ operator between the two tuples and you’ll get the concatenated result.
Similarly, let’s concatenate tuple1 with tuple2:
Code
tup1=(1,"a",True)
tup2=(4,5,6)
tup2+tup1
Output
Ans: Functions in Python refer to blocks that have organised, and reusable codes to perform single, and related events. Functions are important to create better modularity for applications which reuse high degree of coding. Python has a number of built-in functions like print(). However, it also allows you to create user-defined functions.
Pandas is an open source python library which has a very rich set of data structures for data based operations. Pandas with it’s cool features fits in every role of data operation, whether it be academics or solving complex business problems. Pandas can deal with a large variety of files and is one of the most important tools to have a grip on.
Series is a one dimensional pandas data structure which can data of almost any type. It resembles an excel column. It supports multiple operations and is used for single dimensional data operations.
Creating a series from data:
Code
import pandas as pd
data=["1",2,"three",4.0]
series=pd.Series(data)
print(series)
print(type(series))
A pandas groupby is a feature supported by pandas which is used to split and group an object. Like the sql/mysql/oracle groupby it used to group data by classes, entities which can be further used for aggregation. A dataframe can be grouped by one or more columns.
Code
df = pd.DataFrame({'Vehicle':['Etios','Lamborghini','Apache200','Pulsar200'], 'Type':["car","car","motorcycle","motorcycle"]})
df
There are various methods to remove duplicate elements from a list. But, the most common one is, converting the list into a set by using the set() function and using the list() function to convert it back to a list, if required. ex: list0 = [2, 6, 4, 7, 4, 6, 7, 2] list1 = list(set(list0)) print (“The list without duplicates : ” + str(list1)) o/p: The list without duplicates : [2, 4, 6, 7]
Recursion is a function calling itself one or more times in it body. One very important condition a recursive function should have to be used in a program is, it should terminate, else there would be a problem of an infinite loop.
List comprehensions are used for transforming one list into another list. Elements can be conditionally included in the new list and each element can be transformed as needed. It consists of an expression leading a for clause, enclosed in brackets. for ex: list = [i for i in range(1000)] print list
The bytes() function returns a bytes object. It is used to convert objects into bytes objects, or create empty bytes object of the specified size.
Python has the following basic operators: Arithmetic( Addition(+), Substraction(-), Multiplication(*), Division(/), Modulus(%) ), Relational ( <, >, <=, >=, ==, !=, ), Assignment ( =. +=, -=, /=, *=, %= ), Logical ( and, or not ), Membership, Identity, and Bitwise Operators
“with” statement in python is used in exception handling. A file can be opened and closed while executing a block of code, containing the “with” statement., without using the close() function. It essentially makes the code much more easy to read.
The map() function in Python is used for applying a function on all elements of a specified iterable. It consists of two parameters, function and iterable. The function is taken as an argument and then applied to all the elements of an iterable(passed as the second argument). An object list is returned as a result.
def add(n):
return n + n number= (15, 25, 35, 45)
res= map(add, num)
print(list(res))
o/p: 30,50,70,90
init methodology is a reserved method in Python aka constructor in OOP. When an object is created from a class and init methodolgy is called to access the class attributes.
The two static analysis tool used to find bugs in Python are: Pychecker and Pylint. Pychecker detects bugs from the source code and warns about its style and complexity.While, Pylint checks whether the module matches upto a coding standard.
One major difference between a tuple and a dictionary is that dictionary is mutable while a tuple is not. Meaning the content of a dictionary can be changed without changing it’s identity, but in tuple that’s not possible.
Pass is a statentemen which does nothing when executed. In other words it is a Null statement. This statement is not ignored by the interpreter, but the statement results in no operation. It is used when you do not want any command to execute but a statement is required.
Not all objects can be copied in Python, but most can. We ca use the “=” operator to copy an obect to a variable.
ex: var=copy.copy(obj)
The inbuilt function str() can be used to convert a number to a string.
Modules are the way to structure a program. Each Python program file is a module, importing other attributes and objects. The folder of a program is a package of modules. A package can have modules or subfolders.
In Python the object() function returns an empty object. New properties or methods cannot be added to this object.
NumPy stands for Numerical Python while SciPy stands for Scientific Python. NumPy is the basic library for defining arrays and simple mathematica problems, while SciPy is used for more complex problems like numerical integration and optimization and machine learning and so on.
len() is used to determine the length of a string, a list, an array, and so on. ex: str = “greatlearning” print(len(str)) o/p: 13
Encapsulation means binding the code and the data together. A Python class for example.
type() is a built-in method which either returns the type of the object or returns a new type object based on the arguments passed.
ex: a = 100
type(a)
o/p: int
Split function is used to split a string into shorter string using defined seperatos. letters = (” A, B, C”) n = text.split(“,”) print(n)
o/p: [‘A’, ‘B’, ‘C’ ]
Ans. Python has following built-in data types:
Numbers: Python identifies three types of numbers:
Integer: All positive and negative numbers without a fractional part Float: Any real number with floating-point representation Complex numbers: A number with a real and imaginary component represented as x+yj. x and y are floats and j is -1(square root of -1 called an imaginary number) Boolean: The Boolean data type is a data type that has one of two possible values i.e. True or False. Note that ‘T’ and ‘F’ are capital letters.
String: A string value is a collection of one or more characters put in single, double or triple quotes.
List: A list object is an ordered collection of one or more data items which can be of different types, put in square brackets. A list is mutable and thus can be modified, we can add, edit or delete individual elements in a list.
Set: An unordered collection of unique objects enclosed in curly brackets
Frozen set: They are like a set but immutable, which means we cannot modify their values once they are created.
Dictionary: A dictionary object is unordered in which there is a key associated with each value and we can access each value through its key. A collection of such pairs is enclosed in curly brackets. For example {‘First Name’ : ’Tom’ , ’last name’ : ’Hardy’} Note that Number values, strings, and tuple are immutable while as List or Dictionary object are mutable.
Ans. Python docstrings are the string literals enclosed in triple quotes that appear right after the definition of a function, method, class, or module. These are generally used to describe the functionality of a particular function, method, class, or module. We can access these docstrings using the doc attribute. Here is an example:
def square(n):
'''Takes in a number n, returns the square of n'''
return n**2
print(square.__doc__)
Ouput: Takes in a number n, returns the square of n.
In Python, there are no in-built functions that help us reverse a string. We need to make use of an array slicing operation for the same.
1
str_reverse = string[::-1]
Learn more: How To Reverse a String In Python
Ans: Built-in types in Python are as follows –
Integers Floating-point Complex numbers Strings Boolean Built-in functions
Ans: Array elements can be removed using pop() or remove() method. The difference between these two functions is that the former returns the deleted value whereas the latter does not.
Ans: Python is an object-oriented programming language. This means that any program can be solved in python by creating an object model. However, Python can be treated as procedural as well as structural language.
Ans:
Python has a multi-threading package but if you want to multi-thread to speed your code up, then it’s usually not a good idea to use it. Python has a construct called the Global Interpreter Lock (GIL). The GIL makes sure that only one of your ‘threads’ can execute at any one time. A thread acquires the GIL, does a little work, then passes the GIL onto the next thread. This happens very quickly so to the human eye it may seem like your threads are executing in parallel, but they are really just taking turns using the same CPU core. All this GIL passing adds overhead to execution. This means that if you want to make your code run faster then using the threading package often isn’t a good idea.
This is simple. We call the function len() on the string we want to calculate the length of.
len('Ayushi Sharma')
The list comprehension in python is a way to declare a list in one line of code. Let’s take a look at one such example.
One of the less common functions with beginners, zip() returns an iterator of tuples.
list(zip(['a','b','c'],[1,2,3])) [(‘a’, 1), (‘b’, 2), (‘c’, 3)]
When a function makes a call to itself, it is termed recursion. But then, in order for it to avoid forming an infinite loop, we must have a base condition.
Let’s take an example.
def facto(n): if n==1: return 1 return n*facto(n-1) facto(4)
Ans: Functions in Python refer to blocks that have organised, and reusable codes to perform single, and related events. Functions are important to create better modularity for applications which reuse high degree of coding. Python has a number of built-in functions like print(). However, it also allows you to create user-defined functions.
Let’s take a list for this.
mylist=[0,1,2,3,4,5,6,7,8] A negative index, unlike a positive one, begins searching from the right.
mylist[-3]
We use the lower() method for this.
'AyuShi'.lower() ‘ayushi’
To convert it into uppercase, then, we use upper().
There may be times in our code when we haven’t decided what to do yet, but we must type something for it to be syntactically correct. In such a case, we use the pass statement.
def func(*args): pass
The results should be printed in a comma-separated sequence on a single line. Suppose the following input is supplied to the program: 8 Then, the output should be: 40320
Hints: In case of input data being supplied to the question, it should be assumed to be a console input.
Solution: def fact(x): if x == 0: return 1 return x * fact(x - 1)
x=int(raw_input()) print fact(x)
44. With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.
Suppose the following input is supplied to the program: 8 Then, the output should be: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Consider use dict()
Solution: n=int(raw_input()) d=dict() for i in range(1,n+1): d[i]=i*i
print d
45 Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.
Suppose the following input is supplied to the program: 34,67,55,33,12,98 Then, the output should be: ['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98')
Hints: In case of input data being supplied to the question, it should be assumed to be a console input. tuple() method can convert list to tuple
Solution: values=raw_input() l=values.split(",") t=tuple(l) print l print t