Sunday, 3 May 2026

πŸš€ Day 39/150 – Print Prime Numbers in a Range in Python

 


πŸš€ Day 39/150 – Print Prime Numbers in a Range in Python

Prime numbers are numbers greater than 1 that have only two factors: 1 and itself.

Examples:

2, 3, 5, 7, 11, 13...

Let’s explore different ways to print prime numbers in a given range using Python πŸ‘‡

πŸ”Ή Method 1 – Using for Loop

start = 1 end = 20 for num in range(start, end + 1): if num > 1: for i in range(2, num): if num % i == 0: break else: print(num, end=" ")





✅ Simple beginner-friendly method.

πŸ”Ή Method 2 – Taking User Input

start = int(input("Enter start: ")) end = int(input("Enter end: ")) for num in range(start, end + 1): if num > 1: for i in range(2, num): if num % i == 0: break else: print(num, end=" ")





✅ Useful for dynamic programs.

πŸ”Ή Method 3 – Optimized Using √n

start = 1 end = 50 for num in range(start, end + 1): if num > 1: is_prime = True for i in range(2, int(num ** 0.5) + 1): if num % i == 0: is_prime = False break if is_prime: print(num, end=" ")





✅ Faster for larger ranges.



πŸ”Ή Method 4 – Using Function

def is_prime(n): if n <= 1: return False for i in range(2, int(n ** 0.5) + 1): if n % i == 0: return False return True for num in range(1, 21): if is_prime(num): print(num, end=" ")






✅ Clean and reusable.


🎯 Output

2 3 5 7 11 13 17 19


πŸ”‘ Key Takeaways

  • Prime numbers are greater than 1.
  • Use nested loops to test each number.
  • Check till √n for optimization.
  • Functions make code reusable.

Python Coding Challenge - Question with Answer (ID -030526)

 


Explanation:

πŸ”Ή Line 1: Creating the List
x = [1, 2, 3]
A list named x is created.
It contains three elements:
Index 0 → 1
Index 1 → 2
Index 2 → 3

πŸ”Ή Line 2: The Print Statement
print(x.pop(1) + x[1])

Let’s break this into parts:

πŸ”Έ Step 1: x.pop(1)
The pop(1) function:
Removes the element at index 1
Returns the removed value

πŸ‘‰ Before pop: x = [1, 2, 3]
πŸ‘‰ After pop: x = [1, 3]
πŸ‘‰ Returned value: 2

πŸ”Έ Step 2: x[1]
Now the updated list is [1, 3]
x[1] refers to the element at index 1

πŸ‘‰ x[1] = 3

πŸ”Έ Step 3: Addition
2 + 3 = 5

πŸ”Ή Final Output
5

Saturday, 2 May 2026

πŸš€ Day 37/150 – Multiplication Table in Python

 

πŸš€ Day 37/150 – Multiplication Table in Python

A multiplication table shows the result of multiplying a number with a series of numbers.

Example for 5:

5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

Let’s explore different ways to print multiplication table in Python πŸ‘‡


πŸ”Ή Method 1 – Using for Loop

n = 5 for i in range(1, 11): print(n, "x", i, "=", n * i)



✅ Most common and easiest method.

πŸ”Ή Method 2 – Taking User Input

n = int(input("Enter a number: ")) for i in range(1, 11): print(n, "x", i, "=", n * i)



✅ Useful for dynamic tables.

πŸ”Ή Method 3 – Using while Loop

n = 5 i = 1 while i <= 10: print(n, "x", i, "=", n * i) i += 1




✅ Good for loop practice.


πŸ”Ή Method 4 – Using List Comprehension

n = 5 table = [n * i for i in range(1, 11)] print(table)



✅ Creates values as a list.


πŸ”Ή Method 5 – Using Function

def table(n): for i in range(1, 11): print(f"{n} x {i} = {n * i}") table(5)



✅ Reusable and clean method.


🎯 Output

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50

πŸ”‘ Key Takeaways

  • Use for loop for fixed repetitions.
  • Use while loop for manual control.
  • f-strings make output cleaner.
  • Functions help reuse code.

πŸ”₯ Follow for Day 38/150 – Prime Number Check in Python


100 Python Projects — From Beginner to Expert

Python Coding Challenge - Question with Answer (ID -020526)

 


Explanation:

πŸ”Ή Step 1: Understand Operator Priority
and has higher priority than or

πŸ‘‰ So expression becomes:

0 or [] or (5 and 6)

πŸ”Ή Step 2: Evaluate (5 and 6)
5 and 6
5 → Truthy ✅
So and returns second value

πŸ‘‰ Result:

6

πŸ”Ή Step 3: Now Expression Becomes
0 or [] or 6
πŸ”Ή Step 4: Evaluate or (Left to Right)
πŸ‘‰ First value:
0
0 → Falsy ❌ → move ahead
πŸ‘‰ Second value:
[]
Empty list → Falsy ❌ → move ahead
πŸ‘‰ Third value:
6
6 → Truthy ✅

πŸ‘‰ or returns first truthy value

πŸ”Ή Step 5: Final Result
6

Friday, 1 May 2026

πŸš€ Day 36/150 – Sum of Digits of a Number in Python

πŸš€ Day 36/150 – Sum of Digits of a Number in Python

The sum of digits means adding all digits of a number.

Examples:
1234 → 1 + 2 + 3 + 4 = 10
507 → 5 + 0 + 7 = 12

Let’s explore different ways to find sum of digits in Python πŸ‘‡

πŸ”Ή Method 1 – Using while Loop

n = 1234 total = 0 while n > 0: digit = n % 10 total += digit n //= 10 print("Sum of digits:", total)









✅ Best method for logic building.

πŸ”Ή Method 2 – Taking User Input

n = int(input("Enter a number: ")) total = 0 temp = abs(n) while temp > 0: total += temp % 10 temp //= 10 print("Sum of digits:", total)





✅ Works for negative numbers too.

πŸ”Ή Method 3 – Using String + sum()

n = 1234 total = sum(int(i) for i in str(abs(n))) print("Sum of digits:", total)





✅ Shortest and cleanest method.

πŸ”Ή Method 4 – Using Recursion

def digit_sum(n): n = abs(n) if n == 0: return 0 return n % 10 + digit_sum(n // 10) print(digit_sum(1234))









✅ Great for recursion practice.

🎯 Output

Sum of digits: 10

πŸ”‘ Key Takeaways

  • Use % 10 to get the last digit.
  • Use // 10 to remove the last digit.
  • sum(int(i) for i in str(n)) is easiest.
  • Use abs(n) for negative numbers.

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (257) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (10) BI (10) Books (262) Bootcamp (11) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (31) data (6) Data Analysis (32) Data Analytics (22) data management (15) Data Science (356) Data Strucures (17) Deep Learning (161) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (73) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (42) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (296) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (14) PHP (20) Projects (33) pytho (1) Python (1341) Python Coding Challenge (1134) Python Mathematics (1) Python Mistakes (51) Python Quiz (495) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (49) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)