Top 20 reusable code snippet for a python beginner

Recently I jumped to the python. I was a java developer for more than 8 years. It was a recent incident that made me have python skill mandatory. You must be guessed? No, it's not about data science and machine learning. It's about…

solving coding questions in interviews.

You may be surprised that why I am saying so. Hmm, I was interviewed for Facebook which changed my mind and perception towards the technical interview. I am not going into details about that incident though. I have written another blog post for the same.

In short, for FAANG and other top based companies interview, you need two things:-

  1. Strong DS Algo
  2. Speed

yes, Speed matters a lot, and I could not find any other programming language where you can solve a complex tree or graph questions in few lines and within the given time span. You are hardly given 45 minutes with introduction and all, and this 45 minutes you have to show all the skill and speed which you have got. This is why I opted for python. We need SPEEEEDDDD…

So well in this post, I am going to discuss the top 20 common codes which you may encounter while starting coding in python.

1. You have to print multiple words in the same line:

list = ["Manish", "David", "Shamved", "Umesh"]
output :- "Manish" "David" "Shamved" "Umesh"
name_list = ["Manish", "David", "Shamved", "Umesh"]for name in name_list:
print(name, end=" ")

By default, python print creates a new line. Use end=” ” argument in the print function to print in the same line.

2. How to mutate a string in python:

Strings are immutable in python. To mutate the string there are different approaches. One is below:

1. Convert the string to list.
2. change the list
3. join them back to string
Example:name = "Manish"
name_list = list(name)
name_list[3] = "k"
name = ''.join(name_list)
print(name)
Output: Manksh

3. Convert an array of strings to integers:

list_string = ["1", "2", "3", "4"]list_integer = map(int, list_string)print (type(list(list_integer)[0]))o/p:-
<class 'int'>

4. Traverse a String:

str = "manish"for x in range(0, len(str)):print (str[x])

5. Count the number of substrings in the given string:

string = "Python is cool for programming interviews? yes it is!"substring = "is"count = string.count(substring)print(count)Output: 2