2/16/24, 1:13 PM BASIC PYTHON Practice__Amar Sharma - Jupyter Notebook
Created by Amar Sharma
Q1) Reverse String
Write a Python function called reverse_string that takes a string as input and returns the reverse of that
string.
In [1]: def reverse_string(x):
a1 = x[::-1]
return a1
In [2]: reverse_string("amar")
Out[2]: 'rama'
Q2) Palindrome
Write a Python function called is_palindrome that takes a string as input and returns True if the string is a
palindrome (reads the same forwards and backwards), and False otherwise.
In [3]: def is_palindrome(x):
a2 = x
b2 = x[::-1]
return a2 == b2
# == operator to compare a2 and b2.
In [4]: a2 = "amma"
In [5]: is_palindrome(a2)
Out[5]: True
In [6]: is_palindrome("amar")
Out[6]: False
Q3) Count Vowels
Write a Python function called count_vowels that takes a string as input and returns the count of vowels
(a, e, i, o, u, case-insensitive) in the string.
In [7]: def count_vowels(x):
a3 = ["a","e","i","o","u","A","E","I","O","U"]
b3 = 0
for i in x:
if i in a3:
b3 +=1
return b3
localhost:8888/notebooks/BASIC PYTHON Practice__Amar [Link] 1/8
2/16/24, 1:13 PM BASIC PYTHON Practice__Amar Sharma - Jupyter Notebook
In [8]: count_vowels("kviufuyfjvjIFUYFUFJHVJVJCYSAACCV")
Out[8]: 8
In [9]: def count_vowels3(x):
a3 = "aeiouAEIOU"
b3 = 0
for i in a3:
for j in x:
if i==j:
b3 +=1
return b3
In [10]: count_vowels3("kviufuyfjvjIFUYFUFJHVJVJCYSAACCV")
Out[10]: 8
Q4) Remove Duplicates
Write a Python function called remove_duplicates that takes a list as input and returns a new list with
duplicate elements removed, while preserving the original order of elements.
In [11]: def remove_duplicates(x):
a4 = []
for i in x:
if i not in a4:
[Link](i)
return sorted(a4)
In [12]: remove_duplicates([5,5,2,2,7,9,1,6,4])
Out[12]: [1, 2, 4, 5, 6, 7, 9]
In [13]: def remove_duplicates4(x):
a4 = set()
for i in x:
[Link](i)
return sorted(a4)
In [14]: remove_duplicates4([5,5,2,2,7,9,1,6,4])
Out[14]: [1, 2, 4, 5, 6, 7, 9]
Q5) Anagram
Write a Python function called is_anagram that takes two strings as input and returns True if the two
strings are anagrams of each other, and False otherwise. An anagram is a word or phrase formed by
rearranging the letters of another word or phrase, using all the original letters exactly once.
localhost:8888/notebooks/BASIC PYTHON Practice__Amar [Link] 2/8
2/16/24, 1:13 PM BASIC PYTHON Practice__Amar Sharma - Jupyter Notebook
In [15]: def is_anagram(x,y):
if len(x) != len(y):
return False
x_sorted = sorted(x)
y_sorted = sorted(y)
return x_sorted == y_sorted
In [16]: is_anagram("rmrraa","amarrr")
Out[16]: True
Q6) Check Prime number
Write a Python function called is_prime that takes an integer as input and returns True if the integer is a
prime number, and False otherwise. A prime number is a natural number greater than 1 that has no
positive divisors other than 1 and itself.
In [17]: def is_prime(x):
if x<2:
return "Not Prime"
if x>=2:
for i in range(2,x):
if x%i == 0:
return "Not Prime"
else:
return "Prime"
In [18]: is_prime(89)
Out[18]: 'Prime'
Q7) Reverse word, maintain the order
Write a Python function called reverse_words that takes a string as input and returns a new string where
each word in the input string is reversed, while maintaining the order of words.
In [19]: def reverse_word(x):
a7 = [Link]()
b7 = ""
for i in a7:
b7 += i[::-1] + " "
return [Link]()
In [20]: reverse_word("amar sharma")
Out[20]: 'rama amrahs'
localhost:8888/notebooks/BASIC PYTHON Practice__Amar [Link] 3/8
2/16/24, 1:13 PM BASIC PYTHON Practice__Amar Sharma - Jupyter Notebook
In [21]: b = "amar sharma"
c = [Link]()
for i in c:
print(i[::-1], end = " ")
rama amrahs
Q8) Pangram
Write a Python function called is_pangram that takes a string as input and returns True if the string is a
pangram (contains every letter of the alphabet at least once), and False otherwise. Ignore case
sensitivity.
In [22]: def is_pangram(x):
a8 = "abcdefghijklmnopqrstuvwxyz"
for i in a8:
if i not in [Link]():
return False
return True
In [23]: is_pangram("The quick brown fox jumps over the lazy dog")
Out[23]: True
In [24]: def is_pangram88(x):
a88 = [Link]()
b88 = set()
for i in a88:
if [Link]():
[Link](i)
return len(b88) == 26
In [25]: is_pangram88("The quick brown fox jumps over the lazy dog")
Out[25]: True
In [26]: # collected all alphabet in order, use of "".join(),sorted(x),[Link](),[Link]()
def alpha(x):
s = ""
for c in x:
if [Link]():
s += c
sorted_s = ''.join(sorted([Link]()))
return sorted_s
In [27]: x = "Hello, world! This is a test sentence."
alpha(x)
Out[27]: 'acdeeeeehhiilllnnoorssssttttw'
localhost:8888/notebooks/BASIC PYTHON Practice__Amar [Link] 4/8
2/16/24, 1:13 PM BASIC PYTHON Practice__Amar Sharma - Jupyter Notebook
Q9) Sum of numbers
Write a Python function called sum_of_numbers that takes an integer as input and returns the sum of its
digits.
In [28]: def sum_of_numbers(*x):
a = 0
for i in x:
a+=i
return a
In [29]: sum_of_numbers(8888,1,9898)
Out[29]: 18787
Q10) Sum of Digits
Write a Python function called sum_of_digits that takes an integer as input and returns the sum of its
digits.
In [30]: def sum_of_digits(x):
a = str(x)
c=0
for i in a:
b = int(i)
c+=b
return c
In [31]: sum_of_digits(123)
Out[31]: 6
Q11) Find Duplicates in list
Write a Python function called find_duplicates that takes a list as input and returns a list containing all the
elements that appear more than once in the input list.
In [32]: def find_duplicates(x):
b11 = []
for i in x:
if [Link](i)>1:
if i not in b11:
[Link](i)
return b11
In [33]: find_duplicates([1,2,2,4,4,5,5])
Out[33]: [2, 4, 5]
In [34]: find_duplicates([1, 2, 3, 4, 1, 2, 5, 6, 7, 3])
Out[34]: [1, 2, 3]
localhost:8888/notebooks/BASIC PYTHON Practice__Amar [Link] 5/8
2/16/24, 1:13 PM BASIC PYTHON Practice__Amar Sharma - Jupyter Notebook
List comprehension
List comprehension is a concise way of creating lists in Python. It allows you to construct a new list by
applying an expression to each element of an iterable (such as a list, tuple, or range) and optionally
including a condition to filter elements.
The general syntax of a list comprehension is:
[expression for item in iterable if condition]
Where:
expression is the operation you want to perform on each item.
item is a variable representing each element in the iterable.
iterable is the sequence of elements to iterate over.
condition (optional) is a filtering condition that determines whether the item will be included in the
resulting list.
Here's an example to illustrate list comprehension:
# Example 1: Square each number in a list
numbers = [1, 2, 3, 4, 5]
squared_numbers = [x**2 for x in numbers]
print(squared_numbers) # Output: [1, 4, 9, 16, 25]
# Example 2: Extract even numbers from a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers) # Output: [2, 4, 6, 8, 10]
# Example 3: Convert strings to uppercase
words = ["hello", "world", "python"]
uppercase_words = [[Link]() for word in words]
print(uppercase_words) # Output: ['HELLO', 'WORLD', 'PYTHON']
In these examples:
Example 1 squares each number in the numbers list using the expression x**2 .
Example 2 filters out even numbers from the numbers list using the condition x % 2 == 0 .
Example 3 converts each string in the words list to uppercase using the expression
[Link]() .
List comprehensions are concise and readable, making them a preferred choice for creating new lists in
Python. They often result in shorter and more expressive code compared to traditional loops.
In [35]: a = [1,2,3,4,5]
[x**2 for x in a if x%2==0]
Out[35]: [4, 16]
Q12) Reverse Dictionary
Write a Python function called reverse_dictionary that takes a dictionary as input and returns a new
dictionary where the keys and values are swapped.
localhost:8888/notebooks/BASIC PYTHON Practice__Amar [Link] 6/8
2/16/24, 1:13 PM BASIC PYTHON Practice__Amar Sharma - Jupyter Notebook
In [44]: def reverse_dictionary(x):
a12 = {v:k for k,v in [Link]()} # List comprehension
return a12
In [45]: reverse_dictionary({"name":"amar","city":"dhanbad"})
Out[45]: {'amar': 'name', 'dhanbad': 'city'}
Q13) Flatten List
Write a Python function called flatten_list that takes a nested list as input and returns a flattened list,
where all nested lists are flattened into a single level.
In [76]: def flatten_list(x):
a13 = []
for item in x:
if isinstance(item, list):
[Link](flatten_list(item))
else:
[Link](item)
return a13
In [78]: print(flatten_list([1, [2, 3], [4, [5, 6]]]))
print(flatten_list([[1, 2, [3]], 4, [5, [6, 7]], [[8], 9]]))
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Q14) Merge Sorted Lists
Write a Python function called merge_sorted_lists that takes two sorted lists as input and returns a new
sorted list containing all elements from both input lists, merged together.
In [87]: def merge_sorted_lists(x,y):
a14 = []
for i in x:
[Link](i)
for j in y:
[Link](j)
return sorted(a14)
In [88]: print(merge_sorted_lists([8,1, 3, 5, 7], [2, 4, 6, 8]))
print(merge_sorted_lists([-10, 0, 10], [5, 15, 25]))
[1, 2, 3, 4, 5, 6, 7, 8, 8]
[-10, 0, 5, 10, 15, 25]
Q15) Missing numbers
Write a Python function called missing_numbers that takes two lists of integers as input. The function
should return a list of numbers that are missing from the second list, but present in the first list.
localhost:8888/notebooks/BASIC PYTHON Practice__Amar [Link] 7/8
2/16/24, 1:13 PM BASIC PYTHON Practice__Amar Sharma - Jupyter Notebook
In [89]: def missing_numbers(x,y):
a15 = [i for i in x if i not in y]
return a15
In [91]: print(missing_numbers([1, 2, 3, 4, 5], [2, 3, 4]))
print(missing_numbers([10, 11, 12, 13, 14, 15], [10, 12, 13, 15]))
[1, 5]
[11, 14]
In [ ]:
In [ ]:
In [ ]:
In [ ]:
localhost:8888/notebooks/BASIC PYTHON Practice__Amar [Link] 8/8