Common Boolean Operators (OCR GCSE Computer Science)

Revision Note

Flashcards
Robert Hampton

Expertise

Computer Science Content Creator

Common Boolean Operators

  • To demonstrate the use of common Boolean operators, three sample programs written in Python are given below
  • Comments have been included to help understand how the Boolean operators are being used
    • Common Boolean operators #1 - a simple program that assigns Boolean values to two variables and outputs basic comparisons
    • Common Boolean operators #2 - a simple program to output a grade based on a users score
    • Common Boolean operators #3 - a simple program reads a text files and searches for an inputted score
Python code

# -----------------------------------------------------------
# Common Boolean operators #1
# -----------------------------------------------------------
# Assign a Boolean value to a and b
a = True
b = False

# print the result of a and b
print("a and b:", a and b)
# print the result of a or b
print("a or b:", a or b)
# print the result of not a
print("not a:", not a)

# -----------------------------------------------------------
# Common Boolean operators #2
# -----------------------------------------------------------

# Take input for the score from the user
score = int(input("Enter the score: "))

# Compare the score and output the corresponding grade
if score >= 90 and score <= 100:
  print("Grade: A")
elif score >= 80 and score < 90:
  print("Grade: B")
elif score >= 70 and score < 80:
  print("Grade: C")
elif score < 70:
  print("Fail")

# -----------------------------------------------------------
# Common Boolean operators #3
# -----------------------------------------------------------
# Open the file for reading
file = open("scores.txt","r")
# Set flags to false
end_of_file = False
found = False
score = input("Enter a score: ")
# While it's not the end of the file and the score has not been found
while not end_of_file and not found:
  # read the line
  scores = file.readline().strip()
  # if the line equals the score
  if score == str(scores):
    found = True
    print("Score found")
  # if the line is empty
  if scores == "":
    end_of_file = True
    print("Score not found")
file.close()

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.