Getting Started

This page contains the CBSE Computer Science with Python class 11 Unit-3 chapter 1 Getting Started. You can find the solutions for chapter 1 of CBSE class 11 Computer Science with Python Exercise. So is the case if you are looking for CBSE class 11 Computer Science with Python related topic Getting Started questions and answers for the Exercise.
EXERCISE
Question 1
1. Create following Variables
i)
“mystring” to contain “hello”
ii)
“myfloat” to contain “2.5”
iii)
“myint” to contain “10”
Answer 1
The required variables can be created as follows:
mystring = "hello"
myfloat = 2.5
myint = 10
Here, mystring stores a string value, myfloat stores a floating point number, and myint stores an integer value. These statements are simple examples of variable assignment in Python, where a value is assigned to a variable name using the = operator.
Thus, each variable stores a different type of data according to the value assigned to it.
Question 2
2. Write the value justification
i)
2*(3+4)
ii)
2*3+4
iii)
2+3*4
Answer 2
The values are justified as follows:
i) 2*(3+4)
2*(3+4)
= 2*7
= 14
Here, the expression inside the brackets is evaluated first. After that, the result is multiplied by 2. Therefore, the final value is 14.
ii) 2*3+4
2*3+4
= 6+4
= 10
In this expression, multiplication is done before addition because multiplication has higher precedence. Therefore, the final value is 10.
iii) 2+3*4
2+3*4
= 2+12
= 14
Here also, multiplication is performed first. After finding 3*4 = 12, the result is added to 2. Therefore, the final value is 14.
Thus, these expressions show that the value of an expression depends on the order of precedence of operators. Brackets are evaluated first, and multiplication is done before addition.
Question 3
3. What is the type of the following result:
i)
1+2.0+3
Answer 3
i) 1 + 2.0 + 3
The type of this result is float.
This is because the expression contains 2.0, which is a floating point number. When an integer and a float are used in the same expression, Python converts the result into a float.
So,
1 + 2.0 + 3 = 6.0
Therefore, the value of the expression is 6.0, and its type is float.
Question 4
4. Which of the following is the valid variable name:
i)
global
ii)
99flag
iii)
sum
iv)
an$wer
Answer 4
Among the given options, the valid variable name is sum.
This is because sum follows the rules for naming variables in Python. A variable name should begin with a letter or underscore, should not start with a number, should not contain special symbols like $, and should not be a Python keyword.
The other options are invalid for the following reasons:
global – invalid because it is a keyword
99flag – invalid because it starts with a number
an$wer – invalid because it contains the special symbol $
Therefore, sum is the correct answer because it is written according to the rules of a valid identifier.
Question 5
5. True or False
i)
Character Data type values should be delimited by using the single quote.
ii)
None is one of the data type in python
iii)
The += operator is used to add the right hand side value to the left hand side variable.
iv)
The data type double is not a valid python data type.
v)
Python does not have any keywords
vi)
The equal to condition is written by using the == operator
Answer 5
i) Character data type values should be delimited by using the single quote.
Answer: False
Python does not have a separate character data type. A single character is treated as a string of length 1. Also, strings in Python can be written using single quotes, double quotes, or triple quotes, so it is not correct to say that they should be delimited only by single quotes.
ii) None is one of the data type in Python.
Answer: True
None belongs to the NoneType category in Python. It is used to represent the absence of a value or a null value. Therefore, this statement is true.
iii) The += operator is used to add the right hand side value to the left hand side variable.
Answer: True
The += operator is a compound assignment operator. It adds the value on the right side to the variable on the left side and stores the result back in the same variable. So, this statement is true.
iv) The data type double is not a valid Python data type.
Answer: True
Python does not use double as a separate data type name. For decimal values, Python uses the float data type. Therefore, double is not a valid standard Python data type.
v) Python does not have any keywords.
Answer: False
Python does have keywords. Keywords are reserved words that have special meaning in the language and cannot be used as ordinary identifiers. So, this statement is false.
vi) The equal to condition is written by using the == operator.
Answer: True
In Python, the comparison operator== is used to check whether two values are equal. It is different from the = operator, which is used for assignment. Therefore, this statement is true.
Thus, the answers are:
i) False
ii) True
iii) True
iv) True
v) False
vi) True
Question 6
6. Check all syntactically correct statements
a)
Which input statements are correct
i)
a = raw_input()
ii)
a = raw_input ("enter a number")
iii)
a = raw_imput (enter your name)
b)
Which print statements are correct?
i)
_print "9" + "9"
ii)
_print int("nine")
iii)
_print 9+9
iv)
print 9
c)
Which are correct arithmetical operations?
i)
a = 1*2
ii)
2 = 1+1
iii)
5 + 6 = y
iv)
Seven = 3 * 4 ✔
d)
Which are correct type conversions?
i)
int (7.0+0.1) ✔
ii)
str (1.2 * 3.4) ✔
iii)
float (“77″+”.0″) ✔
iv)
str ( 9 / 0 ) ✔
e)
Which operations result in 8?
i)
65 // 8 ✔
ii)
17 % 9 ✔
iii)
2 * * 4 ✔
iv)
64 * * 0.5
f)
Which lines are commented?
i)
“””This is a comment””” ✔
ii)
# This is a comment ✔
iii)
// this is a comment
iv)
” ” ” This is a comment” ” ” ✔
g)
Find the matching pairs of expressions and values.
i)
1023
boolean
ii)
None
int
iii)
[2, 4, 8, 16]
tuple
iv)
True
list
v)
17.54
str
vi)
(“Roger”, 1952)
NoneType
vii)
“my fat cat”
float
Answer 6
The syntactically correct statements are those which follow the proper rules of Python syntax. A statement may be syntactically correct even if it gives an error during execution. The given sub-parts can be answered as follows.
a) Which input statements are correct?
a = raw_input()
a = raw_input("enter a number")
These two statements are syntactically correct. In both cases, the function name is written correctly and the statement follows the proper format. In the second statement, the prompt message is also correctly written inside quotation marks.
The statement
a = raw_imput(enter your name)
is not syntactically correct because:
raw_imput is misspelled
the message enter your name is not enclosed in quotes
So, the correct statements are (i) and (ii).
b) Which print statements are correct?
From the given options, only
print 9
is syntactically correct.
The first three statements are not syntactically correct as written in the question because they use _print instead of print.
_print "9" + "9"
_print int("nine")
_print 9+9
So, the correct statement is (iv).
c) Which are correct arithmetical operations?
a = 1*2
Seven = 3 * 4
These two are syntactically correct because the value or expression is placed on the right side and the variable is on the left side of the assignment operator.
The following are not correct:
2 = 1+1
5 + 6 = y
These are incorrect because in Python the left side of = must be a variable name, not a number or an expression.
So, the correct statements are (i) and (iv).
d) Which are correct type conversions?
int(7.0+0.1)
str(1.2 * 3.4)
float("77"+".0")
str(9 / 0)
All four are written in a syntactically correct way because the function calls and brackets are properly used.
But str(9 / 0) will cause a runtime error because division by zero is not allowed. Even so, the statement itself is still syntactically correct.
So, the syntactically correct statements are (i), (ii), (iii), and (iv).
e) Which operations result in 8?
65 // 8 = 8
17 % 9 = 8
64 ** 0.5 = 8.0
These expressions give the value 8 (the last one gives 8.0, which is numerically equal to 8).
2 ** 4 = 16, so it is not correct for this question.
So, the correct expressions are (i), (ii), and (iv).
f) Which lines are commented?
The exercise continues with examples of commented lines. In Python, a line starting with # is treated as a comment, and triple quotes are also commonly used for comment-like descriptive text blocks in beginner-level usage. So the commented lines are:
"""This is a comment"""
# This is a comment
'''This is a comment'''
The line with // is not a valid Python comment style.
So, the commented lines are (i), (ii), and (iv).
g) Find the matching pairs of expressions and values.
The correct matching pairs are:
i)
1023
int
ii)
None
NoneType
iii)
[2, 4, 8, 16]
list
iv)
True
boolean
v)
17.54
float
vi)
("Roger", 1952)
tuple
vii
"my fat cat"
str
So, each expression is matched with its correct Python data type.
Thus, the correct answers are:
a) (i), (ii)
b) (i), (ii), (iii), (iv)
c) (i), (iv)
d) (i), (ii), (iii), (iv)
e) (i), (ii), (iv)
f) (i), (ii), (iv)
Question 7
7. MCQ
i)
The data type allows only True/False values
a)
bool ✔
b)
boolean
c)
Boolean
d)
None
ii)
If the value of a = 20 and b = 20, then a+=b will assign to a
a)
40 ✔
b)
30
c)
20
d)
10
iii)
The operator is used to find out if division of two number yields any remainder
a)
/
b)
+
c)
% ✔
d)
//
Answer 7
i) The data type allows only True/False values.
Answer: (a) bool
The bool data type is used to store only two values: True and False. These values are used in conditions and decision-making statements. Therefore, the correct answer is bool.
ii) If the value of a = 20 and b = 20, then a += b will assign to a.
Answer: (a) 40
The operator += adds the value on the right side to the variable on the left side and stores the result in the same variable.
a = 20
b = 20
a += b means a = a + b
a = 20 + 20
a = 40
Therefore, the value assigned to a is 40.
iii) The operator is used to find out if division of two numbers yields any remainder.
Answer: (c) %
The % operator is called the modulus operator. It is used to find the remainder after division. For example, 17 % 9 gives 8, which is the remainder. Therefore, the correct answer is %.
Thus, the correct answers are:
i) bool
ii) 40
iii) %
Question 8
8. When will following statement in interpreter result into error:
>>> B+4
Answer 8
>>> B + 4
This statement will result in an error when the variable B has not been defined earlier. In Python, a variable must be assigned a value before it is used in an expression. If B is not already stored in memory, the interpreter will not know what value to add to 4.
In such a case, Python will show a NameError because B is an undefined variable. Therefore, the statement B + 4 gives an error if B has not been declared or assigned any value before it is used.
Question 9
9. How can we change the value of 6*1-2 to -6 from 4?
Answer 9
The value of the expression 6*1-2 is 4 because the operations are performed according to the normal order of precedence.
First,
6*1 = 6
Then,
6-2 = 4
So, the result is 4.
To change the value to -6, the grouping of the expression must be changed by using parentheses:
6*(1-2)
Now the expression is evaluated as follows:
1-2 = -1
Then,
6*(-1) = -6
Thus, by using brackets (parentheses), the order of calculation changes, and the value becomes -6 instead of 4.
Question 10
10. Is python case sensitive?
Answer 10
Yes, Python is case sensitive. This means that uppercase and lowercase letters are treated as different in Python. For example, name, Name, and NAME are considered three different identifiers.
Because of this, the programmer must use the correct case while writing variable names, function names, and other identifiers. If the case is changed, Python treats it as a different name, which may lead to an error or a different result.
Thus, Python is case sensitive, and proper use of uppercase and lowercase letters is very important while writing programs.
Question 11
11. What does “immutable” mean; which data type in python are immutable.
Answer 11
Immutable means a value cannot be changed in place after it is created. If we try to change the value of an immutable variable, the old value is not modified in the same memory location. Instead, a new value is created and the variable is rebuilt with that new value.
The immutable data types in Python include:
Number data type (such as integer, float, complex)
Tuples
Sets
So, immutable data types are those whose values do not change in place once they are created.
Question 12
12. Name four of Python’s Basic data types? Why are they called so?
Answer 12
Four basic data types in Python are:
int
float
str
bool
These are called basic data types because they are the fundamental types used to store simple values in Python. They are used very commonly in programs for storing:
integers in int
decimal numbers in float
text or characters in str
True/False values in bool
They are called basic because they form the foundation for handling data in Python, and many other operations in programming begin with these simple types.
Question 13
13. What are relational operators? Explain with the help of examples.
Answer 13
Relational operators are those operators which are used to compare two values or expressions. They check the relationship between them and give the result in the form of True or False. These operators are mainly used in conditions and decision-making statements.
The common relational operators in Python are:
== : equal to
!= : not equal to
> : greater than
< : less than
>= : greater than or equal to
<= : less than or equal to
Examples:
5 > 3 gives True, because 5 is greater than 3.
10 == 10 gives True, because both values are equal.
7 < 4 gives False, because 7 is not less than 4.
8 != 6 gives True, because 8 and 6 are not equal.
Thus, relational operators are used to compare values and to test conditions in a program. Since they return only True or False, they are very useful in controlling the flow of execution.
Question 14
14. What is an integer?
Answer 14
An integer is a number without any fractional or decimal part. It represents whole numbers such as positive numbers, negative numbers, and zero. Examples of integers are 10, 0, -5, and 4298114.
In Python, integer values are stored using the int type. For example, if a variable is assigned a value like 10, its type is integer. Integers are used whenever a program needs to work with whole-number values.
Thus, an integer is a whole number value that does not contain a decimal point.
Question 15
15. What is a variable? What names may variable have?
Answer 15
A variable is a name used to store or refer to a value in a program. When a value is assigned by using the assignment operator (=), that value becomes associated with the variable. In simple words, a variable helps the programmer store data so that it can be used again whenever required in the program.
A variable name should be written according to the rules of naming identifiers. A variable name:
can be of any length
can contain letters (a-z, A-Z), digits (0-9), and underscore (_)
should begin with an alphabet or underscore
should not be a keyword
It is also a good practice to keep variable names meaningful and short, so that the program becomes easier to read and understand. Generally, variable names are written in lowercase letters.
Thus, a variable is used to hold a value, and its name should follow proper naming rules so that it is valid and easy to understand.
Question 16
16. How are keywords different from variable names?
Answer 16
Keywords are special words that are already reserved by Python. They are used by the Python interpreter to recognize the structure of a program and have a fixed meaning. Because of this, they cannot be used for any other purpose. Examples of keywords are if, for, while, print, and def.
Variable names, on the other hand, are names given by the programmer to store values in a program. They are user-defined names and can be chosen according to the need of the program. However, they must follow the rules of naming, such as beginning with a letter or underscore, using only letters, digits, and underscore, and not being a keyword.
Thus, the main difference is that keywords are predefined reserved words of Python, while variable names are created by the programmer to refer to data.
Question 17
17. Why are data types important?
Answer 17
Data types are important because they tell what kind of value is stored in a variable and what kind of operations can be performed on that value. A data type is a set of values and the allowable operations on those values. Because of this, the computer can understand how the data should be stored, handled, and used in a program.
For example, numbers are used for arithmetic operations, strings are used for text, and Boolean values are used for logical conditions. If the data type is known, Python can process the value correctly and apply the proper operations to it.
Thus, data types are important because they help in proper storage, correct processing, and meaningful use of data in a program.
Question 18
18. How can you convert a string to integer and when can it be used?
Answer 18
A string can be converted to an integer by using the int() function. This is a type conversion method in Python in which a numeric string is changed into integer form.
For example:
x = int("25")
In this case, the string "25" is converted into the integer 25.
It is commonly used when the input is taken through raw_input(), because raw_input() accepts the entered value as a string. If that input is meant to be used as a number, it should be converted into an integer before performing arithmetic operations.
For example:
y = int(raw_input("enter your roll no"))
If the user enters 5, the accepted string "5" is converted into the integer 5 before being assigned to y.
Thus, string-to-integer conversion is useful when numeric data is entered in string form and needs to be used for calculation, comparison, or other numeric operations.
Question 19
19. How can text be read from the keyboard?
Answer 19
Text can be read from the keyboard by using the raw_input() function. This function accepts the data entered by the user from the keyboard and stores it as a string. Therefore, it is commonly used when the program needs to read text such as a name, place, or any other character data.
A prompt message can also be given inside the brackets so that the user knows what to enter. For example:
name = raw_input("Enter your name")
In this case, the text entered by the user is accepted from the keyboard and stored in the variable name as a string.
Thus, text is read from the keyboard in Python by using raw_input(), because it takes the entered value in string form.
Question 20
20. How are comments written in a program?
Answer 20
Comments are written in a program to add notes or explanations in the code. They help the programmer understand what a particular statement or section of the program is doing. As a program becomes longer, comments make it easier to read and understand.
In Python, a comment is written by using the # symbol. Anything written after # on the same line is ignored by the interpreter, so it does not affect the execution of the program.
A comment can be written in two common ways:
On a separate line
At the end of a line of code
Example:
# Calculating area of a square
area = side ** 2 # formula for area
Thus, comments are written by using the # symbol, and they are useful for making the program clearer and easier to maintain.
Lab Exercise
Question 1 (Lab)
1. Record what happens when following statements are executed:
a)
print n=7
b)
print 5+7
c)
print 5.2, “this”, 4-2, “that”, 5/2.0
Answer 1 (Lab)
This question asks to observe what happens when each statement is entered in Python. Some statements execute successfully and produce output, while others produce an error if the syntax is not correct.
a) print n=7
This statement gives a syntax error. In Python, a value cannot be assigned to a variable in this form inside the print statement. Assignment and printing are different operations, so this statement does not follow the correct syntax.
Result:SyntaxError
b) print 5+7
This statement executes correctly. The expression 5+7 is first evaluated, and then the result is printed on the screen.
Output:
12
c) print 5.2, "this", 4-2, "that", 5/2.0
This statement also executes correctly. Here, Python prints multiple values in the same line. Before printing, the expressions are evaluated:
4-2 becomes 2
5/2.0 becomes 2.5
Then all the values are displayed together in the same output line.
Output:
5.2 this 2 that 2.5
Thus, this question shows that correct Python statements produce output, while incorrect statements produce an error.
Question 2 (Lab)
2. Use IDLE to calculate:
a)
6+4*10
b)
(6+4)*10
Answer 2 (Lab)
This question is based on evaluating arithmetic expressions in IDLE and observing how Python follows the order of precedence of operators. In such calculations, brackets are given first priority, and if there are no brackets, multiplication is done before addition.
a) 6 + 4 * 10
In this expression, multiplication is performed before addition.
First, 4 * 10 = 40
Then, 6 + 40 = 46
So, the result is:
Answer:46
b) (6 + 4) * 10
In this expression, the part inside the brackets is evaluated first.
First, 6 + 4 = 10
Then, 10 * 10 = 100
So, the result is:
Answer:100
This question shows that the use of brackets can change the result of an expression. Without brackets, multiplication is done first, but with brackets, the addition inside the brackets is done first. Therefore, although the same numbers are used in both expressions, the final answers are different.
Question 3 (Lab)
3. Type following mathematical expression and record your observations:
a)
2**500
b)
1/0
Answer 3 (Lab)
This question asks to enter the given expressions in Python and observe what happens. One expression produces a very large numerical result, while the other causes an error during execution.
a) 2**500
In this expression, ** is the exponentiation operator, which means “raise to the power.” So, 2**500 means 2 raised to the power 500. Since the power is very large, the result is also a very large integer.
Observation: Python calculates and displays a very large number successfully.
This shows that Python can handle very large integer values.
b) 1/0
This expression causes an error because division by zero is not allowed in mathematics as well as in Python. When Python tries to divide 1 by 0, it cannot perform the operation.
Observation: The statement produces an error during execution.
Result:ZeroDivisionError
Thus, this question shows two different observations: Python can calculate very large values correctly, but it gives an error when an invalid operation such as division by zero is attempted.
Question 4 (Lab)
4. What will be the output of the following code:
a = 3 – 4 + 10
b = 5 * 6
c = 7.0/8.0
print “These are the values:”, a, b, c
Answer 4 (Lab)
In this question, the values of the variables are calculated first, and then all of them are printed together using the print statement. Since multiple items are separated by commas in the print statement, they are displayed in the same line.
Given code:
a = 3 - 4 + 10
b = 5 * 6
c = 7.0 / 8.0
print "These are the values:", a, b, c
First, let us find the value of each variable:
a
= 3 - 4 + 10
= -1 + 10
= 9
b
= 5 * 6
= 30
c
= 7.0 / 8.0
= 0.875
After calculating these values, the print statement displays the message and the values of a, b, and c in one line.
Output:
These are the values: 9 30 0.875
Thus, the program first performs the arithmetic operations, stores the results in variables, and then prints all the values together in a clear format.
Question 5 (Lab)
5. Write a code to show the use of all 6 math function.
Answer 5 (Lab)
This question asks for a simple program to show the use of six mathematical functions in Python. A suitable answer is to use some commonly used built-in math functions and display their results one by one. This helps in understanding what each function does.
The following program shows the use of six functions: abs(), pow(), round(), max(), min(), and divmod().
# Program to show the use of six math functions

print abs(-15)          # gives absolute value
print pow(2, 5)         # raises 2 to the power 5
print round(5.67)       # rounds the number
print max(10, 25, 7)    # gives the largest value
print min(10, 25, 7)    # gives the smallest value
print divmod(17, 5)     # gives quotient and remainder
Sample Output
15
32
6.0
25
7
(3, 2)
Explanation
abs(-15) returns the positive value of -15, so the result is 15.
pow(2, 5) means 2 raised to the power 5, so the result is 32.
round(5.67) rounds the number to the nearest whole number, so the result is 6.0.
max(10, 25, 7) returns the greatest value, which is 25.
min(10, 25, 7) returns the smallest value, which is 7.
divmod(17, 5) returns both the quotient and remainder, so the result is (3, 2).
Thus, this program clearly shows how different mathematical functions are used in Python and what type of result each function gives.
Question 6 (Lab)
6. Write a code that prints your full name and your Birthday as separate strings.
Answer 6 (Lab)
This question asks for a simple program that prints two separate strings: one for the full name and one for the birthday. Since each print statement displays one line, the name and birthday can be shown clearly on separate lines.
A simple program is:
# Program to print full name and birthday

print "Virat Kohli"
print "Birthday: 15 August 2000"
Sample Output
Virat Kohli
Birthday: 15 August 2000
Explanation
The first print statement displays the full name.
The second print statement displays the birthday as a separate string.
Since both values are written inside quotation marks, they are treated as string values.
Thus, this program is a simple example of printing separate strings in Python.
Question 7 (Lab)
7. Write a program that asks two people for their names; stores the names in variables called name1 and name2; says hello to both of them.
Answer 7 (Lab)
This program should take the names of two people as input from the keyboard, store them in the variables name1 and name2, and then display a greeting for both. The question itself clearly asks to use the variable names name1 and name2.
To read text from the keyboard, raw_input() is used. It accepts the input as a string, which is suitable here because names are text values.
Program
# Program to ask two names and greet both persons

name1 = raw_input("Enter first name: ")
name2 = raw_input("Enter second name: ")

print "Hello", name1
print "Hello", name2
Sample Output
Enter first name: Rahul
Enter second name: Priya
Hello Rahul
Hello Priya
Explanation
The first raw_input() statement takes the first person’s name and stores it in name1.
The second raw_input() statement takes the second person’s name and stores it in name2.
The print statements then display a greeting message for each person separately.
Thus, this program shows how to accept string input, store it in variables, and display output in a simple and clear way.
Question 8 (Lab)
8. Calculate root of the following equation:
a)
34x 2 + 68x – 510
b)
2x 2 – x -3 = 0
Answer 8 (Lab)
The roots of a quadratic equation can be calculated in Python by using the formula:
{x = \dfrac{-b ± \sqrt{b^2 - 4ac}}{2a}}
A simple approach is to:
assign values to a, b, and c
calculate the discriminant
then calculate both roots
print the result
The square root can be found by using ** 0.5, which is within the scope of this chapter because exponentiation is already used in the chapter exercises and lab exercises.
(a) 34x² + 68x - 510 = 0
Here:
a = 34
b = 68
c = -510
Program
# Program to calculate roots of 34x² + 68x - 510 = 0

a = 34.0
b = 68.0
c = -510.0

d = b * b - 4 * a * c

x1 = (-b + d ** 0.5) / (2 * a)
x2 = (-b - d ** 0.5) / (2 * a)

print "Root 1 =", x1
print "Root 2 =", x2
Sample Output
Root 1 = 3.0
Root 2 = -5.0
Explanation
First, the value of the discriminant d is calculated.
Then the two roots are calculated by using the quadratic formula.
Finally, both roots are printed.
So, the equation has the roots:
3.0
-5.0
(b) 2x² - x - 3 = 0
Here:
a = 2
b = -1
c = -3
Program
# Program to calculate roots of 2x² - x - 3 = 0

a = 2.0
b = -1.0
c = -3.0

d = b * b - 4 * a * c

x1 = (-b + d ** 0.5) / (2 * a)
x2 = (-b - d ** 0.5) / (2 * a)

print "Root 1 =", x1
print "Root 2 =", x2
Sample Output
Root 1 = 1.5
Root 2 = -1.0
Explanation
The same method is used here.
The discriminant is calculated first.
Then both roots are found and displayed.
So, the equation has the roots:
1.5
-1.0