my_list = [1, 2, 3, 4, 5]
print("Original List:", my_list)
print("Reversed List:", my_list[::-1])
Original List: [1, 2, 3, 4, 5]
Reversed List: [5, 4, 3, 2, 1]
s = "PYTHONPROGRAMMING"
print("Left to Right:", s[::2])
print("Right to Left:", s[::-2])
Left to Right: PTOPORMIG
Right to Left: GIMROPOTP
s = input("Enter a string: ")
vowels = "aeiouAEIOU"
v = c = 0
for char in s:
if char.isalpha():
if char in vowels:
v += 1
else:
c += 1
print("Vowels:", v)
print("Consonants:", c)
Enter a string: hii my name is arjun
Vowels: 7
Consonants: 9
nums = list(map(int, input("Enter 5 integers: ").split()))
if len(set(nums)) < 5:
print("DUPLICATES")
else:
print("ALL UNIQUE")
Enter 5 integers: 1 2 3 4 5
ALL UNIQUE
from collections import Counter
s = 'google.com'
freq = Counter(s)
print(freq)
Counter({'o': 3, 'g': 2, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1})
s = input("Enter a string: ")
result = s[::2]
print("Result:", result)
Enter a string: hii
Result: hi
stack = []
stack.append('A')
stack.append('B')
stack.append('C')
print("Stack:", stack)
print("Popped:", stack.pop())
print("Stack after pop:", stack)
Stack: ['A', 'B', 'C']
Popped: C
Stack after pop: ['A', 'B']
s = 'restart'
char = s[0]
s_new = char + s[1:].replace(char, '$')
print("Modified string:", s_new)
Modified string: resta$t
def get_string(s):
if len(s.strip()) < 2:
return ''
return s[:2] + s[-2:]
print(get_string('General12'))
print(get_string('Ka'))
print(get_string(' K'))
Ge12
KaKa
def mix_strings(a, b):
new_a = b[:2] + a[2:]
new_b = a[:2] + b[2:]
return new_a + " " + new_b
print(mix_strings('abc', 'xyz'))
xyc abz
sentence = "this is a test this is only a test"
words = sentence.split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
print(freq)
{'this': 2, 'is': 2, 'a': 2, 'test': 2, 'only': 1}
queue = []
queue.append('A')
queue.append('B')
queue.append('C')
print("Queue:", queue)
print("Dequeued:", queue.pop(0))
print("Queue after dequeue:", queue)
Queue: ['A', 'B', 'C']
Dequeued: A
Queue after dequeue: ['B', 'C']
from collections import Counter
s = 'thequickbrownfoxjumpsoverthelazydog'
count = Counter(s)
for char, freq in count.items():
if freq > 1:
print(f"{char} {freq}")
t 2
h 2
e 3
u 2
r 2
o 4
def binary_search(arr, x):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1
arr = [1, 3, 5, 7, 9, 11]
x = 7
print("Element found at index:", binary_search(arr, x))
Element found at index: 3