Class VIII - DS Worksheet-1
Class VIII - DS Worksheet-1
Class VIII - DS Worksheet-1
Part – 1
1. What is Python?
a) A high-level programming language
b) A low-level programming language
c) An assembly language
d) A markup language
a) 14 b) 11 c) 10 d) 21
x= 5
y= 2
result= x//y
print(result)
Answers:
1. A high-level programming language
2. 123_variable
3. 11
4. list
5. All of the above
6. 2
7. To perform a specific action based on a condition
8. An iterator that generates a sequence of numbers
Part – 2
a) World!
b) Hello, World!
c) World
d) ld!
x = 10
if x>5:
print(“x is greater than 5”)
else:
print(“x is less than or equal to 5”)
a) x is greater than 5
b) x is less than or equal to 5
c) x is equal to 5
d) x is greater than or equal to 10
Answers:
1. World!
2. # This is a comment
3. It executes a block of code if a condition is false.
4. To execute a block of code a specified number of times.
5. x is greater than 5
6. To end the execution of the loop and return to the beginning.
7. Converts a value to a string
Part – 3
2. In Python, what is the purpose of the semicolon (;) at the end of a statement?
a) It indicates the end of a code block.
b) It separates multiple statements on the same line.
c) It is used to define a function.
d) It is a syntax error.
Answers:
1. =
2. It separates multiple statements on the same line.
3. It separates elements in a tuple or list.
4. **
5. It is used to represent a comment.
6. It is used to define a block of code in structures like loops and functions.
7. Curly braces { }
PART – 4
Answers:
1. Floor division operator
2. Triple double quotes (""" """)
3. It represents the "not" operator in logical expressions.
Part – 5
1. Assertion: In Python, the += operator can be used to concatenate strings. Reasoning: The += operator is used
for in-place addition, which can be used to concatenate strings.
a) Both the assertion and reasoning are true, and the reasoning is a correct explanation of the assertion.
b) Both the assertion and reasoning are true, but the reasoning is not a correct explanation of the assertion.
c) The assertion is true, but the reasoning is false.
d) The assertion is false.
2. Assertion: A variable defined inside a function has local scope. Reasoning: Variables defined inside a
function are only accessible within that function.
a) Both the assertion and reasoning are true, and the reasoning is a correct explanation of the assertion.
b) Both the assertion and reasoning are true, but the reasoning is not a correct explanation of the assertion.
c) The assertion is true, but the reasoning is false.
d) The assertion is false.
3. Assertion: The range() function in Python returns a list of numbers. Reasoning: The range() function
generates a sequence of numbers but does not create a list.
a) Both the assertion and reasoning are true, and the reasoning is a correct explanation of the assertion.
b) Both the assertion and reasoning are true, but the reasoning is not a correct explanation of the assertion.
c) The assertion is true, but the reasoning is false.
d) The assertion is false.
4. Assertion: The break statement can only be used inside a loop in Python. Reasoning: The break statement
is used to exit a loop prematurely.
a) Both the assertion and reasoning are true, and the reasoning is a correct explanation of the assertion.
b) Both the assertion and reasoning are true, but the reasoning is not a correct explanation of the assertion.
c) The assertion is true, but the reasoning is false.
d) The assertion is false.
5. Assertion: A function in Python can have multiple return statements. Reasoning: The return statement is
used to exit a function and optionally return a value.
a) Both the assertion and reasoning are true, and the reasoning is a correct explanation of the assertion.
b) Both the assertion and reasoning are true, but the reasoning is not a correct explanation of the assertion.
c) The assertion is true, but the reasoning is false.
d) The assertion is false.
Answers:
1. Both the assertion and reasoning are true, and the reasoning is a correct explanation of the assertion.
2. Both the assertion and reasoning are true, and the reasoning is a correct explanation of the assertion.
3. Both the assertion and reasoning are true, but the reasoning is not a correct explanation of the assertion.
4. Both the assertion and reasoning are true, and the reasoning is a correct explanation of the assertion.
5. Both the assertion and reasoning are true, and the reasoning is a correct explanation of the assertion.
Part – 6
3. Assertion (A): The "elif" statement is used for handling multiple conditions in Python. Reasoning (R): It
allows you to specify an alternative condition to check if the preceding "if" statement's condition is False.
5. Assertion (A): The "break" statement can be used to exit a loop prematurely in Python. Reasoning (R): It
allows you to terminate the current loop and resume execution after the loop.
Answers:
1. What is Python?
2. Mention one advantage of using Python.
3. Name the two main types of Python modes.
4. How do you comment out a single line in Python?
5. What is a variable in Python?
6. Explain the purpose of indentation in Python.
7. What is a function in Python?
8. How is a Python module different from a Python package?
9. What is a reserved word in Python?
10. What is an operator in Python?
11. Explain the difference between == and = in Python.
12. What does the modulus operator (%) do?
13. Describe the purpose of the double asterisk (**) operator.
14. How does the += operator work in Python?
15. What is the difference between a float and an integer in Python?
16. How do you convert a string to an integer in Python?
17. Explain the concept of dynamic typing in Python.
18. What is a boolean type in Python?
19. How do you check the type of a variable in Python?
20. What is an if statement in Python?
21. How is an if-else statement different from an if statement?
22. What is a loop in Python?
23. Explain the use of a break statement in Python.
24. What is the purpose of a continue statement in a loop?
25. What is a pass statement used for in Python?
26. How do you print output in Python?
27. What is a docstring in Python?
28. Explain the purpose of the input() function.
29. How can you concatenate strings in Python?
30. What is a tuple in Python?
31. Describe the difference between a list and a tuple.
32. What is the purpose of the hash (#) symbol in Python code?
33. How do you create a multi-line string in Python?
34. What are the triple quotes (""" """ or ''' ''') used for?
35. Explain the difference between / and // operators.
36. What is the exponentiation operator in Python?
37. How does the not operator work in Python?
38. What is the purpose of the and and or operators in Python?
39. How do you check the length of a string in Python?
40. Explain the difference between a list and a set.
41. How do you check if a value is of a specific type in Python?
42. What is the purpose of the range() function in Python?
43. How do you define a function in Python?
Answers:
1. Python is a high-level programming language known for its simplicity and readability.
2. One advantage of using Python is its ease of learning and readability, which makes it accessible to
beginners and experienced developers alike.
3. The two main types of Python modes are the interactive mode and the script mode.
4. You can comment out a single line in Python by using the # symbol.
5. A variable in Python is a name given to a memory location to store data. It is used to represent values that
can be changed during the execution of a program.
6. Indentation in Python is used to define blocks of code. It is crucial for Python's syntax, as it replaces curly
braces or other block delimiters used in many other programming languages.
7. A function in Python is a block of reusable code that performs a specific task. It takes input, processes it,
and returns an output.
8. A Python module is a file containing Python definitions and statements. A Python package is a directory
containing multiple modules.
9. A reserved word in Python, also known as a keyword, is a word that is part of the Python language and has
a specific meaning and purpose. Examples include if, else, for, def, etc.
10. An operator in Python is a symbol or a keyword that performs operations on one or more operands to
produce a result.
11. == is a comparison operator used to check if two values are equal, while = is an assignment operator used
to assign a value to a variable.
12. The modulus operator (%) returns the remainder of a division operation.
13. The double asterisk (**) operator is used for exponentiation, raising a number to the power of another.
14. The += operator is used for in-place addition, which adds the right operand to the left operand and assigns
the result to the left operand.
15. A float is a data type in Python used to represent decimal numbers, while an integer represents whole
numbers.
16. To convert a string to an integer in Python, you can use the int() function.
17. Dynamic typing means that you don't need to specify the type of a variable explicitly; the type is
determined at runtime.
18. A boolean type in Python represents truth values, True or False.
19. The type() function can be used to check the type of a variable in Python.
20. An if statement in Python is a conditional statement that executes a block of code if a specified condition is
true.
21. An if-else statement includes an additional block of code to be executed if the initial condition is false.
22. A loop in Python is a control structure that allows a set of instructions to be executed repeatedly until a
specified condition is met.
23. The break statement is used to exit a loop prematurely.
24. The continue statement is used to skip the current iteration of a loop and move to the next iteration.
25. The pass statement is a null operation that does nothing. It is used as a placeholder when a statement is
syntactically required but no action is needed.
26. Output in Python can be printed using the print() function.
27. A docstring in Python is a special type of comment used to document functions, modules, or classes.
28. The input() function is used to take user input in Python.
29. Strings can be concatenated in Python using the + operator.
30. A tuple in Python is an ordered, immutable collection of elements.
31. A list is a collection of elements that can be of any type and can be changed after creation. A tuple is an
ordered collection of elements that cannot be changed after creation.
32. The hash (#) symbol is used to represent comments in Python code.
33. A multi-line string in Python can be created using triple quotes (''' ''' or """ """).
34. Triple quotes (''' ''' or """ """) are used to create multiline strings and also for docstrings.
35. The / operator performs standard division, while // performs floor division (rounding down to the nearest
whole number).
36. The exponentiation operator (**) raises a number to the power of another.
37. The not operator is a unary operator that negates the value of a boolean expression.
38. The and operator returns True if both operands are True, while the or operator returns True if at least
one operand is True.
39. The length of a string in Python can be checked using the len() function.
40. A list is an ordered collection of elements that can be of any type and can be changed after creation. A set
is an unordered collection of unique elements.
41. The isinstance() function can be used to check if a value is of a specific type in Python.
42. The range() function in Python is used to generate a sequence of numbers.
43. A function in Python is defined using the def keyword followed by the function name, parameters, and a
colon.
Write a program for calculating area of a circle in python.
Radius=5
Area=3.14*Radius**2
print("Area of a circle is ",Area)
Output:
Area of a circle is 78.5
Write a program for calculating area of a rectangle in python.
Length=5
Width=4
Area=Length*Width
print("Area of a rectangle is ",Area)
output:
Area of a rectangle is 20
Write a program to calculate average of first three numbers in python.
avg=(1+2+3)/3
print("Average of first 3 numbers is ",avg)
output:
Average of first 3 numbers is 2.0
Python slicing is about obtaining a substring from the given string by slicing it respectively from start to end.
Specify the start index and the end index, separated by a colon, to return a part of the string.
String indexing:
0 1 2 3 4 5 6 7 8 9 10 11 12
H e l l o , W o r l d !
Example
Get the characters from position 2 to position 5 (not included):
b = "Hello, World!"
print(b[2:5])
output: llo
Note: The first character has index 0.
Slice From the Start
By leaving out the start index, the range will start at the first character:
Example
b = "Hello, World!"
print(b[:5])
output: Hello
Slice To the End
By leaving out the end index, the range will go to the end:
Example
Get the characters from position 2, and all the way to the end:
b = "Hello, World!"
print(b[2:])
output: llo, World!
Negative Indexing
Use negative indexes to start the slice from the end of the string:
Example
b = "Hello, World!"
print(b[-5:-2])
output: orl
Some other examples:
Identify the errors , round up the mistakes and re write the program to get the output.
1.
FName=BPS
MName=&
LName=Jc
SchoolName=FName+MName+LName
print(SchoolName)
Output: BPS&Jc
Rewriting program
FName="BPS"
MName="&"
LName="Jc"
SchoolName=FName+MName+LName
print(SchoolName)
Output: BPS&Jc
2.
N1=50
Sum=5+6+N1
Print(sum)
Output: 61
Rewriting program
N1=50
Sum=5+6+N1
print(Sum)
Output: 61
3.
Theory_marks=int(input(“Enter your theory marks”)
Practical_marks=int(input(Enter your practical marks)
Marks=Theorymarks+practical_marks
print(marks)
output: Enter your theory marks: 45
Enter your practical marks : 45
90
Rewriting program
Theory_marks=int(input("Enter your theory marks"))
Practical_marks=int(input("Enter your practical marks"))
Marks=Theory_marks+Practical_marks
print(Marks)
4.
marks=56
if marks>50
print("B grade")
output: B grade
Rewriting program
marks=56
if marks>50:
print("B grade")
output: B grade
Python programming block and statements:
Python Statements :
A Python statement is an instruction that the Python interpreter can execute. There are different types
of statements in Python language as Assignment statements, Conditional statements, Looping statements,
etc.
if example:
if a>0:
print("a is positive number")
if-else example:
if a>0:
print("a is positive number")
else
print("a is negative number") Conditional statements
if-elif-else example:
if a>0:
print("a is positive number")
elif a<0:
print("a is negative number")
else
print(a is zero")
Python Indentation
Python indentation refers to adding white space before a statement to a particular block of code. In another
word, all the statements with the same space to the right, belong to the same code
Python uses indenting to mark a block of code. A block of code is a group of statement which will execute
together.
The indent must have the same amount of spaces or tabs. The following code illustrates what is code block.
From the code above we can see that the : indicates that the code block is about to begin.
In Java or C or C++ we use { to specify the block of code . But in python we use white spaces (indentation)
to specify a block of code.
Example:
i = 20
if (i < 15):
print("i is smaller than 15") ---Block of statements
print("i'm in if Block")
else:
print("i is greater than 15") ----Block of statements
print("i'm in else Block")
print("i'm not in if and not in else Block")
The white blank spaces before print statements is indentation.