Basic PYTHON Practice Notes 1708081773
Basic PYTHON Practice Notes 1708081773
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 [4]: a2 = "amma"
In [5]: is_palindrome(a2)
Out[5]: True
In [6]: is_palindrome("amar")
Out[6]: False
In [8]: count_vowels("kviufuyfjvjIFUYFUFJHVJVJCYSAACCV")
Out[8]: 8
In [10]: count_vowels3("kviufuyfjvjIFUYFUFJHVJVJCYSAACCV")
Out[10]: 8
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 [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.
if len(x) != len(y):
return False
x_sorted = sorted(x)
y_sorted = sorted(y)
In [16]: is_anagram("rmrraa","amarrr")
Out[16]: True
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'
b7 = ""
for i in a7:
b7 += i[::-1] + " "
return b7.rstrip()
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 [23]: is_pangram("The quick brown fox jumps over the lazy dog")
Out[23]: True
a88 = x.lower()
b88 = set()
for i in a88:
if i.isalpha():
b88.add(i)
return len(b88) == 26
In [25]: is_pangram88("The quick brown fox jumps over the lazy dog")
Out[25]: True
Out[27]: 'acdeeeeehhiilllnnoorssssttttw'
In [29]: sum_of_numbers(8888,1,9898)
Out[29]: 18787
In [31]: sum_of_digits(123)
Out[31]: 6
return b11
In [33]: find_duplicates([1,2,2,4,4,5,5])
Out[33]: [2, 4, 5]
Out[34]: [1, 2, 3]
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.
Where:
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
word.upper() .
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]
In [45]: reverse_dictionary({"name":"amar","city":"dhanbad"})
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
for j in y:
a14.append(j)
return sorted(a14)
[1, 2, 3, 4, 5, 6, 7, 8, 8]
[-10, 0, 5, 10, 15, 25]
[1, 5]
[11, 14]
In [ ]:
In [ ]:
In [ ]:
In [ ]: