From bd10d129dc70f184bdbd793b209bb7eba56cbb57 Mon Sep 17 00:00:00 2001 From: Palanivelme15 <137321775+Palanivelme15@users.noreply.github.com> Date: Wed, 21 Jun 2023 23:02:19 +0530 Subject: [PATCH] Assignment-2 --- Assignment-2 | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Assignment-2 diff --git a/Assignment-2 b/Assignment-2 new file mode 100644 index 0000000..7d6d056 --- /dev/null +++ b/Assignment-2 @@ -0,0 +1,101 @@ +1.What are the two values of the Boolean data type? How do you write them? +Answer :- +True - It is considered to be a positive sign or 1 to do some operations. We can write it like True +False- It is considered to be a negative sign or 0 to do some operations. We can write it like False + +2. What are the three different types of Boolean operators? +Answer :- AND, OR, 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 evaluate ). +AND :- +A B Result +FALSE TRUE FALSE +FALSE FALSE FALSE +TRUE TRUE TRUE +TRUE FALSE FALSE + +OR :- +A B Result +FALSE TRUE TRUE +FALSE FALSE FALSE +TRUE TRUE TRUE +TRUE FALSE TRUE + +NOT :- +A Result +TRUE FALSE +FALSE TRUE + +4. What are the values of the following expressions? +(5 > 4) and (3 == 5) -> Ans :- False +not (5 > 4) -> Ans :- False +(5 > 4) or (3 == 5) -> Ans :- True +not ((5 > 4) or (3 == 5)) Ans :- False +(True and True) and (True == False) -> Ans :- False +(not False) or (not True) -> Ans :- True + +5. What are the six comparison operators? +Answer :- >, <, ==, !=, >=, <= + +6. How do you tell the difference between the equal to and assignment operators?Describe a condition and when you would use one. +== → this is an equality operator when you want to check equality between two numbers then we can use this operator. e.g, 7 == 7 -> True , 5 == 8 -> False += → this is an assignment operator which will assign the value to some variable. e.g, var_a = 6 +Here var_a is a variable which is holding a value of 6 in it. + +7. Identify the three blocks in this code: +spam = 0 +if spam == 10: +print('eggs') +if spam > 5: +print('bacon') +else: +print('ham') +print('spam') +print('spam') + +Answer :- Two If blocks and one else block + + + + + + +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 :- +if spam == 1: + print(“Hello”) +elif spam == 2: + print(“Howdy”) +else: + print(“Greetings”) + + +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 - If break runs then it will not allow the program to run further and it will break the loop. +continue - It will skip the underneath program whenever the continue statement runs. It will exclude or ignore the remaining flow and continue from the first line of the program. + +11. In a for loop, what is the difference between range(10), range(0, 10), and range(0, 10, 1)? +Answer :- range(10) - it will start with 0 and go till last number minus one which is 9. Even though we haven’t mentioned the starting point, python will automatically take 0 as default starting point. +range(0, 10) - It will start with 0 and go till the last number minus one which is 9. We have mentioned the starting point as 0 explicitly. +range(0, 10, 1) - It will start with 0 and go till the last number minus one which is 9. We have mentioned the starting point as 0 explicitly and also we have mentioned the step value(third value in the range) which will tell us step value of how much we need to go from first number to next number likewise(difference of numbers). + + + +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. +Answer :- +for loop - +for i in range(1, 11): + print(i) +while loop - +i = 1 +while i <= 10: + print(i) + i += 1 + + +13. If you had a function named bacon() inside a module named spam, how would you call it after importing spam? +Answer :- spam.bacon() +