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

Stop at 6 with a while loop

Python Practice › Loop Programs › Program 80

Stop at 6 with a while loop

EasyLoop Programs · 80 of 90

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

Output
1
2
3
4
5

Approach

  1. break leaves the loop.

Solution

Python
n = 1while n <= 10:    if n == 6:        break    print(n)    n += 1
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

Last element with a while loop

Python Practice › Loop Programs › Program 79

Last element with a while loop

EasyLoop Programs · 79 of 90

Write a Python program to print the last element of a list using a while loop.

Input
["s", "q", "a", "t", "o", "o", "l", "s"]
Output
s | shortcut: s

Approach

  1. Move an index to the end of the list, then read that item.

Solution

Python
c = ["s", "q", "a", "t", "o", "o", "l", "s"]i = 0while i < len(c) - 1:    i += 1print(c[i], "| shortcut:", c[-1])
Output
s | shortcut: s

Line by line

Lines 3-4

The loop stops on the last valid index.

Line 5

c[-1] is the normal way to read the last item.

Learn the concept

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

More loop programs

← All 90 Loop Programs