Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Python Questions: N 5 For I in Range (0, N) : For J in Range (0, N) : Print (" ",end " ") Print ("/T")

Here are the steps to find number of i's in a dataframe column and filter records having 'i' alphabet: 1. Find number of i's in a column: To find the number of occurrences of character 'i' in the 'empname' column, we can use str.findall() method along with sum(): ```python num_i = df['empname'].str.findall('i').sum() ``` 2. Filter records having 'i': To filter records where the 'empname' column contains character 'i', we can use str.contains() method: ```python df[df['empname'].str.contains('i')] ```

Uploaded by

gayatri2g
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Python Questions: N 5 For I in Range (0, N) : For J in Range (0, N) : Print (" ",end " ") Print ("/T")

Here are the steps to find number of i's in a dataframe column and filter records having 'i' alphabet: 1. Find number of i's in a column: To find the number of occurrences of character 'i' in the 'empname' column, we can use str.findall() method along with sum(): ```python num_i = df['empname'].str.findall('i').sum() ``` 2. Filter records having 'i': To filter records where the 'empname' column contains character 'i', we can use str.contains() method: ```python df[df['empname'].str.contains('i')] ```

Uploaded by

gayatri2g
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Questions

1)

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

n=5

for i in range(0,n):

for j in range(0,n):

print("*",end=" ")

print("\t")

2)

*****
* *
* *
* *
*****
n=5

for i in range(n):

for j in range(n):

if i==0 or i==(n-1) or j==0 or j==(n-1):

print("*",end="")

else:

print(" ",end="")

print("\t")
3)

*
**
***
****
*****
for i in range(0,n+1):

for j in range(0,i):

print('*',end=" ")

print("\t")

4)

*
**
***
****
*****
size=5

for i in range(size):

for j in range(1,size-i):

print(' ',end="")

for k in range(0,i+1):

print('*',end="")

print()
5)

**

**

* *

* *

******

size=5

for i in range(size):

for j in range(size):

if i==size-1 or j==0 or i==j:

print("*",end="")

else:

print(" ",end="")

print("\t")

6)

*****
****
***
**
*
for i in range(0,5):

for j in range(i,5):

print("*",end=' ')

print("\t")
7)

*
***
*****
*******
*********

SR.NO. LIST TUPLE

1 Lists are mutable Tuples are immutable

Implication of iterations is Time- The implication of iterations is


2 consuming comparatively Faster

The list is better for performing


operations, such as insertion and Tuple data type is appropriate
3 deletion. for accessing the elements

Tuple consume less memory


4 Lists consume more memory as compared to the list

Tuple does not have many


5 Lists have several built-in methods built-in methods.

The unexpected changes and In tuple, it is hard to take


6 errors are more likely to occur place.
1. How to make a List as Read Only List?
Convert it to tuple
2. Difference between iterator and list iterator?
The fundamental difference between a list and an iterator is that a list contains a number of
objects in a specific order - so you can, for instance, pull one of them out from somewhere
in the middle.

... whereas an iterator yields a number of objects in a specific order, often creating them on
the fly as requested:

my_iter = iter(range(1000000000000))
my_iter
next(my_iter)

3. What design patterns you know in Java?


Decorator
Decorator pattern allows a user to add new functionality to an existing object without
altering its structure. This type of design pattern comes under structural pattern as this
pattern acts as a wrapper to existing class.
This pattern creates a decorator class, which wraps the original class and provides
additional functionality keeping the class methods signature intact.
The motive of a decorator pattern is to attach additional responsibilities of an object
dynamically.

4. What is a Singleton class?

Singleton Pattern in Python – A


Complete Guide
Singleton Pattern in Python - A Complete Guide - GeeksforGeeks

A Singleton pattern in python is a design pattern that allows you to create just
one instance of a class, throughout the lifetime of a program. Using a singleton
pattern has many benefits. A few of them are:
 To limit concurrent access to a shared resource.
 To create a global point of access for a resource.
 To create just one instance of a class, throughout the lifetime of a
program.

6. How to remove duplicate elements from a list in python?


a=[1,2,3,4,2,3,4,3,2,1,4]
list(set(a))

10. How to open a database connection? How the application understands from
where the query is to be read?

Q. Common Elements

Given two 1-dimensional arrays containing strings of lowercase alphabets, print the elements
that are common in both the arrays i.e. the strings that are present in both the ar

Array_1 = ["ab", "dc", "ab", "ab"]

Array_2 = ["dc", "ab", "ab"]

com=[]

for i in range(0,len(Array_2)):

for j in range(0,len(Array_1)):

if Array_2[i]==Array_1[j]:

com.append(Array_2[i])

Array_1[j]=""

print(Array_1)

break

print(com)

print(Array_1)

print(Array_2)
Largest Sum Contiguous Subarray
 Difficulty Level : Medium
 Last Updated : 01 Dec, 2021
Write an efficient program to find the sum of contiguous subarray within a one-
dimensional array of numbers that has the largest sum. 

a = [-2, -3, 4, -1, -2, 1, 5, -3]

max=-99999

l=[]

for i in range(0,len(a)):

for j in range(0,len(a)):

sum_max=sum(a[i:(len(a)-j)])

if max<sum_max:

max=sum_max

print(max)
Q3. Write a program: single input as a string(lets say "aaabcccfffghh"), you have to return the
char and their occurrence as a string. In this case you have to return "a3b1c3f3g1h2"

a="aaabcccfffghh"

li=[]

for i in a:

if i+str(a.count(i)) not in li:

li.append(i+str(a.count(i)))

print(''.join(li))

15. What is Pandas in Python?


 Pandas is an open-source software library developed for Python and is useful in data
manipulation and analysis. It provides plenty of data structures and operations such as
modification of numerical tables and time series. It can deal with different types of files
and is considered to be one of the important tools to have a grip on.
 Some of the attributes or methods provided by Pandas are:
o axes: It returns a row axis label list.
o empty: It returns true if the series is empty otherwise it returns false.
o size: It returns the count of elements in the underlying data.
o values: It returns the series as ndarray.
o head(): It returns the n rows from the beginning of a data frame or series.
o tail(): Returns the n rows from the end of a data frame or series.
self
The word 'self' is used to represent the instance of a class. By using the "self" keyword
we access the attributes and methods of the class in python.

__init__ method
"__init__" is a reseved method in python classes. It is called as a constructor in object
oriented terminology. This method is called when an object is created from a class and
it allows the class to initialize the attributes of the class.

Question:
Find number of i’s in a dataframe column
df['empname'].str.findall('i').sum()

Filter records having ‘i’ alphabet


df[df['empname'].str.contains('i')==True]

You might also like