Loading...
Please wait while we prepare your content
Please wait while we prepare your content
Solutions for Computer Science, Class 11, CBSE
Assertion (A): Python is a cross-platform language.
Reasoning (R): Python code can run on a variety of platforms which makes programs very portable as any program written in one platform can easily be used on another.
Both A and R are true and R is the correct explanation of A.
Explanation
Python is a cross-platform language, meaning it is platform-independent and can run across different operating systems such as Windows, Linux/Unix, macOS, and others. Python's ability to execute on various platforms enhances the portability of its programs, allowing the same code to be used across different environments without modification.
Assertion (A): Python comes with its own IDLE.
Reasoning (R): IDLE is Python's Integrated Development and Learning Environment which allows programmers to easily write, modify and execute Python programs.
Both A and R are true and R is the correct explanation of A.
Explanation
Python comes with its own IDLE, which is included with Python installations. IDLE is a simple Integrated Development and Learning Environment; it is a program that allows the user to edit, run, browse and debug a Python program from a single interface.
Assertion (A): The interactive mode of Python gives instant result of the typed statement.
Reasoning (R): Script mode is an interactive window where Python code can be executed in the same window.
A is true but R is false.
Explanation
The interactive mode of Python gives instant results for typed statements. The Python Shell is an interactive window where you can type in Python code and see the output immediately in the same window. It serves as an interface between Python commands and the operating system.
Assertion (A): Python uses an interpreter to convert source code to object code.
Reasoning (R): Python interpreter scans and translates the whole program into machine code in one go.
A is true but R is false.
Explanation
Python uses an interpreter to convert source code into object code. The Python interpreter does not scan and translate the whole program into machine code in one go. Instead, it interprets the code line by line, converting each line into machine code and executing it immediately.
Assertion (A): To print the value of a variable, Python uses print() method.
Reasoning (R): print() method displays the content on the system screen or console.
Both A and R are true and R is the correct explanation of A.
Explanation
The print()
function is a built-in function in Python that displays the content on the system screen or console. To print the value of a variable, Python uses the print()
function.
Assertion (A): The shortcut key to run a Python program from script mode is F1.
Reasoning (R): Python programs/scripts are stored in files with .py extension.
A is false but R is true.
Explanation
The shortcut key to run a Python program from script mode is F5. Python programs/scripts are stored in files with .py or .pyw extension.
Assertion (A): Python is the fastest language.
Reasoning (R): Python is an interpreted language.
A is false but R is true.
Explanation
Python is not considered the fastest programming language as it is slower than many compiled languages such as C or C++ because it is interpreted rather than compiled. Python is an interpreted language, which means that Python code is executed line by line at runtime by an interpreter, without the need for a separate compilation step to machine code.
Assertion (A): Python is a case-sensitive language.
Reasoning (R): Python is easy to understand as it has a clearly defined syntax and simple structure.
Both A and R are true but R is not the correct explanation of A.
Explanation
Python is a case-sensitive language. This means that Python differentiates between capital and small alphabets. Python programs use clear, simple, concise instructions and clearly defined syntax and simple structure that are easy to read and understand even by non-programmers.
Free and Open source
Reason — Python is both free and open-source, meaning it is available for free and its source code can be viewed, modified, and distributed by anyone.
print("Hello")
Reason — In Python, when using the print()
function, strings must be enclosed in matching pairs of either single (') or double quotes ("). Therefore, the correct statement is print("Hello")
.
Interpreted
Reason — Python is an interpreted language. This means that Python code is executed line by line at runtime by an interpreter, without the need for a separate compilation step to machine code.
Which of the following codes is correct?
1.
print("Programming is fun")
print("Python")
print("Computer Science")
2.
print ("Programming is fun)
print("Python")
print("Computer Science)
3.
Print("Programming is fun")
print("Python")
print("Computer Science")
4.
Print("Programming is fun")
Print("Python")
Print("Computer Science")
print("Programming is fun")
print("Python")
print("Computer Science")
Reason — In Python, the print()
function is written in lowercase (print) and strings must be enclosed in matching pairs of either single (') or double quotes ("). Therefore, the correct code would be :
print("Programming is fun")
print("Python")
print("Computer Science")
Python treats capital and small letters as different
Reason — Python is a case-sensitive language. This means that Python differentiates between capital and small alphabets.
Some limitations of Python programming language are:
In script mode, we write a Python program in a file and execute it to display the output and in interactive mode, we use the Python prompt '>>>' to start typing commands and see immediate output.
Flow chart | Algorithm |
---|---|
A flow chart is defined as a visual representation of the sequence of steps and decisions needed to perform a process/task. | A finite sequence of steps required to get the desired output is called an algorithm. |
In the flowchart, symbols/shapes are used. | In the algorithm, plain text is used. |
Step 1: INPUT num1
Step 2: INPUT num2
Step 3: IF num2 == 0 THEN
Step 4: PRINT "Error: Division by zero is not allowed."
ELSE
Step 5: COMPUTE quotient = num1 / num2
Step 6: PRINT quotient
Two friends decide who gets the last slice of a cake by flipping a coin five times. The first person to win three flips wins the cake. An input of 1 means player 1 wins a flip, and a 2 means player 2 wins a flip. Design an algorithm to determine who takes the cake?
Step 1: Initialize Counters:
SET p1 = 0
SET p2 = 0
Step 2: Process Coin Flips:
FOR each flip from 1 to 5
INPUT result
IF result is 1 THEN
INCREMENT p1
ELSE IF result is 2 THEN
INCREMENT p2
IF p1 is 3 THEN
PRINT "Player 1 wins the cake."
EXIT the algorithm
ELSE IF p2 is 3 THEN
PRINT "Player 2 wins the cake."
EXIT the algorithm
Step 1: Input the marks in Computer Science
Step 2: Input the marks in Mathematics
Step 3: Input the marks in Physics
Step 4: Set total marks by adding marks in Computer Science, Mathematics and Physics
Step 6: Set percentage by dividing total marks by 300 and then multiply it by 100
Step 7: Display total marks and percentage
Step 1:
INPUT number1
INPUT number2
Step 2:
IF number1 > number2 THEN
PRINT "The greatest number is: " + number1
ELSE
PRINT "The greatest number is: " + number2
Step 3: END
Write an algorithm that performs the following:
Ask a user to enter a number.
If the number is between 5 and 15, write the word GREEN.
If the number is between 15 and 25, write the word BLUE.
If the number is between 25 and 35, write the word ORANGE.
If it is any other number, write that ALL COLORS ARE BEAUTIFUL.
Step 1: INPUT number
Step 2:
IF number is between 5 and 15 THEN
PRINT "GREEN"
ELSE IF number is between 15 and 25 THEN
PRINT "BLUE"
ELSE IF number is between 25 and 35 THEN
PRINT "ORANGE"
ELSE
PRINT "ALL COLORS ARE BEAUTIFUL"
Step 1: INPUT num1, num2, num3, num4
Step 2: SET largest = num1
Step 3: SET smallest = num1
Step 4: IF num2 > largest THEN
Step 5: SET largest = num2
ELSE IF num2 < smallest THEN
Step 6: SET smallest = num2
Step 7: IF num3 > largest THEN
Step 8: SET largest = num3
ELSE IF num3 < smallest THEN
Step 9: SET smallest = num3
Step 10: IF num4 > largest THEN
Step 11: SET largest = num4
ELSE IF num4 < smallest THEN
Step 12: SET smallest = num4
Step 13: PRINT "The largest number is: " + largest
Step 14: PRINT "The smallest number is: " + smallest
Decomposition is the process of breaking down a complex problem or system into smaller, easily manageable parts. These smaller problems are solved one after the other until the bigger complex problem is resolved. This stage involves breaking down the problem into smaller components so that they can be tackled more easily. The more we can break down a problem, the easier it is to solve.
Step 1: Start
Step 2: Input the number "n" from the user.
Step 3: Initialize a variable "fact" to 1.
Step 4: If n is 0, then the factorial is 1 and go to Step 7.
Step 5: If "n" is greater than 0, multiply "fact" by each integer from 1 to "n" in sequence.
Step 6: The final value of "fact" is the result.
Step 7: Display "fact".
Step 8: End
Step 1: INPUT price
Step 2: INPUT quantity
Step 3: COMPUTE total_bill = price * quantity
Step 4: COMPUTE gst_amount = total_bill * 0.05
Step 5: COMPUTE bill_with_gst = total_bill + gst_amount
Step 6: PRINT "Total Bill: ₹" + total_bill
Step 7: PRINT "Bill with GST: ₹" + bill_with_gst
IDLE is a simple Integrated Development and Learning Environment that comes with Python. The most important feature of IDLE is that it is a program that allows the user to edit, run, browse and debug a Python program from a single interface.
Printing in Python refers to using the print()
function to output information to the console. The print()
function takes one or more arguments, converts each argument to a string (if necessary), and displays them as text. On the other hand, displaying can be a more general term that encompasses presenting information or output in various forms, not limited to just text output in the console.
The salient features of Python are as follows:
Advantages of Python programming language are:
Interactive mode is useful for testing code. We can type the commands one by one and get the result of error immediately for each command. Disadvantages of Interactive mode are that it does not save commands in form of a program and also output is sandwiched between commands.
Write the given instructions in Interactive and Script modes to get the following result:
Python is easy to learn and write.
It allows us to work in two modes: Interactive mode and Script mode.
Interactive mode is also known as Python Shell and Script mode is known as Python Editor. It is a platform-independent language.
We find it interesting to work with Python.
Interactive Modes
>>> print("Python is easy to learn and write.")
>>> print("It allows us to work in two modes: Interactive mode and Script mode.")
>>> print("Interactive mode is also known as Python Shell and Script mode is known as Python Editor. It is a platform-independent language.")
>>> print("We find it interesting to work with Python.")
Script Modes
print("Python is easy to learn and write.")
print("It allows us to work in two modes: Interactive mode and Script mode.")
print("Interactive mode is also known as Python Shell and Script mode is known as Python Editor. It is a platform-independent language.")
print("We find it interesting to work with Python.")
Python is easy to learn and write.
It allows us to work in two modes: Interactive mode and Script mode.
Interactive mode is also known as Python Shell and Script mode is known as Python Editor. It is a platform-independent language.
We find it interesting to work with Python.
(a) print(n=17) — It raises an error because the syntax print(n=17) is incorrect. In this context, n=17 is trying to use an argument name assignment within the print() function, which is not valid.
(b) print(8 + 9) — 17
(c) print(4.2, "hello", 6 - 2, "world", 15/2.0) — 4.2 hello 4 world 7.5
(d) print("123abc", sep = '-') — 123abc
(e) print("XXX", end ='!') — XXX!
Try the following code on Python Shell and evaluate the output generated:
>>> print("Python is easy to learn and write.")
>>> print(3.14159*7)
>>> print('I am a class XI' + 'student')
>>> print('I', 'm')
>>> print("class XI student")
>>> print("I'm", 16, "years old")
Python is easy to learn and write.
21.99113
I am a class XIstudent
I m
class XI student
I'm 16 years old