Python Programs: Logical and String Operators
Part 1: Logical Operators
1. Check if a number is between 10 and 100 using logical AND operator.
num = 55
if num > 10 and num < 100:
print("The number is between 10 and 100.")
else:
print("The number is NOT between 10 and 100.")
Output: The number is between 10 and 100.
2. Check if a person is eligible to vote using AND operator.
age = 18
has_id = True
if age >= 18 and has_id:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Output: You are eligible to vote.
3. Check if at least one subject is passed using OR operator.
math = 40
science = 35
if math >= 35 or science >= 35:
print("You passed at least one subject.")
else:
print("You failed both subjects.")
Output: You passed at least one subject.
4. Use NOT operator to check if it is not raining.
is_raining = False
if not is_raining:
print("You can go outside.")
else:
print("Stay indoors.")
Output: You can go outside.
Part 2: String Operators
1. Concatenate two strings using + operator.
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print("Full name:", full_name)
Output: Full name: John Doe
2. Repeat a string multiple times using * operator.
word = "Hi! "
print(word * 3)
Output: Hi! Hi! Hi!
3. Check if two strings are equal.
password1 = "abc123"
password2 = "abc123"
if password1 == password2:
print("Passwords match.")
else:
print("Passwords do not match.")
Output: Passwords match.
4. Check if a character is in a string.
text = "Python Programming"
if "P" in text:
print("The letter P is in the text.")
else:
print("The letter P is not in the text.")
Output: The letter P is in the text.