Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

python popular codes

these are some python codes

Uploaded by

Wandering Mind
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python popular codes

these are some python codes

Uploaded by

Wandering Mind
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PYTHON ASSINGNMENT

1) tuple1 = (10, 20, 30, 40, 50)


tuple1 = tuple1[ : :-1]
print(tuple1)

2) tuple1 = ("Orange", [10, 20, 30], (5, 15, 25))


print(tuple1[1][1])

3) tuple1= (50, )
print(tuple1)

4) tuple1 = (10, 20, 30, 40)

a, b, c, d = tuple1
print(a)
print(b)
print(c)
print(d)

5) tuple1 = (11, 22)


tuple2 = (55, 44)
tuple1, tuple2 = tuple2, tuple1
print(tuple2)
print(tuple1)

6) tuple1 = (11, 22, 33, 44, 55, 66)

tuple2 = tuple1[3:-1]

print(tuple2)

7) tuple1 = (11, [22, 33], 44, 55)


tuple1[1][0] = 222
print(tuple1)

8) tuple1 = (('a', 23), ('b', 37), ('c', 11), ('d', 29))

tuple1 = tuple(sorted(list(tuple1), key=lambda x: x[1]))

print(tuple1)

9) tuple1 = (50, 10, 60, 70, 50)


print(tuple1.count(50))

10) def count(s, c) :

res = 0

for i in range(len(s)) :
PYTHON ASSINGNMENT
if (s[i] == c):

res = res + 1

return res

str= "geeksforgeeks"

c = 'e'

print(count(str, c))

11) list1 = [5, 20, 15, 20, 25, 50, 20]

def remove_value(sample_list, val):


return [i for i in sample_list if i != val]

res = remove_value(list1, 20)


print(res)

12) list1 = [5, 10, 15, 20, 25, 50, 20]

index = list1.index(20)

list1[index] = 200
print(list1)

13) list1 = ["Bhanu", "", "Karthik", "Bhavani", "", "Maha"]

res = list(filter(None, list1))


print(res)

14) list1 = ["M", "na", "i", "Ke"]


list2 = ["y", "me", "s", "lly"]
list3 = [i + j for i, j in zip(list1, list2)]
print(list3)

15) str1 = 'James'


print("Original String is", str1)
res = str1[0]
l = len(str1)
mi = int(l / 2)
res = res + str1[mi]
res = res + str1[l - 1]
print("New String:", res)

16) def get_middle_three_chars(str1):


print("Original String is", str1)
PYTHON ASSINGNMENT
mi = int(len(str1) / 2)
res = str1[mi - 1:mi + 2]
print("Middle three chars are:", res)
get_middle_three_chars("DhoniTheKing")
get_middle_three_chars("JaSonAy")

17) def append_middle(s1, s2):


print("Original Strings are", s1, s2)
mi = int(len(s1) / 2)
x = s1[:mi:]
x = x + s2
x = x + s1[mi:]
print("After appending new string in middle:", x)
append_middle("Bhanu", "Bhavani")

18) def mix_string(s1, s2):

first_char = s1[0] + s2[0]


middle_char = s1[int(len(s1) / 2):int(len(s1) / 2) + 1] + s2[int(len(s2) / 2):int(len(s2) / 2) + 1]
last_char = s1[len(s1) - 1] + s2[len(s2) - 1]
res = first_char + middle_char + last_char
print("Mix String is ", res)

s1 = "India"
s2 = "China"
mix_string(s1, s2)

19) str1 = "BHanUTejA"


print('Original String:', str1)
lower = []
upper = []
for char in str1:
if char.islower():

lower.append(char)
else:
upper.append(char)
sorted_str = ''. join(lower + upper)
print('Result:', sorted_str)

20) def find_digits_chars_symbols(sample_str):


char_count = 0
digit_count = 0
symbol_count = 0
for char in sample_str:
if char.isalpha():
char_count += 1
elif char.isdigit():
digit_count += 1
PYTHON ASSINGNMENT
else:
symbol_count += 1

print("Chars =", char_count, "Digits =", digit_count, "Symbol =", symbol_count)

sample_str = "P@yn2at&#i5ve"
print("total counts of chars, Digits, and symbols \n")
find_digits_chars_symbols(sample_str)

21) s1 = "Abc"
s2 = "Xyz"
s1_length = len(s1)
s2_length = len(s2)
length = s1_length if s1_length > s2_length else s2_length
result = ""
s2 = s2[::-1]
for i in range(length):
if i < s1_length:
result = result + s1[i]
if i < s2_length:
result = result + s2[i]

print(result)

22)

test_str = "GeeksforGeeks is best for Geeks"

test_sub = "Geeks"

print("The original string is : " + test_str)

print("The substring to find : " + test_sub)

res = [i for i in range(len(test_str)) if test_str.startswith(test_sub, i)]

print("The start indices of the substrings are : " + str(res))

23) n = int(input("Enter number"))


PYTHON ASSINGNMENT
sum = 0

for num in range(1, n + 1, 1):

sum = sum + num

print("Sum of first ", n, "numbers is: ", sum)

average = sum / n

print("Average of ", n, "numbers is: ", average)

24) def reverse_word(s, start, end):

while start < end:

s[start], s[end] = s[end], s[start]

start = start + 1

end -= 1

s = "i like this program very much"

s = list(s)

start = 0

while True:

try:

end = s.index(' ', start)

reverse_word(s, start, end - 1)

start = end + 1

except ValueError:

reverse_word(s, start, len(s) - 1)

break

s.reverse()

s = "".join(s)
PYTHON ASSINGNMENT
print(s)

25) str1 = "Bhanu"


print("Original String is:", str1)

str1 = ''.join(reversed(str1))
print("Reversed String is:", str1)

26) test_string = "GfG is best for CS and also best for Learning"

tar_word = "best"

print("The original string : " + str(test_string))

res = test_string.rindex(tar_word)

print("Index of last occurrence of substring is : " + str(res))

27) text= "I am Batman"

splitted_text= text.split()

(splitted_text)

28)

test_list = ["", "AlltheBest", "", "is", "best", ""]

print ("Original list is : " + str(test_list))

while("" in test_list) :

test_list.remove("")

print ("Modified list is : " + str(test_list))

29) import string

a_string = '!hi. wh?at is the weat[h]er lik?e.'

new_string = a_string.translate(str.maketrans('', '', string.punctuation))

print(new_string)

30) import re

ini_string = "123abcjw:, .@! eiw"


PYTHON ASSINGNMENT
print ("initial string : ", ini_string)

result = re.sub('[\W_]+', '', ini_string)

31) test_str = 'geeksfor23geeks is best45 for gee34ks and cs'

print("The original string is : " + test_str)

res = []

temp = test_str.split()

for idx in temp:

if any(chr.isalpha() for chr in idx) and any(chr.isdigit() for chr in idx):

res.append(idx)

print("Words with alphabets and numbers : " + str(res))

You might also like