Range Check:
# Example 1: Checking if a number is within a specific range
user_input = int(input("Enter a number between 1 and 100: "))
if 1 <= user_input <= 100:
print("Valid input.")
else:
print("Input out of range.")
# Example 2: Validate if a number is within a specific range.
num = int(input("Enter a number: "))
if 10 <= num <= 50:
print("Valid range")
else:
print("Out of range")
# Example 3: Validate if a user's age is between 18 and 60.
age = int(input("Enter your age: "))
if 18 <= age <= 60:
print("Age is valid.")
else:
print("Age is not valid.")
# Example 4:
def range_check(number, min_val, max_val):
if min_val <= number <= max_val:
return True
else:
return False
# Example usage:
num = 25
if range_check(num, 10, 50):
print("Number is within the range.")
else:
print("Number is outside the range.")
Length Check:
# Example 1: Checking the length of a string
user_input = input("Enter a username (between 5 and 10 characters): ")
if 5 <= len(user_input) <= 10:
print("Valid username.")
else:
print("Invalid username length.")
# Example 2: Validate the length of a string.
username = input("Enter a username: ")
if len(username) >= 5 and len(username) <= 10:
print("Valid length")
else:
print("Invalid length")
# Example 3: Validate if a credit card number has valid digits.
card_number = input("Enter a credit card number: ")
if card_number.isdigit() and len(card_number) == 16:
print("Valid credit card number")
else:
print("Invalid credit card number")
# Example 4: Validate if a username has between 6 and 12 characters.
username = input("Enter username: ")
if 6 <= len(username) <= 12:
print("Username length is valid.")
else:
print("Username length is not valid.")
# Example 5:
def length_check(text, min_length, max_length):
if min_length <= len(text) <= max_length:
return True
else:
return False
# Example usage:
input_text = "Hello, World!"
if length_check(input_text, 5, 15):
print("Text length is valid.")
else:
print("Text length is invalid.")
Presence Check:
# Example 1: Checking if a string is not empty
user_input = input("Enter your name: ")
if user_input:
print(f"Hello, {user_input}!")
else:
print("Name is required.")
# Example 2: Validate if a user has entered a value.
value = input("Enter a value: ")
if value:
print("Value is present")
else:
print("Value is not present")
# Example 3: Validate if an email address is provided.
email = input("Enter your email: ")
if email:
print("Email is provided.")
else:
print("Email is not provided.")
# Example 4:
def presence_check(text):
if text:
return True
else:
return False
# Example usage:
input_text = ""
if presence_check(input_text):
print("Text is present.")
else:
print("Text is absent.")
Type Check:
# Example 1: Checking if the input is an integer
user_input = input("Enter an integer: ")
if user_input.isdigit():
user_input = int(user_input)
print(f"Valid integer: {user_input}")
else:
print("Invalid input. Please enter an integer.")
# Example 2: Validate if the input is an integer.
value = input("Enter a number: ")
if [Link]():
print("Valid integer")
else:
print("Invalid integer")
# Example 3: Validate if a user input is an integer.
value = input("Enter a number: ")
if [Link]():
print("It's an integer.")
else:
print("It's not an integer.")
# Example 4:
def type_check(value, data_type):
if isinstance(value, data_type):
return True
else:
return False
# Example usage:
num = 42
if type_check(num, int):
print("Value is an integer.")
else:
print("Value is not an integer.")
Format Check:
# Example 1: Checking if an email address has a valid format
import re
email = input("Enter an email address: ")
if [Link](r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", email):
print("Valid email address.")
else:
print("Invalid email address format.")
# Example 2: Validate if a phone number follows the format (XXX) XXX-XXXX.
phone = input("Enter your phone number: ")
import re
if [Link](r'^\(\d{3}\) \d{3}-\d{4}$', phone):
print("Phone number format is valid.")
else:
print("Phone number format is not valid.")
# Example 3:
import re
def format_check(email):
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
if [Link](pattern, email):
return True
else:
return False
# Example usage:
email_address = "example@[Link]"
if format_check(email_address):
print("Email format is valid.")
else:
print("Email format is invalid.")
Check Digits (e.g., ISBN Check):
# Example: Checking the validity of an ISBN-13
def is_valid_isbn(isbn):
if len(isbn) != 13:
return False
if not [Link]():
return False
total = 0
for i in range(12):
total += int(isbn[i]) * (1 if i % 2 == 0 else 3)
check_digit = (10 - (total % 10)) % 10
return int(isbn[-1]) == check_digit
user_input = input("Enter an ISBN-13: ")
if is_valid_isbn(user_input):
print("Valid ISBN-13.")
else:
print("Invalid ISBN-13.")