Nothing Special   »   [go: up one dir, main page]

Class VIII - DS Worksheet-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

WORKSHEET ON PYTHON

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

2. Which of the following is NOT a valid variable name in Python?


a) my_variable
b) 123_variable
c) _my_variable
d) myVariable

3. What will the following code output?


print(3 + 4 * 2)

a) 14 b) 11 c) 10 d) 21

4. Which of the following data types is mutable in Python?


a) int b) float c) list d) tuple

5. What does the len() function do in Python?


a) Returns the length of a string
b) Returns the length of a list
c) Returns the length of a tuple
d) All of the above

6. What will be the output of the following code?

x= 5
y= 2
result= x//y
print(result)

a) 2.5 b) 2.0 c) 2 d) 2.2

7. What is the purpose of the if statement in Python?


a) To define a function
b) To perform a specific action based on a condition
c) To create a loop
d) To print text on the screen

8. What does the range() function return in Python?


a) A list of numbers
b) A tuple of numbers
c) An iterator that generates a sequence of numbers
d) A dictionary of numbers

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

1. What is the output of the following code?


my_string = “Hello, World!”
print(my_string[7:])

a) World!
b) Hello, World!
c) World
d) ld!

2. Which of the following is a correct way to comment a single line in Python?


a) // This is a comment
b) # This is a comment
c) /* This is a comment */
d) <!-- This is a comment -->

3. What does the else statement do in Python?


a) It executes a block of code if a condition is true.
b) It executes a block of code if a condition is false.
c) It defines a function.
d) It creates a loop.

4. What is the purpose of the for loop in Python?

a) To execute a block of code a specified number of times.


b) To execute a block of code while a condition is true.
c) To execute a block of code based on a condition.
d) To define a function.

5. What is the result of the following code?

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

6. What is the purpose of the break statement in a loop?


a) To end the execution of the loop and return to the beginning.
b) To skip the current iteration and continue with the next.
c) To execute a specific block of code.
d) To create a new loop.

7. What does the str() function do in Python?


a) Converts a value to a string
b) Returns the length of a string
c) Checks if a value is a string
d) Splits a string into a list

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

1. Which of the following is a valid Python assignment operator?


a) :=
b) =
c) ->
d) <-

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.

3. What is the role of a comma (,) in Python code?


a) It is used to concatenate strings.
b) It separates elements in a tuple or list.
c) It is a decimal point in numeric literals.
d) It is an arithmetic operator.

4. Which symbol is used for the exponentiation operator in Python?


a) ^
b) **
c) ^
d) %

5. What is the purpose of the hash (#) symbol in Python code?


a) It is used to represent a comment.
b) It indicates the start of a function.
c) It is an operator for power.
d) It is used for string interpolation.

6. What is the role of a colon (:) in Python programming?


a) It indicates the end of a statement.
b) It is used to define a block of code in structures like loops and functions.
c) It is a syntax error.
d) It separates function arguments.

7. Which punctuation marks are used to define a block of code in Python?


a) Curly braces { }
b) Parentheses ( )
c) Square brackets [ ]
d) Angle brackets < >

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

1. In Python, what does the double forward slash (//) represent?


a) Integer division operator
b) Exponentiation operator
c) Bitwise AND operator
d) Floor division operator

2. Which punctuation mark is used to define a docstring in Python?


a) Single quotes (' ')
b) Double quotes (" ")
c) Triple single quotes (''' ''')
d) Triple double quotes (""" """)

3. What is the role of the exclamation mark (!) in Python code?


a) It represents the "not" operator in logical expressions.
b) It is used for string interpolation.
c) It is used to access attributes of an object.
d) It indicates a comment.

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

1. Assertion (A): Python is an interpreted language.


Reasoning (R): Python code is executed line by line by the Python interpreter, without the need for
compilation.

a) Both A and R are true, and R is the correct explanation for A.


b) Both A and R are true, but R is NOT the correct explanation for A.
c) A is true, but R is false.
d) Both A and R are false.

2. Assertion (A): Python uses dynamic typing.


Reasoning (R): In Python, you don't need to explicitly declare the data type of a variable; it is determined at
runtime.

a) Both A and R are true, and R is the correct explanation for A.


b) Both A and R are true, but R is NOT the correct explanation for A.
c) A is true, but R is false.
d) Both A and R are false.

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.

a) Both A and R are true, and R is the correct explanation for A.


b) Both A and R are true, but R is NOT the correct explanation for A.
c) A is true, but R is false.
d) Both A and R are false.

4. Assertion (A): Python is an object-oriented programming (OOP) language.


Reasoning (R): It supports concepts such as classes, objects, and inheritance for structuring code.

a) Both A and R are true, and R is the correct explanation for A.


b) Both A and R are true, but R is NOT the correct explanation for A.
c) A is true, but R is false.
d) Both A and R are 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.

a) Both A and R are true, and R is the correct explanation for A.


b) Both A and R are true, but R is NOT the correct explanation for A.
c) A is true, but R is false.
d) Both A and R are false.

Answers:

1. Both A and R are true, and R is the correct explanation for A.


2. Both A and R are true, and R is the correct explanation for A.
3. Both A and R are true, and R is the correct explanation for A.
4. Both A and R are true, and R is the correct explanation for A.
5. Both A and R are true, and R is the correct explanation for A.

Short answer type:

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

String slicing in python

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 !

-13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

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

Get the characters from the start to position 5 (not included):

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

Get the characters:

From: "o" in "World!" (position -5)

To, but not included: "d" in "World!" (position -2):

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.

#example python statements-------commenting statements


a=10
a,b=10,20 Assignment statements.
a=b=30

b=int(input("Enter a number"))-------------Input statement.


print(b)-----------------------output statement.

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")

while and for are looping statements or iterative statements.

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

Indent to create block statement

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.

this is a line of code


this is another line of code
this is the third line of code:
this is a line of code inside the block
another line in the block
third line in the code block
went out of the 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.

You might also like