Computer science Conditionals

4.1 Boolean
  1. What is a Boolean?
    Answer: A value which is true or false; it is a variable type.

  2. What does type(True) return?
    Answer: bool (indicating Boolean type).

  3. What is str(True) used for?
    Answer: To convert a Boolean to a true value type (string).

  4. What is an if statement?
    Answer: Runs a program on specific conditions.

  5. What is the syntax of an if statement?
    Answer:

    python

    Copy code

    if (condition): # code block else: # code block

  6. What should I know about the if branch?
    Answer:

    • Prints something if the condition is met.

    • Must evaluate to a Boolean (True or False).

  7. What should I know about the elif branch?
    Answer:

    • It has a condition.

    • Only runs if the first if condition is not met.

  8. What should I know about the else branch?
    Answer:

    • Executes the program if the condition in if and elif is not met.

    • No specific condition is required.

4.2 if Statements & 4.3 Comparison Operators
  1. What are the comparison operators in Python?
    Answer:

    • == : equal to

    • != : not equal to

    • < : less than

    • > : greater than

    • <= : less than or equal to

    • >= : greater than or equal to

  2. How do you check if more than one condition is true?
    Answer: Use and or or.

  3. What is the order of the alphabet when comparing strings?
    Answer:

    • Symbols/numbers

    • Uppercase letters

    • Lowercase letters

  4. How do you compare strings in Python?
    Answer:

    • == : equal to

    • != : not equal to

    • < : earlier in dictionary order

    • > : later in dictionary order

    4.4 Logical Operators
    1. Given x = 5, what is the result of x < 10?
      Answer: True

    2. What are the logical operators in Python (according to priority, with description)?
      Answer:

      • not : Changes a true value to false (negates the value).

      • and : True if both conditions are true.

      • or : True if at least one condition is true.

    3. What is a truth table?
      Answer:

      • Shows values of Boolean expressions given x and y.

      • Used to analyze logic in expressions.

    4. What is a Venn Diagram used for in logical operators?
      Answer:

      • Visually shows logical operators.

    Refer to additional resources for more information on:

    • Order of operators

    • De Morgan's Law

    • Short circuits

    4.5 Floating Point Numbers and Rounding
    1. Why do we use the round command?
      Answer: Because Python sometimes rounds very weirdly; it’s used to round a number to a specific decimal value.

    2. What does round(value, decimals) do?
      Answer: Rounds a number to a given number of decimal places.

    3. What is float in Python?
      Answer: Changes an element to a float type (decimal).

    4. How do you generate a random number?
      Answer:

      • Use randint inside of a random function.

      • Imports the random library (provides random options).

    5. What is a package in Python?
      Answer: Organizes related modules into a hierarchical structure.