Print the keys of a dictionary

Python Practice › Loop Programs › Program 66

Print the keys of a dictionary

EasyLoop Programs · 66 of 90

Write a Python program to print the keys of a dictionary using a loop.

Input
{"name": "virat", "sports": "cricket"}
Output
name
sports

Approach

  1. Looping over a dict gives its keys.

Solution

Python
player = {"name": "virat", "sports": "cricket"}for key in player:    print(key)
Output
name
sports

Learn the concept

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

More loop programs

← All 90 Loop Programs

Odd numbers 1-10 into a list

Python Practice › Loop Programs › Program 65

Odd numbers 1-10 into a list

EasyLoop Programs · 65 of 90

Write a Python program that adds the odd numbers from 1 to 10 into an empty list.

Output
[1, 3, 5, 7, 9]

Approach

  1. Append each odd number.

Solution

Python
odds = []for n in range(1, 11):    if n % 2 == 1:        odds.append(n)print(odds)
Output
[1, 3, 5, 7, 9]

Learn the concept

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

More loop programs

← All 90 Loop Programs

Even numbers 1-10 into a list

Python Practice › Loop Programs › Program 64

Even numbers 1-10 into a list

EasyLoop Programs · 64 of 90

Write a Python program that adds the even numbers from 1 to 10 into an empty list.

Output
[2, 4, 6, 8, 10]

Approach

  1. Append each even number.

Solution

Python
evens = []for n in range(1, 11):    if n % 2 == 0:        evens.append(n)print(evens)
Output
[2, 4, 6, 8, 10]

Learn the concept

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

More loop programs

← All 90 Loop Programs

Each character on a new line

Python Practice › Loop Programs › Program 63

Each character on a new line

EasyLoop Programs · 63 of 90

Write a Python program to print each character of a string on a new line.

Input
"Sqatools"
Output
S
q
a
t
o
o
l
s

Approach

  1. Loop over the string; print() adds a newline after each character.

Solution

Python
for ch in "Sqatools":    print(ch)
Output
S
q
a
t
o
o
l
s

Learn the concept

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

More loop programs

← All 90 Loop Programs

Display numbers from a list

Python Practice › Loop Programs › Program 62

Display numbers from a list

EasyLoop Programs · 62 of 90

Write a Python program to display the numbers of a list separated by commas.

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

Approach

  1. Print each number with end="," except after the last one.

Solution

Python
numbers = [1, 5, 8, 0, 4]for i, n in enumerate(numbers):    end = "," if i < len(numbers) - 1 else "\n"    print(n, end=end)
Output
1,5,8,0,4

Line by line

Line 3

Every item except the last is followed by a comma.

Learn the concept

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

More loop programs

← All 90 Loop Programs

First 20 natural numbers

Python Practice › Loop Programs › Program 61

First 20 natural numbers

EasyLoop Programs · 61 of 90

Write a Python program to print the first 20 natural numbers with a for loop, separated by commas.

Output
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

Approach

  1. Join the numbers with commas.

Solution

Python
print(",".join(str(n) for n in range(1, 21)))
Output
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20

Line by line

Line 1

join needs strings, so each number is converted with str().

Learn the concept

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

More loop programs

← All 90 Loop Programs

Table of 5 with a for loop

Python Practice › Loop Programs › Program 60

Table of 5 with a for loop

EasyLoop Programs · 60 of 90

Write a Python program to print the multiplication table of 5 with a for loop.

Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Approach

  1. Loop i from 1 to 10.

Solution

Python
for i in range(1, 11):    print(f"5 x {i} = {5 * i}")
Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Learn the concept

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

More loop programs

← All 90 Loop Programs

Star triangle up and down

Python Practice › Loop Programs › Program 59

Star triangle up and down

EasyLoop Programs · 59 of 90

Write a Python program to print stars growing from 1 to 5 and back to 1.

Output
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Approach

  1. Build the list of row sizes 1..5..1 and print each.

Solution

Python
n = 5sizes = list(range(1, n + 1)) + list(range(n - 1, 0, -1))for s in sizes:    print("* " * s)
Output
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Line by line

Line 2

The two ranges give 1, 2, 3, 4, 5, 4, 3, 2, 1.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Does a string contain a digit?

Python Practice › Loop Programs › Program 58

Does a string contain a digit?

EasyLoop Programs · 58 of 90

Write a Python program to check whether a string contains a digit.

Input
"Python123", "Python"
Output
Python123 True
Python False

Approach

  1. Loop until a digit is found; use for ... else for the “not found” case.

Solution

Python
for text in ["Python123", "Python"]:    for ch in text:        if ch.isdigit():            print(text, True)            break    else:        print(text, False)
Output
Python123 True
Python False

Line by line

Line 5

break stops at the first digit.

Lines 6-7

The else runs only when the loop ended without finding one.

Learn the concept

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

More loop programs

← All 90 Loop Programs

Validate a password

Python Practice › Loop Programs › Program 57

Validate a password

MediumLoop Programs · 57 of 90

Write a Python program to check a password: at least one lowercase letter, one uppercase letter, one digit, one of $ # @, and 5 to 15 characters long.

Input
"Abc@1234", "abc123", "Ab1$"
Output
Abc@1234 -> Valid password
abc123 -> Invalid: needs an uppercase letter, needs one of $#@
Ab1$ -> Invalid: length must be 5-15

Approach

  1. Loop over the characters once, recording which kinds were seen.
  2. Check all rules at the end and list any that failed.

Solution

Python
def check(password):    has_lower = has_upper = has_digit = has_special = False    for ch in password:        if ch.islower():            has_lower = True        elif ch.isupper():            has_upper = True        elif ch.isdigit():            has_digit = True        elif ch in "$#@":            has_special = True    problems = []    if not 5 <= len(password) <= 15:        problems.append("length must be 5-15")    if not has_lower:        problems.append("needs a lowercase letter")    if not has_upper:        problems.append("needs an uppercase letter")    if not has_digit:        problems.append("needs a digit")    if not has_special:        problems.append("needs one of $#@")    return "Valid password" if not problems else "Invalid: " + ", ".join(problems) for pw in ["Abc@1234", "abc123", "Ab1$"]:    print(pw, "->", check(pw))
Output
Abc@1234 -> Valid password
abc123 -> Invalid: needs an uppercase letter, needs one of $#@
Ab1$ -> Invalid: length must be 5-15

Line by line

Line 2

Four flags start as False.

Lines 3-11

One pass over the password sets the flags.

Lines 12-22

Each rule adds a readable reason when it fails, so the user knows what to fix.

Learn the concept

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

More loop programs

← All 90 Loop Programs