Showing posts with label Python Tips. Show all posts
Showing posts with label Python Tips. Show all posts

Friday, 31 July 2026

๐Ÿš€ Day 101/150 – Password Strength Checker in Python

 

๐Ÿš€ Day 101/150 – Password Strength Checker in Python

A strong password helps protect your accounts from unauthorized access. A good password should contain a mix of uppercase letters, lowercase letters, numbers, and special characters, and it should be at least 8 characters long.

In this post, we'll explore four different ways to check password strength in Python.


Method 1 – Check Password Length

The simplest way to check whether a password is strong is by verifying its length.

password = input("Enter your password: ") if len(password) >= 8: print("Strong Password") else: print("Weak Password")





Sample Input

python123

Output

Strong Password

Explanation

  • input() reads the password entered by the user.

  • len() calculates the password length.

  • If the length is 8 or more, the password is considered strong.

  • Otherwise, it is considered weak.


Method 2 – Check for Uppercase, Lowercase, and Digits

Verify that the password contains different types of characters.

password = input("Enter your password: ") has_upper = any(char.isupper() for char in password) has_lower = any(char.islower() for char in password) has_digit = any(char.isdigit() for char in password) if has_upper and has_lower and has_digit: print("Strong Password") else: print("Weak Password")










Sample Input
Python123

Output

Strong Password

Explanation

  • isupper() checks for uppercase letters.

  • islower() checks for lowercase letters.

  • isdigit() checks for numeric digits.

  • any() returns True if at least one matching character is found.

  • The password is strong only if all three conditions are satisfied.


Method 3 – Check for Special Characters

Require the password to include at least one special character.

password = input("Enter your password: ") special = "!@#$%^&*()_+-=[]{}|;:',.<>?/" has_special = any(char in special for char in password) if has_special: print("Password contains a special character.") else: print("Password needs a special character.")










Sample Input
Python@123

Output

Password contains a special character.

Explanation

  • A string of allowed special characters is created.

  • The program checks whether any character in the password belongs to that string.

  • A password with at least one special character is generally more secure.


Method 4 – Complete Password Strength Checker

Combine all the checks into a single program.

password = input("Enter your password: ") special = "!@#$%^&*()_+-=[]{}|;:',.<>?/" if (len(password) >= 8 and any(char.isupper() for char in password) and any(char.islower() for char in password) and any(char.isdigit() for char in password) and any(char in special for char in password)): print("Strong Password") else: print("Weak Password")











Sample Input

Python@123

Output

Strong Password

Explanation

  • The password must be at least 8 characters long.

  • It must contain:

    • At least one uppercase letter.

    • At least one lowercase letter.

    • At least one digit.

    • At least one special character.

  • If all conditions are met, the password is considered strong.


Comparison of Methods

MethodBest For
Check LengthBasic password validation
Character Type CheckChecking letters and numbers
Special Character CheckImproving password security
Complete Password CheckerReal-world password validation

๐Ÿ”ฅ Key Takeaways

  • A strong password should be at least 8 characters long.

  • Include uppercase letters, lowercase letters, digits, and special characters.

  • Functions like isupper(), islower(), isdigit(), and any() make password validation simple.

  • Combining multiple checks provides better password security.

  • Password strength checkers are commonly used in login and registration systems.

Stay tuned for Day 102 of the #150DaysOfPython series! ๐Ÿš€

Thursday, 30 July 2026

๐Ÿš€ Day 94/150 – Recursive Fibonacci in Python

 

๐Ÿš€ Day 94/150 – Recursive Fibonacci in Python

The Fibonacci sequence is one of the most popular examples used to understand recursion. In this sequence, each number is the sum of the two preceding numbers.

Fibonacci Sequence:



0, 1, 1, 2, 3, 5, 8, 13, 21, ...

In this post, we'll explore four different ways to generate Fibonacci numbers using recursion and related approaches.


Method 1 – Basic Recursive Function

A recursive function calls itself to calculate the Fibonacci number.

def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) print(fibonacci(6))








Output
8

Explanation
    If n is 0 or 1, the function returns n.
  • Otherwise, it returns the sum of the previous two Fibonacci numbers.
  • The function keeps calling itself until it reaches the base case.

Method 2 – Taking User Input

Calculate the Fibonacci number recursively using user input.


def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) num = int(input("Enter the position: ")) print("Fibonacci Number:", fibonacci(num))










Sample Input
7

Output

Fibonacci Number: 13

Explanation

  • The user enters the position in the Fibonacci sequence.
  • The recursive function calculates the Fibonacci number at that position.
  • The result is displayed.

Method 3 – Print Fibonacci Series Using Recursion

Print the first n Fibonacci numbers recursively.
def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) terms = 7 for i in range(terms): print(fibonacci(i), end=" ")












Output
0 1 1 2 3 5 8

Explanation

  • The for loop calls the recursive function for each position.
  • Each Fibonacci number is printed in sequence.
  • This generates the first terms Fibonacci numbers.

Method 4 – Recursive Function with Error Handling

Handle invalid input such as negative numbers.

def fibonacci(n): if n < 0: return "Position cannot be negative." if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) print(fibonacci(-5))












Output
Position cannot be negative.

Explanation

  • The function first checks whether the position is negative.
  • If it is, an error message is returned.
  • Otherwise, recursion proceeds normally.

Comparison of Methods

MethodBest For
Basic RecursionLearning recursion
User InputInteractive programs
Recursive SeriesPrinting multiple Fibonacci numbers
Error HandlingValidating user input

๐Ÿ”ฅ Key Takeaways

  • The Fibonacci sequence is a classic example of recursion.
  • Every recursive function must include a base case to stop recursive calls.
  • Recursive Fibonacci is easy to understand but inefficient for large values because it repeats calculations.
  • Input validation helps prevent invalid recursive calls.
  • For large Fibonacci numbers, iterative or dynamic programming approaches are more efficient than recursion.

Tuesday, 28 July 2026

๐Ÿš€ Day 93/150 – Recursive Factorial in Python

 

๐Ÿš€ Day 93/150 – Recursive Factorial in Python

Recursion is a programming technique where a function calls itself to solve a smaller version of the same problem. One of the most common examples of recursion is calculating the factorial of a number.

The factorial of a positive integer n is the product of all positive integers from 1 to n.

Formula:

5! = 5 × 4 × 3 × 2 × 1 = 120

In this post, we'll explore four different ways to calculate the factorial of a number using recursion and related approaches.


Method 1 – Basic Recursive Function

A recursive function calls itself until it reaches the base case.

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1) print(factorial(5))





Output

120

Explanation
    If n is 0 or 1, the function returns 1.
  • Otherwise, it returns n × factorial(n - 1).
    The function keeps calling itself until the base case is reached.

Method 2 – Taking User Input

Calculate the factorial recursively using user input.

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1) num = int(input("Enter a number: ")) print("Factorial:", factorial(num))









Sample Input
6

Output

Factorial: 720

Explanation
  • The user enters a number.
  • The recursive function calculates its factorial.
  • The result is displayed.

Method 3 – Recursive Function with Error Handling

Prevent negative numbers from being processed.

def factorial(n): if n < 0: return "Factorial is not defined for negative numbers." if n == 0 or n == 1: return 1 return n * factorial(n - 1) print(factorial(-3))










Output
Factorial is not defined for negative numbers.

Explanation

  • The function first checks for negative numbers.
  • If the input is negative, it returns an error message.
  • Otherwise, recursion continues normally.

Method 4 – Recursive Function with Default Argument

Use a default argument to calculate the factorial of 5 if no value is provided.
def factorial(n=5): if n == 0 or n == 1: return 1 return n * factorial(n - 1) print(factorial()) print(factorial(4))





























Explanation

  • The function first checks whether the position is negative.
  • If it is, an error message is returned.
  • Otherwise, recursion proceeds normally. 

Output

120
24
Explanation

Comparison of Methods

MethodBest For
Basic RecursionLearning recursion
User InputInteractive programs
Error HandlingValidating input
Default ArgumentOptional function parameters

๐Ÿ”ฅ Key Takeaways

  • Recursion is a technique where a function calls itself.
  • Every recursive function must have a base case to stop the recursion.
  • Factorial is one of the best examples for understanding recursion.
  • Always validate input before performing recursive calculations.
  • While recursion is elegant, iterative solutions are often more memory-efficient for very large inputs.

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (326) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (315) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (302) Cybersecurity (34) data (10) Data Analysis (43) Data Analytics (31) data management (16) Data Science (412) Data Strucures (18) Deep Learning (209) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (12) flask (4) flutter (1) FPL (17) Generative AI (77) Git (12) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (369) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (15) PHP (20) Projects (34) Python (1353) Python Coding Challenge (1206) Python Mathematics (8) Python Mistakes (51) Python Quiz (587) Python Tips (97) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (54) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)