Python for data science
Test your Python skills with these 20 practice questions and solutions from Day 2 of Learning Python for Data Science. Covers string operations, data types, and more!
x = "123"
y = int(x)
print(type(y))
Solution:
# Output: <class 'int'>
sentence = "Let's learn Python!"
Solution:
substring = sentence[6:11]
print(substring) # Output: 'learn'
7.5
into an integer and print the result.Solution:
num = 7.5
int_num = int(num)
print(int_num) # Output: 7
Solution:
text = "Hello World"
print(text.upper()) # Output: 'HELLO WORLD'
x = "Data"
y = "Science"
print(x + y)
Solution:
# Output: 'DataScience'
Solution:
text = "Python Programming"
print(len(text)) # Output: 18
Solution:
text = "Data Science"
print(text[5:]) # Output: 'Science'
"Python"[::-1]
?Solution:
print("Python"[::-1]) # Output: 'nohtyP'
Solution:
text = "Python Programming"
print(text.startswith("P")) # Output: True
Solution:
text = "Data Analysis"
print(text.replace("a", "o")) # Output: 'Doto Anolysis'
Solution:
text = "PYTHON"
print(text.lower()) # Output: 'python'
"Hello" * 3
?Solution:
print("Hello" * 3) # Output: 'HelloHelloHello'
Solution:
text = "Data Science"
print(text[-3:]) # Output: 'nce'
text = " Machine Learning "
, write a Python command to remove leading and trailing spaces.Solution:
text = " Machine Learning "
print(text.strip()) # Output: 'Machine Learning'
2024
into a string.Solution:
num = 2024
print(str(num)) # Output: '2024'
Solution:
num = 10
print(type(num)) # Output: <class 'int'>
Solution:
text = ""
print(len(text) == 0) # Output: True
print("5" + "5")
?Solution:
# Output: '55' (String concatenation, not addition)
a = "Python"
, write a command to extract the first three characters.Solution:
a = "Python"
print(a[:3]) # Output: 'Pyt'
.lower()
and .casefold()
in Python strings?Solution: .lower()
converts a string to lowercase but .casefold()
is more aggressive as it is designed for case-insensitive string matching, handling special cases for certain languages.
Example:
text = "ß"
print(text.lower()) # Output: 'ß'
print(text.casefold()) # Output: 'ss'
These practice questions cover Python string manipulation, type conversions, and essential built-in functions. Practicing them will help solidify your understanding of these fundamental concepts.
We hope this article was helpful for you and you learned a lot about data analyst interview from it. If you have friends or family members who would find it helpful, please share it to them or on social media.
Join our social media for more.
Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Welcome to Day 13 of Learning Python for Data Science! Today, we’re focusing on three…
Test your understanding of Python Data Structure, which we learned in our previous lesson of…
Welcome to Day 12 of Learning Python for Data Science. Today, we’ll dive into Pandas,…
NumPy Array in Python is a powerful library for numerical computing in Python. It provides…
Welcome to Day 9 of Learning Python for Data Science. Today we will explore comprehensions,…
Test your understanding of Python Data Structure, which we learned in our previous lesson of…