0% found this document useful (0 votes)
14 views

Assignment 2

The document discusses Boolean operators and comparison operators in Python. It provides explanations of True and False Boolean values, the AND, OR, and NOT Boolean operators, and comparison operators like ==, !=, <, >, <=, and >=. It also gives examples of Boolean expressions and their evaluated values. Code samples are provided to demonstrate if/else conditional blocks and for/while loops.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Assignment 2

The document discusses Boolean operators and comparison operators in Python. It provides explanations of True and False Boolean values, the AND, OR, and NOT Boolean operators, and comparison operators like ==, !=, <, >, <=, and >=. It also gives examples of Boolean expressions and their evaluated values. Code samples are provided to demonstrate if/else conditional blocks and for/while loops.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.What are the two values of the Boolean data type? How do you write them?

Answer: Python Boolean type is one of the built-in data types provided by Python, which
represents one of the two values i.e., True or False. Generally, it is used to represent the
truth values of the expressions. For example, 1==1 is True whereas 2<1 is False.

2. What are the three different types of Boolean operators?

Answer: The three basic Boolean operators are: AND, OR, and NOT.

3. Make a list of each Boolean operator's truth tables (i.e., every possible combination of Boolean
values for the operator and what it evaluates).

Answer:

A B A and B A or B Not A

False False False False True

False True False True True

True False False True False

True True True True False

4. What are the values of the following expressions?

Answer:

(5 > 4) and (3 == 5): False

not (5 > 4): False

(5 > 4) or (3 == 5): True

not ((5 > 4) or (3 == 5)): False

(True and True) and (True == False): False

(not False) or (not True): True

5. What are the six comparison operators?

Answer:

A. Equal operator (==) – We first start with Equal comparison operator. In simple terms, if value on
the Left hand side is equal to Right hand side then, it would return with True. Otherwise it would be
False.

B. Not equal operator (!=) – It is used to check if two values are equal or not.
C. Less than (<) – If value on the left hand side is less than right hand side one. Then, it would result
in True. To compare string values, it checks for first letter and if the first letter is same. Then, it
compares subsequent letters in the word.

D. Greater than (>) – If the value on left hand side is greater than the right hand side then, it results
in True. Otherwise, it returns with False.

E. Greater than or equal to (>=) – If the left hand side is greater than or equal to right hand side
value. It would result in True. Notice that, with Greater than (>) – it was just one condition we had to
check. But, with Greater than or equal to – we need to check for two conditions.

F. Less than or equal to (<=) – If the left hand side is less than or equal to right hand side value. It
would result in True. Again, we check two conditions – less than (<) and equal to (=).

6. How do you tell the difference between the equal to and assignment operators? Describe a
condition and when you would use one.

7. Identify the three blocks in this code:

spam = 0 (Value Assignment Block)


if spam == 10: (If Condition Block)
print('eggs')
if spam > 5:
print('bacon')
else: (Else Condition Block)
print('ham')
print('spam')
print('spam')

8. Write code that prints Hello if 1 is stored in spam, prints Howdy if 2 is stored in spam, and prints
Greetings! if anything else is stored in spam.
Answer:

9.If your programme is stuck in an endless loop, what keys you’ll press?

Answer: Ctrl+C
10. How can you tell the difference between break and continue?

Answer:

Break: The break statement is used to terminate the loop or statement in which it is present. After
that, the control will pass to the statements that are present after the break statement, if
available. If the break statement is present in the nested loop, then it terminates only those loops
which contains break statement.

Continue: Continue is also a loop control statement just like the break
statement. continue statement is opposite to that of break statement, instead of terminating the
loop, it forces to execute the next iteration of the loop.
As the name suggests the continue statement forces the loop to continue or execute the next
iteration. When the continue statement is executed in the loop, the code inside the loop following
the continue statement will be skipped and the next iteration of the loop will begin.

11. In a for loop, what is the difference between range(10), range(0, 10), and range(0, 10, 1)?

Answer: No difference. Range will take value for 0 to 9

12. Write a short program that prints the numbers 1 to 10 using a for loop. Then write an
equivalent program that prints the numbers 1 to 10 using a while loop.

13. If you had a function named bacon() inside a module named spam, how would you call it after
importing spam?

Answer: This function can be called with spam. bacon()

You might also like