DEV Community

Cover image for Daily Python Practice: Bridging the Gap Between Learner and Pro
Bhagyashree
Bhagyashree

Posted on

Daily Python Practice: Bridging the Gap Between Learner and Pro

Python Programs to Practice

1) Print: 1 1 1 1 1

Concept

Printing the same value repeatedly.

Logic

Use a loop to print 1 five times.

Algorithm

  1. Start
  2. Repeat 5 times
  3. Print 1
  4. Stop

Flowchart

                        ┌─────────┐
                        │  START  │
                        └────┬────┘
                             │
                      ┌──────▼──────┐
                      │ count = 1   │
                      └──────┬──────┘
                             │
                      ┌──────▼──────┐
                      │ count <= 5? │
                      └───┬────┬────┘
                        Yes    No
                          │    │
                          │    ▼
                          │  ┌───────┐
                          │  │  END  │
                          │  └───────┘
                          │
                    ┌─────▼─────┐
                    │ Print 1   │
                    └─────┬─────┘
                          │
                    ┌─────▼─────┐
                    │count+=1   │
                    └─────┬─────┘
                          │
                          └───────────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

count = 1

while count <= 5:
    print(1, end=" ")
    count += 1
Enter fullscreen mode Exit fullscreen mode

Result

2) Print: 1 2 3 4 5

Concept

Natural numbers.

Logic

Print numbers from 1 to 5.

Algorithm

  1. Start
  2. Loop from 1 to 5
  3. Print number
  4. Stop

Flowchart

                          START
                            │
                            ▼
                        ┌─────────┐
                        │ num = 1 │
                        └────┬────┘
                             │
                             ▼
                        ┌──────────┐
                        │num <= 5 ?│
                        └──┬────┬──┘
                        Yes│    │No
                           │    ▼
                           │  END
                           │
                           ▼
                        ┌──────────┐
                        │Print num │
                        └────┬─────┘
                             │
                             ▼
                        ┌──────────┐
                        │ num+=1   │
                        └────┬─────┘
                             │
                             └──────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

num = 1

while num <= 5:
    print(num, end=" ")
    num += 1
Enter fullscreen mode Exit fullscreen mode

Result


3) Print: 1 3 5 7 9

Concept

Odd numbers.

Logic

Increase by 2 each time.

Formula

Odd Number = 2n - 1

Algorithm

  1. Start
  2. Loop from 1 to 9 with step 2
  3. Print
  4. Stop

Flowchart

                      START
                        │
                        ▼
                    ┌─────────┐
                    │ num = 1 │
                    └────┬────┘
                         │
                         ▼
                    ┌──────────┐
                    │num <= 9 ?│
                    └──┬────┬──┘
                    Yes│    │No
                       │    ▼
                       │   END
                       │
                       ▼
                    ┌──────────┐
                    │Print num │
                    └────┬─────┘
                         │
                         ▼
                    ┌──────────┐
                    │ num+=2   │
                    └────┬─────┘
                         │
                         └────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

num = 1

while num <= 9:
    print(num, end=" ")
    num += 2
Enter fullscreen mode Exit fullscreen mode

Result


4) Print: 3 6 9 12 15

Concept

Multiples of 3.

Formula

Multiple = 3 × n

Algorithm

  1. Start
  2. Multiply numbers 1 to 5 by 3
  3. Print
  4. Stop

Flowchart

                       START
                         │
                         ▼
                    ┌─────────┐
                    │ num = 3 │
                    └────┬────┘
                         │
                         ▼
                    ┌───────────┐
                    │num <= 15 ?│
                    └──┬─────┬──┘
                    Yes│     │No
                       │     ▼
                       │    END
                       │
                       ▼
                    ┌──────────┐
                    │Print num │
                    └────┬─────┘
                         │
                         ▼
                    ┌──────────┐
                    │ num+=3   │
                    └────┬─────┘
                         │
                         └────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

num = 3

while num <= 15:
    print(num, end=" ")
    num += 3
Enter fullscreen mode Exit fullscreen mode

Result

4.1) Print: 15 12 9 6 3

Logic

Reverse multiples of 3.

Algorithm

Step 1: Start
Step 2: Initialize num = 15
Step 3: Repeat while num ≥ 3
Step 4: Print num
Step 5: num = num - 3
Step 6: Repeat Step 3
Step 7: Stop

Flowchart

                    ┌─────────┐
                    │ START   │
                    └────┬────┘
                         │
                         ▼
                    ┌────────────┐
                    │ num = 15   │
                    └─────┬──────┘
                          │
                          ▼
                    ┌─────────────┐
                    │ num >= 3 ?  │
                    └───┬─────┬───┘
                        Yes    No
                        │      │
                        │      ▼
                        │   ┌──────┐
                        │   │ END  │
                        │   └──────┘
                        │
                        ▼
                ┌─────────────┐
                │ Print num   │
                └─────┬───────┘
                      │
                      ▼
                ┌─────────────┐
                │ num = num-3 │
                └─────┬───────┘
                      │
                      └──────────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

num = 15

while num >= 3:
    print(num, end=" ")
    num -= 3
Enter fullscreen mode Exit fullscreen mode

Result

4.2) Print: 10 8 6 4 2

Concept

Reverse even numbers.

Formula

Even Number = 2n

Algorithm

Step 1: Start
Step 2: Initialize num = 10
Step 3: Repeat while num ≥ 2
Step 4: Print num
Step 5: num = num - 2
Step 6: Repeat Step 3
Step 7: Stop

Flowchart

                    ┌─────────┐
                    │ START   │
                    └────┬────┘
                         │
                         ▼
                    ┌────────────┐
                    │ num = 10   │
                    └─────┬──────┘
                          │
                          ▼
                    ┌─────────────┐
                    │ num >= 2 ?  │
                    └───┬─────┬───┘
                        Yes    No
                        │      │
                        │      ▼
                        │   ┌──────┐
                        │   │ END  │
                        │   └──────┘
                        │
                        ▼
                ┌─────────────┐
                │ Print num   │
                └─────┬───────┘
                      │
                      ▼
                ┌─────────────┐
                │ num = num-2 │
                └─────┬───────┘
                      │
                      └──────────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

num = 10

while num >= 2:
    print(num, end=" ")
    num -= 2
Enter fullscreen mode Exit fullscreen mode

Result

4.3) Print: 9 7 5 3 1

Concept

Reverse odd numbers.

Algorithm

Step 1: Start
Step 2: Initialize num = 9
Step 3: Repeat while num ≥ 1
Step 4: Print num
Step 5: num = num - 2
Step 6: Repeat Step 3
Step 7: Stop

Flowchart

                        ┌─────────┐
                        │ START   │
                        └────┬────┘
                             │
                             ▼
                        ┌────────────┐
                        │ num = 9    │
                        └─────┬──────┘
                              │
                              ▼
                        ┌─────────────┐
                        │ num >= 1 ?  │
                        └───┬─────┬───┘
                            Yes    No
                            │      │
                            │      ▼
                            │   ┌──────┐
                            │   │ END  │
                            │   └──────┘
                            │
                            ▼
                    ┌─────────────┐
                    │ Print num   │
                    └─────┬───────┘
                          │
                          ▼
                    ┌─────────────┐
                    │ num = num-2 │
                    └─────┬───────┘
                          │
                          └──────────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

num = 9

while num >= 1:
    print(num, end=" ")
    num -= 2
Enter fullscreen mode Exit fullscreen mode

Result


5) Multiples of 3 and 5

Concept

Numbers divisible by both 3 and 5.

Formula

n % 3 == 0 and n % 5 == 0

Algorithm

  1. Read limit
  2. Check each number
  3. Print if divisible by both

Flowchart

                     START
                       │
                       ▼
                    ┌─────────┐
                    │ num = 1 │
                    └────┬────┘
                         │
                         ▼
                    ┌────────────┐
                    │num <= 100 ?│
                    └──┬──────┬──┘
                    Yes│      │No
                       │      ▼
                       │     END
                       │
                       ▼
                    ┌────────────────────┐
                    │num%3==0 AND        │
                    │num%5==0 ?          │
                    └──┬─────────────┬───┘
                    Yes│             │No
                       │             │
                       ▼             │
                    ┌──────────┐     │
                    │Print num │     │
                    └────┬─────┘     │
                         │           │
                         └────┬──────┘
                              ▼
                        ┌──────────┐
                        │ num+=1   │
                        └────┬─────┘
                             │
                             └────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

num = 1

while num <= 100:
    if num % 3 == 0 and num % 5 == 0:
        print(num, end=" ")
    num += 1
Enter fullscreen mode Exit fullscreen mode

Result (n=50)

6) Multiples of 3 or 5

Formula

n % 3 == 0 or n % 5 == 0

Algorithm

Step 1: Start
Step 2: Input n
Step 3: Initialize i = 1
Step 4: Repeat while i ≤ n
Step 5: If i % 3 == 0 OR i % 5 == 0, print i
Step 6: Increment i by 1
Step 7: Repeat Step 4
Step 8: Stop

Flowchart

                ┌─────────┐
                │ START   │
                └────┬────┘
                     │
                     ▼
             ┌─────────────┐
             │ Input n     │
             └──────┬──────┘
                    │
                    ▼
             ┌─────────────┐
             │ i = 1       │
             └──────┬──────┘
                    │
                    ▼
             ┌─────────────┐
             │ i <= n ?    │
             └───┬─────┬───┘
                Yes    No
                 │      │
                 │      ▼
                 │   ┌──────┐
                 │   │ END  │
                 │   └──────┘
                 │
                 ▼
      ┌──────────────────────────┐
      │ i % 3 == 0 OR i % 5 ==0 ?│
      └──────┬─────────────┬─────┘
            Yes            No
             │              │
             ▼              │
      ┌─────────────┐       │
      │ Print i     │       │
      └──────┬──────┘       │
             │              │
             └──────┬───────┘
                    ▼
             ┌─────────────┐
             │ i = i + 1   │
             └──────┬──────┘
                    │
                    └──────────► Back to (i <= n ?)
Enter fullscreen mode Exit fullscreen mode

Python Code

n = int(input("Enter limit: "))

for i in range(1, n + 1):
    if i % 3 == 0 or i % 5 == 0:
        print(i, end=" ")
Enter fullscreen mode Exit fullscreen mode

Result (n=20)


7) Divisors of Given Number

Concept

Numbers that divide perfectly.

Formula

n % i == 0

Algorithm

  1. Read number
  2. Check 1 to n
  3. Print divisors

Flowchart

                ┌─────────┐
                │ START   │
                └────┬────┘
                     │
                     ▼
             ┌─────────────┐
             │ Input n     │
             └──────┬──────┘
                    │
                    ▼
             ┌─────────────┐
             │ i = 1       │
             └──────┬──────┘
                    │
                    ▼
             ┌─────────────┐
             │ i <= n ?    │
             └───┬─────┬───┘
                Yes    No
                 │     │
                 │     ▼
                 │   ┌──────┐
                 │   │ END  │
                 │   └──────┘
                 │
                 ▼
           ┌─────────────┐
           │ n % i == 0? │
           └───┬─────┬───┘
              Yes    No
               │      │
               ▼      │
         ┌─────────┐  │
         │ Print i │  │
         └────┬────┘  │
              │       │
              └───┬───┘
                  ▼
           ┌─────────┐
           │ i = i+1 │
           └────┬────┘
                │
                └────────► Back to (i <= n ?)
Enter fullscreen mode Exit fullscreen mode

Python Code

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

for i in range(1, n + 1):
    if n % i == 0:
        print(i, end=" ")
Enter fullscreen mode Exit fullscreen mode

Result

8) Count of Divisors

Concept

Count total divisors.

Algorithm

Step 1: Start
Step 2: Input n
Step 3: Initialize i = 1 and count = 0
Step 4: Repeat while i ≤ n
Step 5: If n % i == 0, increment count by 1
Step 6: Increment i by 1
Step 7: Repeat Step 4
Step 8: Print count
Step 9: Stop

Flowchart

                        START
                           │
                           ▼
                    ┌────────────┐
                    │ Input n    │
                    └────┬───────┘
                         │
                         ▼
                    ┌─────────────────┐
                    │ i=1, count=0    │
                    └────┬────────────┘
                         │
                         ▼
                    ┌──────────┐
                    │ i <= n ? │
                    └──┬────┬──┘
                    Yes│     │No
                       │    ▼
                       │ ┌──────────┐
                       │ │PrintCount│
                       │ └────┬─────┘
                       │      │
                       │     END
                       │
                       ▼
                    ┌────────────┐
                    │ n%i == 0 ? │
                    └──┬─────┬───┘
                    Yes│     │No
                       │     │
                       ▼     │
                    ┌───────────┐
                    │count+=1   │
                    └────┬──────┘
                        │
                        ▼
                    ┌──────────┐
                    │ i = i+1  │
                    └────┬─────┘
                         │
                         └────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

n = int(input("Enter number: "))
i = 1
count = 0

while i <= n:
    if n % i == 0:
        count += 1
    i += 1

print("Count =", count)
Enter fullscreen mode Exit fullscreen mode

Result


9) Prime Number

Concept

Exactly two divisors.

Formula

Count of divisors = 2

Algorithm

  1. Count divisors
  2. If count=2 → Prime

Flowchart

                      START
                        │
                        ▼
                    ┌────────────┐
                    │ Input n    │
                    └────┬───────┘
                         │
                         ▼
                    ┌─────────────────┐
                    │i=1,count=0      │
                    └────┬────────────┘
                         │
                         ▼
                    ┌──────────┐
                    │ i <= n ? │
                    └──┬────┬──┘
                    Yes│    │No
                        │    ▼
                        │ ┌────────────┐
                        │ │count == 2 ?│
                        │ └──┬─────┬───┘
                        │ Yes│     │No
                        │    │     │
                        │    ▼     ▼
                        │ Prime  Not Prime
                        │    │     │
                        │    └──┬──┘
                        │       ▼
                        │      END
                        │
                        ▼
                        ┌────────────┐
                        │ n%i == 0 ? │
                        └──┬─────┬───┘
                        Yes│     │No
                           │     │
                           ▼     ▼
                        ┌──────────┐
                        │count+=1  │
                        └────┬─────┘
                             ▼
                        ┌──────────┐
                        │ i = i+1  │
                        └────┬─────┘
                             │
                             └────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

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

i = 1
count = 0

while i <= n:
    if n % i == 0:
        count += 1
    i += 1

if count == 2:
    print("Prime Number")
else:
    print("Not Prime Number")
Enter fullscreen mode Exit fullscreen mode

Result


10) Perfect Number

Concept

Sum of proper divisors equals the number.

Formula

Sum(divisors except itself) = n

Example:

6 = 1 + 2 + 3
Enter fullscreen mode Exit fullscreen mode

Algorithm

Step 1: Start
Step 2: Input n
Step 3: Initialize i = 1 and total = 0
Step 4: Repeat while i < n
Step 5: If n % i == 0, add i to total
Step 6: Increment i by 1
Step 7: Repeat Step 4
Step 8: If total == n, print "Perfect Number"
Step 9: Else print "Not Perfect Number"
Step 10: Stop

Flowchart

                       START
                        │
                        ▼
                    ┌────────────┐
                    │ Input n    │
                    └────┬───────┘
                         │
                         ▼
                    ┌─────────────────┐
                    │i=1,total=0      │
                    └────┬────────────┘
                         │
                         ▼
                    ┌──────────┐
                    │ i < n ?  │
                    └──┬────┬──┘
                    Yes│    │No
                       │    ▼
                       │ ┌────────────┐
                       │ │ total==n ? │
                       │ └──┬─────┬───┘
                       │ Yes│     │No
                       │    ▼     ▼
                       │ Perfect Not Perfect
                       │    │      │
                       │    └──┬───┘
                       │       ▼
                       │      END
                       │
                       ▼
                    ┌────────────┐
                    │ n%i == 0 ? │
                    └──┬─────┬───┘
                    Yes│     │No
                       │     │
                       ▼     ▼
                    ┌──────────┐
                    │total+=i  │
                    └────┬─────┘
                         ▼
                    ┌──────────┐
                    │ i = i+1  │
                    └────┬─────┘
                         │
                         └────► Back to Decision
Enter fullscreen mode Exit fullscreen mode

Python Code

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

i = 1
total = 0

while i < n:
    if n % i == 0:
        total += i
    i += 1

if total == n:
    print("Perfect Number")
else:
    print("Not Perfect Number")
Enter fullscreen mode Exit fullscreen mode

Result

11) Print 1 3 5 7 9 2 4 6 8 10 in Single Loop

Logic

Print odds first, then evens using one loop.

Algorithm

  • First five iterations → odd numbers
  • Next five iterations → even numbers

Flowchart

                    ┌─────────┐
                    │ START   │
                    └────┬────┘
                         │
                         ▼
                    ┌──────────┐
                    │ i = 1    │
                    └────┬─────┘
                         │
                         ▼
                    ┌──────────┐
                    │ i <= 10 ?│
                    └──┬─────┬─┘
                    Yes│     │No
                       │     ▼
                       │    END
                       │
                       ▼
            ┌──────────────────────┐
            │ i <= 5 ?             │
            └──────┬────────────┬──┘
                Yes│            │No
                   │            │
                   ▼            ▼
        ┌─────────────────┐   ┌─────────────────┐
        │ Print 2*i-1     │   │ Print 2*(i-5)   │
        └──────┬──────────┘   └──────┬──────────┘
               │                     │
               └──────┬──────────────┘
                      ▼
               ┌──────────┐
               │ i = i+1  │
               └────┬─────┘
                    │
                    └────► Back to Decision (i <= 10 ?)
Enter fullscreen mode Exit fullscreen mode

Python Code

i = 1

while i <= 10:
    if i <= 5:
        print(2 * i - 1, end=" ")
    else:
        print(2 * (i - 5), end=" ")
    i += 1
Enter fullscreen mode Exit fullscreen mode

Result

12) Factorial of Given Number

Concept

Product of numbers from 1 to n.

Formula

n! = n × (n − 1) × (n − 2) × ... × 2 × 1
Special case:
0! = 1

Example

5! = 120
Enter fullscreen mode Exit fullscreen mode

Algorithm

  1. fact=1
  2. Multiply 1 to n
  3. Print fact

Flowchart

                    ┌─────────┐
                    │ START   │
                    └────┬────┘
                         │
                         ▼
                    ┌────────────┐
                    │ Input n    │
                    └────┬───────┘
                         │
                         ▼
                    ┌─────────────────┐
                    │ fact=1, i=1     │
                    └────┬────────────┘
                         │
                         ▼
                    ┌──────────┐
                    │ i <= n ? │
                    └──┬─────┬──┘
                    Yes│     │No
                       │     ▼
                       │    END
                       │
                       ▼
                    ┌──────────┐
                    │ fact *= i│
                    └────┬─────┘
                         │
                         ▼
                    ┌──────────┐
                    │ i = i+1  │
                    └────┬─────┘
                         │
                         └────► Back to Decision (i <= n ?)
Enter fullscreen mode Exit fullscreen mode

Python Code

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

fact = 1
i = 1

while i <= n:
    fact *= i
    i += 1

print("Factorial =", fact)
Enter fullscreen mode Exit fullscreen mode

Result


13) Sum of First n Numbers

Concept

Addition of natural numbers.

Formula

Sum = n(n + 1) / 2

Algorithm

  1. Read n
  2. Add 1 to n
  3. Print sum

Flowchart

                       START
                        │
                        ▼
                    ┌────────────┐
                    │ Input n    │
                    └────┬───────┘
                         │
                         ▼
                    ┌────────────────┐
                    │i=1,total=0     │
                    └────┬───────────┘
                         │
                         ▼
                    ┌──────────┐
                    │ i <= n ? │
                    └──┬────┬──┘
                    Yes│    │No
                       │    ▼
                       │ ┌──────────┐
                       │ │Print Sum │
                       │ └────┬─────┘
                       │      │
                       │     END
                       │
                       ▼
                    ┌──────────┐
                    │total+=i  │
                    └────┬─────┘
                         ▼
                    ┌──────────┐
                    │ i=i+1    │
                    └────┬─────┘
                         │
                         └────► Back to Decision (i <= n ?)
Enter fullscreen mode Exit fullscreen mode

Python Code

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

i = 1
total = 0

while i <= n:
    total += i
    i += 1

print("Sum =", total)
Enter fullscreen mode Exit fullscreen mode

Result


14) Print First 5 Prime Numbers

Concept

Generate prime numbers until 5 are found.

Logic

Check each number for primality.

Algorithm

Step 1: Start
Step 2: Initialize no = 2 and prime_count = 0
Step 3: Repeat while prime_count < 5
Step 4: Check whether no is prime
Step 5: If prime, print no and increment prime_count
Step 6: Increment no by 1
Step 7: Go to Step 3
Step 8: Stop

Flowchart

                          START
                            │
                            ▼
                    ┌────────────────────┐
                    │ no=2, count=0      │
                    └─────────┬──────────┘
                              │
                              ▼
                    ┌──────────────────┐
                    │ count < 5 ?      │
                    └─────┬────────┬───┘
                       Yes│        │No
                          │        ▼
                          │       END
                          ▼
                    ┌──────────────────┐
                    │ find_prime(no) ? │
                    └─────┬────────┬───┘
                       Yes│        │No
                          ▼        │
                    ┌───────────┐  │
                    │ Print no  │  │
                    └─────┬─────┘  │
                          ▼        │
                    ┌───────────┐  │
                    │count+=1   │  │
                    └─────┬─────┘  │
                          └──┬─────┘
                             ▼
                    ┌───────────┐
                    │ no = no+1 │
                    └─────┬─────┘
                          │
                          └────► Back to Decision (count < 5 ?)
Enter fullscreen mode Exit fullscreen mode

Python Code

no = 2
prime_count = 0
while prime_count < 5:
    result = find_prime(no)
    if result:
        print(no, end=" ") 
        prime_count+=1
    no+=1

Enter fullscreen mode Exit fullscreen mode

Result

15) Sum of First 5 Prime Numbers

Concept

Generate the first five prime numbers and add them.

Formula

2 + 3 + 5 + 7 + 11 = 28
Enter fullscreen mode Exit fullscreen mode

Algorithm

  1. Initialize sum = 0
  2. Find prime numbers
  3. Stop after 5 primes
  4. Print sum

Flowchart

                      START
                        │
                        ▼
                    ┌────────────────────┐
                    │ no=2, count=0      │
                    └─────────┬──────────┘
                              │
                              ▼
                    ┌──────────────────┐
                    │ count < 5 ?      │
                    └─────┬────────┬───┘
                       Yes│        │No
                          │        ▼
                          │       END
                          ▼
                    ┌──────────────────┐
                    │ find_prime(no) ? │
                    └─────┬────────┬───┘
                       Yes│        │No
                          ▼        │
                    ┌───────────┐  │
                    │ Print no  │  │
                    └─────┬─────┘  │
                          ▼        │
                    ┌───────────┐  │
                    │count+=1   │  │
                    └─────┬─────┘  │
                          └──┬─────┘
                             ▼
                    ┌───────────┐
                    │ no = no+1 │
                    └─────┬─────┘
                          │
                          └────► Back to Decision (count < 5 ?)
Enter fullscreen mode Exit fullscreen mode

Python Code

count = 0
total_sum = 0
num = 2

while count < 5:
    i = 2
    while i < num and num % i != 0:
        i += 1

    if i == num:
        total_sum += num
        count += 1

    num += 1

print("Sum =", total_sum)

Enter fullscreen mode Exit fullscreen mode

Result

Formula Revision Table

Program Formula/Idea
Same number Repeat constant value
Natural numbers i
Odd numbers 2n - 1
Even numbers 2n
Multiples of 3 3 × n
Multiples of 5 5 × n
Divisor n % i == 0
Prime Exactly 2 divisors
Perfect Sum of proper divisors = number
Factorial n! = 1×2×...×n
Sum of first n numbers n(n+1)/2
First 5 primes 2, 3, 5, 7, 11
Sum of first 5 primes 28

Top comments (0)