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

Thursday, 20 August 2026

๐Ÿš€ Day 102/150 – Email Validation Program in Python

 

๐Ÿš€ Day 102/150 – Email Validation Program in Python

Email validation is a common task in many applications such as registration forms, login systems, and contact forms. A valid email address should follow a proper format, such as containing an @ symbol, a domain name, and a valid extension.

In this post, we'll explore four different ways to validate an email address in Python.


Method 1 – Basic Email Validation

Check whether the email contains both @ and ..

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

user@example.com

Output
Valid Email

Explanation

  • input() reads the email address.

  • The program checks if the email contains both @ and ..

  • If both are present, it considers the email valid.

  • Otherwise, it prints "Invalid Email".


Method 2 – Check Email Format

Ensure the email contains exactly one @ and ends with a common domain extension.


email = input("Enter your email: ") if email.count("@") == 1 and email.endswith((".com", ".org", ".net")): print("Valid Email") else: print("Invalid Email")







Sample Input
python@gmail.com

Output

Valid Email

Explanation

    count("@") ensures there is only one @.
    endswith() checks if the email ends with .com, .org, or .net.
    Both conditions must be true for the email to be valid.

Method 3 – Using Regular Expressions

Use Python's re module for more accurate email validation.

import re email = input("Enter your email: ") pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" if re.match(pattern, email): print("Valid Email") else: print("Invalid Email")










Sample Input
hello123@gmail.com

Output

Valid Email

Explanation

  • The re module provides support for regular expressions.

  • re.match() checks whether the email matches the specified pattern.

  • This method is more reliable than checking only for @ and ..


Method 4 – Validate Multiple Email Addresses

Check several email addresses stored in a list.


emails = [ "alice@gmail.com", "bob@yahoo", "charlie@example.com" ] for email in emails: if "@" in email and "." in email: print(email, "- Valid") else: print(email, "- Invalid")











Output
alice@gmail.com - Valid 
bob@yahoo - Invalid 
charlie@example.com - Valid

Explanation

  • A list of email addresses is created.

  • The for loop checks each email one by one.

  • Emails containing both @ and . are marked as valid.

  • Others are marked as invalid.


Comparison of Methods

MethodBest For
Basic ValidationBeginners learning string operations
Format CheckSimple real-world validation
Regular ExpressionsAccurate email validation
Multiple EmailsValidating lists of email addresses

๐Ÿ”ฅ Key Takeaways

  • Email validation helps ensure users enter properly formatted email addresses.

  • Basic validation checks for the presence of @ and ..

  • count() and endswith() provide additional format checks.

  • The re module offers a more robust way to validate email addresses using regular expressions.

  • Email validation is commonly used in registration forms, login systems, and web applications.

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

Thursday, 6 August 2026

๐Ÿš€ Day 96/150 – map() Function in Python

 



๐Ÿš€ Day 96/150 – map() Function in Python

The map() function is a built-in Python function used to apply a function to every item in an iterable, such as a list or tuple. It helps you write cleaner and more concise code by avoiding explicit loops.

Syntax:

map(function, iterable)

In this post, we'll explore four common examples of using the map() function in Python.


Method 1 – Using map() with a Normal Function

Apply a normal function to every element in a list.

def square(num): return num ** 2 numbers = [1, 2, 3, 4, 5] result = list(map(square, numbers)) print(result)








Output

[1, 4, 9, 16, 25]

Explanation

  • square() returns the square of a number.
  • map() applies the square() function to every element in numbers.
  • list() converts the map object into a list.

Method 2 – Using map() with a Lambda Function

Use a lambda function for shorter code.

numbers = [2, 4, 6, 8] result = list(map(lambda x: x * 2, numbers)) print(result)





Output

[4, 8, 12, 16]

Explanation

  • lambda x: x * 2 doubles each element.
  • map() applies the lambda function to every item in the list.
  • The result is converted into a list.

Method 3 – Using map() with Multiple Iterables

map() can process multiple iterables at the same time.

list1 = [1, 2, 3] list2 = [4, 5, 6] result = list(map(lambda x, y: x + y, list1, list2)) print(result)






Output

[5, 7, 9]

Explanation

  • map() takes one element from each list at the same position.
  • The lambda function adds the corresponding elements.
  • The result is returned as a new list.

Method 4 – Taking User Input

Use map() to convert multiple user inputs into integers.

numbers = list(map(int, input("Enter numbers separated by spaces: ").split())) print(numbers)




Sample Input

10 20 30 40

Output

[10, 20, 30, 40]

Explanation

  • input() reads the values as a string.
  • split() separates the string into a list of strings.
  • map(int, ...) converts each string into an integer.
  • list() stores the converted values in a list.

Comparison of Methods

MethodBest For
Normal FunctionReusing existing functions
Lambda FunctionShort and simple operations
Multiple IterablesProcessing two or more lists together
User InputConverting input values to the desired data type

๐Ÿ”ฅ Key Takeaways

  • map() applies a function to every element in an iterable.
  • It returns a map object, which is often converted to a list using list().
  • map() works with both normal functions and lambda functions.
  • It can process multiple iterables simultaneously.
  • map() makes code cleaner and often replaces explicit for loops for simple transformations.

Tuesday, 4 August 2026

๐Ÿš€ Day 95/150 – Lambda Function Examples in Python



๐Ÿš€ Day 95/150 – Lambda Function Examples in Python

A lambda function is a small, anonymous function in Python. It is useful when you need a simple function for a short period without defining it using the def keyword.

The syntax of a lambda function is:

lambda arguments: expression

In this post, we'll explore four common examples of lambda functions in Python.


Method 1 – Simple Lambda Function

Create a lambda function to add two numbers.

add = lambda a, b: a + b print(add(5, 3))



Output

8
Explanation
  • lambda a, b: defines an anonymous function with two parameters.

  • a + b is the expression whose result is returned automatically.

  • add(5, 3) returns 8.

Method 2 – Lambda with map()

Use a lambda function with map() to square each element in a list.

numbers = [1, 2, 3, 4, 5] squares = list(map(lambda x: x ** 2, numbers)) print(squares)






Output
[1, 4, 9, 16, 25]

Explanation

  • map() applies the lambda function to every element in the list.

  • lambda x: x ** 2 returns the square of each number.

  • list() converts the result into a list.


Method 3 – Lambda with filter()

Use a lambda function to filter even numbers from a list.


numbers = [1, 2, 3, 4, 5, 6] even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers)







Output
[2, 4, 6]

Explanation

  • filter() keeps only the elements for which the lambda function returns True.

  • lambda x: x % 2 == 0 checks whether a number is even.

  • The result is converted into a list.


Method 4 – Lambda with sorted()

Sort a list of tuples based on the second element.

students = [ ("Alice", 85), ("Bob", 92), ("Charlie", 78) ] sorted_students = sorted(students, key=lambda student: student[1]) print(sorted_students)









Output
[('Charlie', 78), ('Alice', 85), ('Bob', 92)]

Explanation

  • sorted() sorts the list.

  • The key parameter specifies the sorting rule.

  • lambda student: student[1] tells Python to sort using the second element (marks).


Comparison of Methods

MethodBest For
Simple LambdaShort mathematical operations
map()Transforming every element
filter()Selecting elements based on a condition
sorted()Custom sorting

๐Ÿ”ฅ Key Takeaways

  • A lambda function is a small anonymous function written in a single line.

  • It is best suited for short and simple operations.

  • map() uses lambda functions to transform data.

  • filter() uses lambda functions to select matching elements.

  • sorted() uses lambda functions to define custom sorting rules.

  • For complex logic, use a regular function (def) instead of a lambda function.

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



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! ๐Ÿš€

Popular Posts

Categories

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

Followers

Python Coding for Kids ( Free Demo for Everyone)