Home > Backend Development > Python Tutorial > Weekend Tasks - List

Weekend Tasks - List

Linda Hamilton
Release: 2024-12-24 00:37:13
Original
979 people have browsed it

Task:1
s = "a4k3b2"

1) Write a program to get the output 'abbbbklllbcc'

s = "a4k3b2"
output = ""
i = 0

while i < len(s):
    first = s[i]  
    second =s[i + 1] 
    if second.isdigit():
        alpha=chr(ord(first)+1)
        output=output+ first+ (int(second)*alpha)
        i+=2

print(output)
Copy after login

Output:

abbbbklllbcc

2) Write a program to get the output 'aaaaakkkkbbb'

s = "a4k3b2"
output = ""
i = 0

while i < len(s):
    first = s[i]  
    second =s[i + 1] 
    if second.isdigit():
        output=output+ first+ (int(second)*first)
        i+=2

print(output)
Copy after login

Output:

aaaaakkkkbbb

Task:2

matrix = [[10,20,30], [40,50,60], [70,80,90]]

Join the given matrix into single list using comprehensive for and normal for loop.
Method:1(Using normal for loop)

matrix = [[10,20,30], [40,50,60], [70,80,90]]
output=[]

for i in matrix:
    for j in i:
        output.append(j)
print(output)
Copy after login

Method:2(Using comprehensive for loop)

matrix = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]

output = [j for i in matrix for j in i]
print(output)
Copy after login

Output:

[10, 20, 30, 40, 50, 60, 70, 80, 90]
Copy after login

Task:3
l = ['ABC','DEF', 'GHI', 'JKL']
Get OUTPUT: ['ABC', 'def','GHI', 'jkl']

l = ['ABC', 'DEF', 'GHI', 'JKL']

output = [] 
for i, alpha in enumerate(l):
    if i % 2 != 0:
        output.append(alpha.casefold())
    else:
        output.append(alpha)
print(output)
Copy after login

Output:

['ABC', 'def', 'GHI', 'jkl']
Copy after login

Transpose Matrix: Transpose of a matrix is obtained by changing rows to columns and columns to rows.

Weekend Tasks - List

The above is the detailed content of Weekend Tasks - List. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template