0% found this document useful (0 votes)
4 views

Python

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

Python

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

https://www.youtube.com/playlist?

list=PLhQjrBD2T3817j24-GogXmWqO5Q5vYy0V

Frequently asked 8 Python practice questions and answers in Data Analyst


Interview:
1. Check for Prime Number:
Write a program that takes an integer as input and checks if it is a prime number.
num = int(input('Enter a number: '))
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print(f'{num} is not a prime number')
break
else:
print(f'{num} is a prime number')
else:
print(f'{num} is not a prime number')

2. Odd or Even:
Write a program that takes an integer and checks if it is odd or even.
num = int(input('Enter a number: '))
if num % 2 == 0:
print('The number is even')
else:
print('The number is odd')

3. Positive, Negative, or Zero:


Write a program that takes an integer and checks if it is positive, negative, or zero.
num = int(input('Enter a number: '))
if num > 0:
print('The number is positive')
elif num < 0:
print('The number is negative')
else:
print('The number is zero')

4. Check Divisibility:
Write a program that takes an integer and checks if it is divisible by 5 and 11.
num = int(input('Enter a number: '))
if num % 5 == 0 and num % 11 == 0:
print('The number is divisible by both 5 and 11')
elif num % 5 == 0 or num % 11 == 0:
print('The number is divisible by 5 or 11')
else:
print('The number is not divisible by 5 or 11')
5. Check for Leap Year:
Write a program that takes a year as input and checks if it is a leap year.
year = int(input('Enter a year: '))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f'{year} is a leap year')
else:
print(f'{year} is not a leap year')

6. Check Character Type:


Write a program that takes a character and checks if it is a vowel, consonant, digit,
or special character.
char = input('Enter a character: ')
if char.isdigit():
print('It is a digit')
elif char.isalpha():
if char.lower() in 'aeiou':
print('It is a vowel')
else:
print('It is a consonant')
else:
print('It is a special character')

7. Grade Calculator:
Write a program that takes a score as input and prints the corresponding grade
based on the following ranges:
- 90-100: A
- 80-89: B
- 70-79: C
- 60-69: D
- Below 60: F
score = int(input('Enter your score: '))
if score >= 90:
print('Grade: A')
elif score >= 80:
print('Grade: B')
elif score >= 70:
print('Grade: C')
elif score >= 60:
print('Grade: D')
else:
print('Grade: F')

8. Check Palindrome:
Write a program that takes a string and checks if it is a palindrome.
word = input('Enter a word: ')
if word == word[::-1]:
print('The word is a palindrome')
else:
print('The word is not a palindrome')

Use Python to turn messy data into valuable insights!

Here are the main functions you need to know:

1. 𝗱𝗿𝗼𝗽𝗻𝗮(): Clean up your dataset by removing missing values. Use df.dropna() to


eliminate rows or columns with NaNs and keep your data clean.

2. 𝗳𝗶𝗹𝗹𝗻𝗮(): Replace missing values with a specified value or method. With the help
of df.fillna(value) you maintain data integrity without losing valuable information.

3. 𝗱𝗿𝗼𝗽_𝗱𝘂𝗽𝗹𝗶𝗰𝗮𝘁𝗲𝘀(): Ensure your data is unique and accurate. Use


df.drop_duplicates() to remove duplicate rows and avoid skewing your analysis by
aggregating redundant data.

4. 𝗿𝗲𝗽𝗹𝗮𝗰𝗲(): Substitute specific values throughout your dataset. The function


df.replace(to_replace, value) allows for efficient correction of errors and
standardization of data.

5. 𝗮𝘀𝘁𝘆𝗽𝗲(): Convert data types for consistency and accuracy. Use the cast
function df['column'].astype(dtype) to ensure your data columns are in the correct
format you need for your analysis.

6. 𝗮𝗽𝗽𝗹𝘆(): Apply custom functions to your data. df['column'].apply(func) lets you


perform complex transformations and calculations. It works with both standard and
lambda functions.

7. 𝘀𝘁𝗿.𝘀𝘁𝗿𝗶𝗽(): Clean up text data by removing leading and trailing whitespace.


Using df['column'].str.strip() helps you to avoid hard-to-spot errors in string
comparisons.

8. 𝘃𝗮𝗹𝘂𝗲_𝗰𝗼𝘂𝗻𝘁𝘀(): Get a quick summary of the frequency of values in a column.


df['column'].value_counts() helps you understand the distribution of your data.

9. 𝗽𝗱.𝘁𝗼_𝗱𝗮𝘁𝗲𝘁𝗶𝗺𝗲(): Convert strings to datetime objects for accurate date and


time manipulation. For time series analysis the use of pd.to_datetime(df['column'])
will often be one of your first steps in data preparation.

10. 𝗴𝗿𝗼𝘂𝗽𝗯𝘆(): Aggregates data based on specific columns. Use


df.groupby('column') to perform operations like sum, mean, or count on grouped
data.
 By using @property, you can "reuse" the name of a property to avoid creating
new names for the getters, setters, and deleters.
Encapsulation Encapsulation isn't just about grouping variables and methods
together in a class; it's about safeguarding the inner workings of that class, creating
a clear and safe interface for the rest of your program to interact with.
Data Hiding
Simplifying Complexity
Reusability and Modularity

Overriding Methods: A subclass can provide its specific implementation of a


method from the parent class. In our example, Car overrides the start method of
Vehicle .

Types of Polymorphism
Compile-Time Polymorphism (Method Overloading) This type of polymorphism
occurs when multiple methods within a class have the same name but different
parameters. For example, based on the arguments provided, a drawing method
could be overloaded to draw various shapes, such as circles, rectangles, or
triangles.
Run-Time Polymorphism (Method Overriding): This occurs when a subclass
provides a specific implementation for a method that is already provided by its
parent class. This way, the method behaves differently depending on the object that
invokes it. For instance, a generic Vehicle class method start() could be overridden
by Car and Bike subclasses to start a car or bike specifically.

You might also like