PWP Question Bank Class test 1[1]
PWP Question Bank Class test 1[1]
4) Explain decision making statements If- else, if- elif- else with example.
Ans. The if-else statement: if statements executes when the conditions following if is true and it
does nothing when the condition is false. The if-else statement takes care of a true as well as
false condition.
Syntax-1: If condition: Or
Statement(s) Syntax-2: If condition:
else: If_Block
Statement(s) else:
else_Block
Example: i=20
if(i<15):
print(" less than 15")
else:
print("greater than 15")
output:
greater than 15
if-elif-else (ladder) statements: Here, a user can decide among multiple options. The if
statements are executed from the top down. As soon as one of the conditions controlling the if is
true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If
none of the conditions is true, then the final else statement will be executed.
Syntax: if (condition-1):
statement
elif (condition-2):
statements
.
.
elif(condition-n):
statements
else:
statements
Example:
i = 20
if (i == 10):
print ("i is 10")
elif (i == 15):
print ("i is 15")
elif (i == 20):
print ("i is 20")
else:
print ("i is not present")
output: i is 20
7)Write in brief about Set in Python. Write operations with suitable examples.
Answer: A Set is an unordered collection data type that is iterable, mutable and has no duplicate
elements. Python’s set class represents the mathematical notion of a set. The major advantage of
using a set, as opposed to a list, is that it has a highly optimized method for checking whether a
specific element is contained in the set. This is based on a data structure known as a hash table.
If Multiple values are present at the same index position, then the value is appended to that index
position, to form a Linked List. In, Python Sets are implemented using dictionary with dummy
variables, where key beings the members set with greater optimizations to the time complexity.
Set = set(["a", "b", "c"])
print("Set: ")
print(Set)
# Adding element to the set
Set.add("d")
print("\nSet after adding: ")
print(Set)
Output:
Set:
set(['a', 'c', 'b'])
Set after adding:
set(['a', 'c', 'b', 'd'])
16)Write a python program to calculate sum of digit of given number using function.
17)List building blocks of python.
Ans.
Character set: All characters that python can recognize. The below table illustrates the Python
character set along with examples.
Tokens: Tokens in python are building blocks of the Python programming language. The role
letters and words play for the English language, Similar to role token play for a python
programming language.
Python has the following tokens:
1)keywords
2)identifiers
3)literals : a)String literals b)Numeric literals c)Boolean Literals d)Special literal None
Tokens, Example
Keywords: Words that are already defined False, True, if, elif, else, for, while, pass,
and convey a special meaning to the language continue, lambda return, finally, import, def
compiler/interpreter
Identifiers: names given to different parts of def square, num=20, a_lst=[1,2,3]; here
program like variables, functions, object, square, num and a_lst are identifiers.
class, names given to different datatypes.
Literals/Constants: Data items that have fixed String: ‘Mayank‘,’abc‘,’anish‘; Numeric:
values 1,1.2,4,-3.95; Boolean: True,False
Special literal None; meaning nothing