Lowercase letter diamond

Python Practice › Loop Programs › Program 90

Lowercase letter diamond

MediumLoop Programs · 90 of 90

Write a Python program to print the letters a to y in rows of 1, 3, 5, 7, 5, 3, 1 letters.

Output
a
b c d
e f g h i
j k l m n o p
q r s t u
v w x
y

Approach

  1. Same idea as the previous program with different row sizes.

Solution

Python
import string letters = iter(string.ascii_lowercase)for size in [1, 3, 5, 7, 5, 3, 1]:    print(" ".join(next(letters) for _ in range(size)))
Output
a
b c d
e f g h i
j k l m n o p
q r s t u
v w x
y

Line by line

Line 3

An iterator hands out the letters one at a time across all rows.

Line 5

next(letters) takes the following letter each time it is called.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Uppercase letter diamond

Python Practice › Loop Programs › Program 89

Uppercase letter diamond

MediumLoop Programs · 89 of 90

Write a Python program to print the letters A to Y in rows of 1, 2, 3, 4, 5, 4, 3, 2, 1 letters.

Output
A
B C
D E F
G H I J
K L M N O
P Q R S
T U V
W X
Y

Approach

  1. Keep a letter counter and print the right number of letters on each row.

Solution

Python
sizes = [1, 2, 3, 4, 5, 4, 3, 2, 1]code = ord("A")for size in sizes:    row = [chr(code + i) for i in range(size)]    print(" ".join(row))    code += size
Output
A
B C
D E F
G H I J
K L M N O
P Q R S
T U V
W X
Y

Line by line

Line 2

ord("A") is 65; adding to it walks through the alphabet.

Line 4

Each row takes the next size letters.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Maximum number in a list

Python Practice › Loop Programs › Program 88

Maximum number in a list

EasyLoop Programs · 88 of 90

Write a Python program to find the largest number in a list using a loop.

Input
[12, 14, 45, 88, 63, 97, 88]
Output
Maximum number: 97

Approach

  1. Assume the first item is the largest and replace it whenever a bigger one appears.

Solution

Python
numbers = [12, 14, 45, 88, 63, 97, 88]largest = numbers[0]for n in numbers[1:]:    if n > largest:        largest = nprint("Maximum number:", largest)
Output
Maximum number: 97

Line by line

Line 2

Starting from the first item works for negative numbers too, unlike starting from 0.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Copy and sort descending

Python Practice › Loop Programs › Program 87

Copy and sort descending

EasyLoop Programs · 87 of 90

Write a Python program to copy elements from one list into an empty list and print them in descending order.

Input
[2, 5, 8, 0, 1, 4]
Output
[8, 5, 4, 2, 1, 0]

Approach

  1. Copy with a loop, then sort with reverse=True.

Solution

Python
a = [2, 5, 8, 0, 1, 4]b = []for x in a:    b.append(x)b.sort(reverse=True)print(b)
Output
[8, 5, 4, 2, 1, 0]

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Sort a list with loops

Python Practice › Loop Programs › Program 86

Sort a list with loops

MediumLoop Programs · 86 of 90

Write a Python program to sort a list using loops (without sort()).

Input
[6, 8, 2, 3, 1, 0, 5]
Output
[0, 1, 2, 3, 5, 6, 8]

Approach

  1. Bubble sort: compare neighbours and swap them when they are in the wrong order; repeat.

Solution

Python
numbers = [6, 8, 2, 3, 1, 0, 5]n = len(numbers)for i in range(n - 1):    for j in range(n - 1 - i):        if numbers[j] > numbers[j + 1]:            numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]print(numbers)
Output
[0, 1, 2, 3, 5, 6, 8]

Line by line

Line 3

After each outer pass the largest remaining item has “bubbled” to the end.

Line 4

The inner loop can stop earlier each time, because the end is already sorted.

Line 6

Tuple assignment swaps the two neighbours.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Count special characters in a file

Python Practice › Loop Programs › Program 85

Count special characters in a file

EasyLoop Programs · 85 of 90

Write a Python program to count the special characters in a file.

Input
file.txt: "student@gmail.com" / "##comment"
Output
4

Approach

  1. Count characters that are not letters, digits or whitespace.

Solution

Python
with open("file.txt", "w") as f:    f.write("student@gmail.com\n##comment\n") count = 0with open("file.txt") as f:    for line in f:        for ch in line:            if not ch.isalnum() and not ch.isspace():                count += 1print(count)
Output
4
Note

The file has 4 special characters: @, . and two #. The original sample answer (3) missed the dot.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Count uppercase letters in a file

Python Practice › Loop Programs › Program 84

Count uppercase letters in a file

EasyLoop Programs · 84 of 90

Write a Python program to count the uppercase letters in a file.

Input
file.txt: "Virat Kohli scored 100 in the last match"
Output
2

Approach

  1. Count characters where isupper() is True.

Solution

Python
with open("file.txt", "w") as f:    f.write("Virat Kohli scored 100 in the last match") with open("file.txt") as f:    text = f.read()print(sum(1 for ch in text if ch.isupper()))
Output
2

Line by line

Line 5

sum(1 for ...) counts the matching characters.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Count digits in a file

Python Practice › Loop Programs › Program 83

Count digits in a file

EasyLoop Programs · 83 of 90

Write a Python program to count the digits in a file.

Input
file.txt: "Virat Kohli scored 100 in the last match"
Output
3

Approach

  1. Loop over every character of the file and count isdigit().

Solution

Python
with open("file.txt", "w") as f:    f.write("Virat Kohli scored 100 in the last match") count = 0with open("file.txt") as f:    for ch in f.read():        if ch.isdigit():            count += 1print(count)
Output
3

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Count characters in a file

Python Practice › Loop Programs › Program 82

Count characters in a file

EasyLoop Programs · 82 of 90

Write a Python program to count the characters in a file (letters, digits, punctuation and spaces, not counting newlines).

Input
file.txt: "I'm learning python" / "At" / "Sqatools"
Output
29

Approach

  1. Read the file line by line and add the length of each line without its newline.

Solution

Python
with open("file.txt", "w") as f:    f.write("I'm learning python\nAt\nSqatools\n") count = 0with open("file.txt") as f:    for line in f:        count += len(line.rstrip("\n"))print(count)
Output
29

Line by line

Lines 1-2

Creates the sample file so the program runs anywhere.

Lines 6-7

rstrip("\n") removes the newline before counting.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Stop at 6 with a for loop

Python Practice › Loop Programs › Program 81

Stop at 6 with a for loop

EasyLoop Programs · 81 of 90

Write a Python program to print 1 to 10 but stop when the number is 6, using a for loop.

Output
1
2
3
4
5

Approach

  1. break leaves the for loop.

Solution

Python
for n in range(1, 11):    if n == 6:        break    print(n)
Output
1
2
3
4
5

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs