Python offers several built - in data structures such as lists, tuples, sets, and dictionaries.
Lists:
# Creating a list
my_list = [1, 2, 3, 4, 5]
# Accessing elements
print(my_list[0])
# Modifying elements
my_list[1] = 10
print(my_list)
Dictionaries:
# Creating a dictionary
my_dict = {'name': 'John', 'age': 25}
# Accessing values
print(my_dict['name'])
# Adding a new key - value pair
my_dict['city'] = 'New York'
print(my_dict)
Control flow statements like if - else
, for
, and while
are used to control the execution of a program.
# if - else statement
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
# for loop
for i in range(5):
print(i)
# while loop
count = 0
while count < 5:
print(count)
count += 1
Functions are used to group a set of statements that perform a specific task. Lambda expressions are anonymous functions.
# Function definition
def add_numbers(a, b):
return a + b
result = add_numbers(3, 5)
print(result)
# Lambda expression
multiply = lambda x, y: x * y
print(multiply(2, 4))
Python supports object - oriented programming concepts such as classes and objects.
# Class definition
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, my name is {self.name} and I am {self.age} years old.")
# Creating an object
p = Person("Alice", 30)
p.introduce()
Pandas is a powerful library for data manipulation and analysis.
import pandas as pd
# Creating a DataFrame
data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
# Accessing columns
print(df['Name'])
# Filtering data
filtered_df = df[df['Age'] > 30]
print(filtered_df)
NumPy is used for numerical computations in Python.
import numpy as np
# Creating a NumPy array
arr = np.array([1, 2, 3, 4, 5])
# Performing operations on the array
print(arr * 2)
# Multi - dimensional array
matrix = np.array([[1, 2], [3, 4]])
print(matrix)
Matplotlib and Seaborn are popular libraries for data visualization.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# Creating a sample DataFrame
data = {'x': [1, 2, 3, 4, 5], 'y': [2, 4, 6, 8, 10]}
df = pd.DataFrame(data)
# Scatter plot using Matplotlib
plt.scatter(df['x'], df['y'])
plt.xlabel('x')
plt.ylabel('y')
plt.title('Scatter Plot')
plt.show()
# Box plot using Seaborn
sns.boxplot(x = df['y'])
plt.show()
try - except
blocks are used for error handling in Python.
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
Python provides functions to read and write files.
# Writing to a file
with open('test.txt', 'w') as f:
f.write('Hello, World!')
# Reading from a file
with open('test.txt', 'r') as f:
content = f.read()
print(content)
The pdb
module in Python can be used for debugging.
import pdb
def add_numbers(a, b):
pdb.set_trace()
return a + b
result = add_numbers(3, 5)
print(result)
Use meaningful variable names and add comments to your code.
# Function to calculate the area of a circle
import math
def calculate_circle_area(radius):
"""
This function calculates the area of a circle given its radius.
:param radius: The radius of the circle.
:return: The area of the circle.
"""
return math.pi * radius ** 2
Use generators instead of lists when dealing with large datasets to save memory.
# Generator example
def square_numbers(n):
for i in range(n):
yield i ** 2
gen = square_numbers(5)
for num in gen:
print(num)
Use the unittest
module to write unit tests for your functions.
import unittest
def add_numbers(a, b):
return a + b
class TestAddNumbers(unittest.TestCase):
def test_add_numbers(self):
result = add_numbers(3, 5)
self.assertEqual(result, 8)
if __name__ == '__main__':
unittest.main()
In conclusion, a solid understanding of Python is crucial for acing data science interviews. By mastering the fundamental concepts, usage methods, common practices, and best practices covered in this blog, you will be well - prepared to handle Python - related questions in data science interviews. Remember to practice coding regularly and apply these concepts in real - world scenarios to enhance your skills.