πŸ’₯ Crack the Python Interview


Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python.

​

Python interviews rarely ask for theory. So today we go through most common, application based questions asked in interviews with simple snippets. Most concepts are covered in our past issues and you can do a quick refresher here. If you like this format time to time, do give your feedback in the poll at the end.

​

β†ͺ️ Reverse a string

In: text = "samplestring"

# [start : stop : step]
# leaving start/stop empty means use whole string
# step = -1 means go backwards, one character at a time
reversed_text = text[::-1]
print(reversed_text)
Out: gnirtselpmas

​

πŸ“‹ Check for Palindrome (words reading same forward and backward)

In:
word = "level"
is_palindrome = word == word[::-1] # logic used in above snippet
print(is_palindrome)
Out: True

​

πŸ”  Count number of vowels in a sentence

In:
sentence = "Pandas Daily is superb" # define a string

# loop over each character, make it lowercase, check if it is a vowel
count = sum(1 for char in sentence if char.lower() in "aeiou")
print(count)
Out: 7

​

✨ Print even numbers in a given range

In:
start, end = 10, 18

# for loop to go through numbers from start to end
# x % 2 gives remainder when divided by 2

evens = [x for x in range(start, end+1) if x % 2 == 0]
print(evens)
Out: [10, 12, 14, 16, 18]

​

πŸ’ͺ Max value in a list

In:
numbers = [3, 18, 7, 2]
max_value = max(numbers)
print(max_value)
Out: 18

​

πŸŒ€ Fibonacci Sequence [ πŸš€ Outsmart the interviewers]

In:
n = 5 # how many numbers in sequence
fib = [0, 1] # sequence starts with 0 and 1

# loop to get other 3 numbers
for _ in range(2, n):
  # each new number = sum of last two numbers in the list
  fib.append(fib[-1] + fib[-2])
print(fib[:n])
Out: [0, 1, 1, 2, 3]

​

πŸ‘₯ Remove duplicates from list

In:
values = [1, 3, 3, 5, 1]

# 'set' is a datatype with unique elements
# convert list to set then back to list

unique_values = list(set(values))
print(unique_values)
Out: [1, 3, 5]

​

🟨🟧 List Comprehension: Squares of Numbers

In:
nums = [2, 4, 6]
# for loop runs through each number and multiplies it by itself
squares = [x*x for x in nums]
print(squares)
Out: [4, 16, 36]

β­πŸ“£ That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all agree that dashboards, emails, logs should read like English. Meaning 1.2 million beats 1234567 and 3.1M is more readable than 3145728. Stakeholders scan - and they get pissed if they have to parse messy numbers 😭😭 So today, we explore a third-party humanize library. Format numbers, file sizes, and dates/times into human-readable text. Mini exercise at the end. πŸ₯‡ Ready, Get, Set, Go! Install and...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Amazon reviews, chats and social media text (posts, comments, stories etc) are full of emojis πŸ”₯πŸ”₯ Great for expression! But give a hard time for text interpretation (Amazon, Meta would be going crazy). That is where Python's demoji library comes in. From a given text you can: Extract emojis Remove them if needed Replace with textual meaning Understand sentiment and what not... Let's begin!! Install demoji...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all know Python’s basic container data types β€” lists, dictionaries, tuples, sets. "Container" is just a data type that holds multiple items instead of single values. However, many times basics are not enough. Like if you want count of different items in the list you will run a loop like below. In: fruits = ["apple", "banana", "apple", "cherry"] counts = {} for item in fruits: counts[item] =...