0% found this document useful (0 votes)
3 views9 pages

Chapter 3

Chapter 3 introduces Python as a free and open-source programming language, detailing its limitations such as speed and memory consumption. It explains the differences between script mode and interactive mode, and provides algorithms and pseudocode for various programming tasks. Additionally, it highlights Python's features, advantages, and the concept of decomposition in programming.

Uploaded by

kainatsjs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Chapter 3

Chapter 3 introduces Python as a free and open-source programming language, detailing its limitations such as speed and memory consumption. It explains the differences between script mode and interactive mode, and provides algorithms and pseudocode for various programming tasks. Additionally, it highlights Python's features, advantages, and the concept of decomposition in programming.

Uploaded by

kainatsjs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Chapter 3:

Getting Started with Python

1. Python is free and open-source programming language. It means that anyone can download, use,
modifyand distribute it without any licensing fees or legal restrictions. In addition, Python’s source
code isavailable for anyone to access, modify and distribute as per their requirements.

2. Limitations of Python:

(a) Speed: Python is an interpreted language, which means that it is generally slower compared to

compiled languages like C or C++.

(b) Mobile Development: Python is not a very good language for mobile development. It is seen as a

weak language for mobile computing.

(c) Memory Consumption: Python is not a good choice for memory-intensive tasks. Due to the
flexibility of the data types, Python’s memory consumption is also high.

(d) Runtime Errors: Python programmers have cited several issues with the design of the language.

Because the language is dynamically typed, it requires more testing and has errors that only show

up at run-time.

3. The main differences between script mode and interactive mode:

(a) In script mode, Python code is executed from a script file with a “.py” extension. The code is saved

in a file and the entire script is executed sequentially from start to finish. On the other hand,

interactive mode allows you to execute Python statements and expressions directly in an interactive

shell without the need to save code in a separate file.

(b) In script mode, the input to the program is usually provided through predefined variables,

command-line arguments or reading from files. The output is typically written to the console or

saved to files. In interactive mode, you can interactively enter input at the prompt and immediately

see the output. Each line of code is executed and evaluated as soon as you press the Enter key.

(c) Script mode is commonly used for developing larger projects or reusable code that can be run

repeatedly. On the other hand, interactive mode is useful for experimenting, testing snippets of

code, exploring libraries or quickly evaluating expressions.

4. A flowchart and an algorithm are both tools used in computer programming, but they are different
in nature and serve different purposes. Here are the differences between flowchart and algorithm:

(a) A flowchart is a graphical representation of a process, using symbols, shapes and arrows to
illustrate the flow of information or steps involved in solving a problem. An algorithm, on the other
hand, is a step-by-step procedure for solving a problem or performing a task, which can be
implemented in code.
(b) A flowchart is a visual representation of a process, while an algorithm is a set of instructions that

describes how to perform a task or solve a problem.

(c) A flowchart provides a high-level view of a process, showing the steps involved in solving a
problem or performing a task. An algorithm provides a detailed step-by-step description of how to
perform each step.

(d) A flowchart is represented using symbols, while an algorithm is represented using a natural

language, pseudocode or programming language.

5. The pseudocode to read two numbers, divide one by another and display the quotient:

(a) Start

(b) Declare variables num1, num2, quotient

(c) Read num1

(d) Read num2

(e) If num2 is equal to 0, display “Error: Division by zero”

(f) Else, set quotient = num1 / num2

(g) Display “Quotient is: ”, quotient

(h) End

6. Algorithm to determine who takes the cake in the given scenario:

(a) Start

(b) Declare variables player1_wins and player2_wins, both initialized to 0.

(c) Loop 5 times:

(i) Read the result of the coin flip (1 for player 1 win, 2 for player 2 win).

(ii) If the result is 1, increment player1_wins. If it is 2, increment player2_wins.

(iii) If either player has won 3 flips, exit the loop.

(d) If player1_wins is equal to 3, display “Player 1 wins the cake!”

(e) If player2_wins is equal to 3, display “Player 2 wins the cake!”

(f) If neither player has won 3 flips, display “It’s a tie!”

(g) End

7. A pseudocode to print all multiples of 5 between 10 and 25 (including both 10 and 25):

(a) Start

(b) Set variable i = 10

(c) Loop while i <= 25:

(i) If i is a multiple of 5, print i


(ii) Increment i by 1

(d) End

8. A pseudocode that will perform the tasks mentioned:

(a) Start

Declare variables cs, math, phys, aggregate, percentage

Read marks for Computer Science from the user and store them in the cs

Read marks for Mathematics from the user and store them in the math

Read marks for Physics from the user and store them in phys

(b) Set aggregate = cs + math + phys

Display “Aggregate marks: ”, aggregate

(c) Set percentage = (aggregate / 300) * 100

Display “Percentage of marks: ”, percentage

End

9. The algorithm to find the greatest among two different numbers entered by the user:

(a) Start

(b) Declare variables num1 and num2 to store the two input numbers

(c) Read in num1 from the user

(d) Read in num2 from the user

(e) If num1 is greater than num2, display “num1 is the greatest”

(f) Otherwise, if num2 is greater than num1, display “num2 is the greatest”

(g) Otherwise, if num1 and num2 are equal, display “Both numbers are equal”

(h) End

10. The algorithm that performs the tasks mentioned:

(a) Start

(b) Declare variable num to store the user input

(c) Read the number from the user and store it in num

(d) If num is between 5 and 15, display “GREEN”

(e) Else, if num is between 15 and 25, display “BLUE”

(f) Else, if num is between 25 and 35, display “ORANGE”

(g) Else, display “ALL COLORS ARE BEAUTIFUL”

(h) End
11. Algorithm to accept four numbers as input and find the largest and smallest of them:

(a) Start

(b) Declare variables num1, num2, num3, num4, largest and smallest

(c) Read in num1 from the user

(d) Read in num2 from the user

(e) Read in num3 from the user

(f) Read in num4 from the user

(g) Set largest equal to num1

(h) Set smallest equal to num1

(i) If num2 is greater than largest, set largest equal to num2

(j) If num3 is greater than largest, set largest equal to num3

(k) If num4 is greater than largest, set largest equal to num4

(l) If num2 is less than the smallest, set the smallest equal to num2

(m) If num3 is less than the smallest, set the smallest equal to num3

(n) If num4 is less than the smallest, set the smallest equal to num4

(o) Display the largest and smallest numbers

(p) End

12. The concept of decomposition is based on breaking down complex systems or problems into
smaller, more manageable components. By doing so, we can understand each component in isolation
and tackle each one individually, leading to a simpler and more manageable solution overall.

13. Algorithm to find the factorial of an inputted number:

(a) Start

(b) Declare variables num and factorial

(c) Read in num from the user

(d) Set factorial equal to 1

(e) If num is 0 or 1, set factorial equal to 1

(f) If num is greater than 1, do the following:

(i) Set i equal to 1

(ii) Repeat the following steps until i is greater than num:

• Multiply factorial by i

• Increment i by 1

(g) Display the factorial


(h) End

14. Match the pairs:

15. Pseudocode to print the bill depending upon the price and quantity of an item:

(a) Start

(b) Declare variables price, quantity, total cost and GST

(c) Read the price of the item from the user and store it in the price variable

(d) Read the quantity of the item from the user and store it in the quantity variable

(e) Multiply the price by the quantity to calculate the total cost and store it in the total cost variable

(f) Display the total cost

(g) Calculate the GST by multiplying the total cost by 0.05 (5%) and store it in the GST variable

(h) Add the GST to the total cost to calculate the bill GST, and store it in a variable with the same
name

(i) Display the bill GST

(j) End

16. Python was first released on February 20, 1991, by its creator, Guido van Rossum. The initial
release was Python 0.9.0, which was a successor to the ABC language.

17. Python was developed by Guido van Rossum, a Dutch programmer, in the late 1980s and early
1990s. The design of Python was influenced by two programming languages: ABC and Modula-3. ABC
was a language designed for teaching programming concepts and it had an easy-to-learn syntax and
interactive shell. Modula-3, on the other hand, was a system programming language that had a
strong module system and support for low-level programming.

18. Yes, Python is a case-sensitive programming language, which means that it distinguishes between
uppercase and lowercase letters. For example, the variable name and Name are two different
variables in Python.

19. IDLE is a simple Integrated Development 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.
20. In Python, both displaying and printing are methods of outputting data to the console or
terminal, but there is a difference between them. Displaying: means showing data or output on the
console, but without actually printing it. In Python, this is often done by calling a function that
returns a value, such as assigning a value to a variable. For example, consider the following code:

x=5

x*2

In this code, the expression x * 2 is evaluated and its result, 10, is computed. However, this value is
not printed or displayed on the console.

Printing: on the other hand, means outputting data to the console so that it is visible to the user. In
Python, this is done using the print() function. For example:

x=5

print(x * 2)

In this code, the value of x * 2, which is 10, is printed to the console using the print() function.

21. Salient features of Python are:

(a) Simple and Easy: Python is a simple language that is easy to learn.

(b) Free/Open Source: Anybody can use Python without the need to purchase a license.

(c) High-level Language: Being a high-level language, it can be easily understood by the user without
the need to be concerned with low-level details.

(d) Portable: Python codes are machine and platform-independent.

(e) Extensible and Embeddable: Python programs support the usage of C/C++ codes.

(f) Standard Library: Python standard library contains pre-written tools for programming.

22. Cross-platform software is software that can run on multiple operating systems or platforms
without requiring modification. In the context of Python, it means that Python programs can be
written once and run on multiple platforms, such as Windows, Linux, and macOS, without making
any changes to the code.

23. Python offers the following advantages:

(a) Platform-independent: It is platform-independent and can run across different operating systems/
platforms like Windows, Linux/Unix, macOS and other operating systems.

(b) Readability: Python programs use clear, simple, concise and English-like instructions that are easy
to read and understand even by non-programmers or people with no substantial programming
background.

(c) Object-Oriented Programming Language: It is an interactive, interpreted and Object-oriented


programming language

(d) Higher Productivity: Since Python is a simple language with small codes and extensive libraries, it
offers higher productivity to programmers than languages like C++ and Java.
(e) Syntax Highlighting: It allows distinguishing between input, output and error messages by
different color codes.

24. There are two ways to work in Python:

(a) Interactive Mode: In this mode, we can execute Python code interactively, line by line and see the
results immediately. We can run Python in interactive mode by opening a Python interpreter, which
allows us to enter Python commands and see their output immediately. Interactive mode is useful
for testing and experimenting with small code snippets.

(b) Script Mode: In this mode, we can write a Python program in a script file, save it and then
execute it using the Python interpreter. We can create a Python script using a text editor or an
integrated development environment (IDE) such as IDLE. Script mode is useful for writing larger
programs or scripts that need to be executed multiple times.

25. Advantages of working in Interactive mode in Python:

(a) We can get immediate feedback for each line of code you enter, which allows for quick
experimentation and testing.

(b) It is easy to debug code in interactive mode, as you can quickly test different inputs and see the
results.

Disadvantages of working in Interactive mode in Python:

(a) Interactive mode is best suited for small to medium-sized projects and it can be tedious for larger
projects.

(b) Code written in interactive mode is not easily reusable in other projects or scripts.

26. (a) To display the sum of 3, 8.0, 6*12:

(i) Open Python in interactive mode.

(ii) Type “3 + 8.0 + 6*12” and press enter.

(iii) The sum, which is 77.0, will be displayed on the screen.

(b) To print the sum of 16, 5.0, 44.0:

(i) Open Python in interactive mode.

(ii) Type “print (16 + 5.0 + 44.0)” and press enter.

(iii) The sum, which is 65.0, will be printed on the screen.

27. (a) Open Python in interactive mode.

(b) Type the following commands and press enter after each line:

(i) print("Python is easy to learn and write.")

(ii) print("It allows us to work in two modes: Interactive mode and Script mode.")

(iii) print("Interactive mode is also known as Python Shell and Script mode is known as Python Editor.
It is a platform-independent language.")

(iv) print("We find it interesting to work with Python.")


(c) The output will be displayed on the screen.

(d) Type “python filename.py” and press enter, where the filename

Instructions in Script mode:

(a) Open a text editor such as Notepad.

(b) Type the following code and save the file with a .py extension:

(i) print("Python is easy to learn and write.")

(ii) print("It allows us to work in two modes: Interactive mode and Script mode.")

(iii) print("Interactive mode is also known as Python Shell and Script mode is known as Python Editor.
It is a platform-independent language.")

(iv) print("We find it interesting to work with Python.")

(c) Type “python filename.py” and press enter, where the filename is the name of the file we saved
in step 2.

(d) The output will be displayed on the screen.

28. Do it yourself

29. (a) print(n=17) – this code will produce an error.

(b) print(8+9) – the output will be 17.

(c) print(4.2, "hello", 6-2, "world", 15/2.0) – the output will be 4.2 hello 4 7.5

(d) print("123abc",sep='-') – the output will be 123abc

(e) print("XXX",end='!') – the output will be XXX!

30. (a) 46

(b) 100

31. Output:

• Python is easy to learn and write.

• 21.99113

• I am a class XI student

• I’m

• class XI student

• I’m 16 years old

You might also like