Unit 1
Q.1 Write Python Features.
Simple: Python is a simple and small language Reading a program written in Python feels
almost like reading English. This is in fact the greatest strength of Python which allows
programmers to concentrate on the solution to the problem rather than the language itself.
Easy to Learn: A Python program is clearly defined and easily readable. The structure of
the program is very simple. It uses few keywords and a clearly defined syntax. This makes
it easy for just anyone to pick up the language quickly
Versatile: Python supports development of a wide range of applications ranging from
simple text processing to WWW browsers to games
Free and Open Source: Python is an example of an open source software. Therefore,
anyone can freely distribute it, read the source code, edit it, and even use the code to write
new (free) programs
High-level Language: When writing programs in Python, the programmers don't have to
worry about the low-level details like managing memory used by the program, etc. They
just need to concentrate on writing solutions of the current problem at hand.
Interactive: Programs in Python work in interactive mode which allows interactive testing
and debugging of pieces of code. Programmers can easily interact with the interpreter
directly at the Python prompt to write their programs.
Dynamic: Python execute dynamically Programs written in Python can be copied and used
for development of application If There is any error, it is reported at run-time to allow
interactive program development
Extensible: Since Python is an open source software, anyone can add low-level modules
to the interpreter. These modules enable programmers to add to or customize their tools to
work more efficiently. Moreover, if you want a piece of code not to be accessible for
everyone, then you can even code that your program in C or C++ and then use them from
your Python program.
Extensive Libraries Python has a huge library that is easily portable across different
platforms. Library functions allows programmers to perform Wide range of applications
Easy Maintenance: Code written in Python is easy to maintain
Secure: The Python language environment is secure from altering data. Apart from this,
additional security checks can be easily added to implement additional security features
2
Robust: Python programmers cannot manipulate memory directly. Morcover errors are
raised as an exception. that can be caught and handled by the program code. For every
syntactical mistake, a simple interpret message is displayed. All these things makes the
language robust.
Multi-threaded: Python supports multi-threading, that is executing more than one process
of a program simultaneously. It also allows programmers to perform process management
tasks
Garbage Collection: The Python runtime environment handles garbage collection of all
Python objects. Objects which are currently not in use are deleted.
Q.2 What is Modularization? Explain Top-Down Approach.
Algorithms are used to manipulate the data for a given problem. For a complex problem, its
algorithm is often divided into smaller units called modules. This process of dividing an algorithm
into modules is called modularization.
The key advantages of modularization are as follows:
● It makes the complex algorithm simpler to design and implement.
● Each module can be designed independently.
● While designing one module, the details of other modules can be ignored, thereby
enhancing clarity in design which in turn simplifies implementation, [Link],
documenting, and maintenance of the overall algorithm.
There are two main approaches
● top-down approach and bottom-up approach, as Bottom-up approach.
Top-down approach :
● Each module can be divided into one or more sub-modules
● These modules can further be decomposed into one or more sub-modules, and this process
of decomposition is iterated until the desired level of module complexity is achieved.
● Top-down design method is a form of stepwise refinement where we begin with the
topmost module and incrementally add modules that it calls. Therefore, in a top-down
approach, we start from an abstract design and then at each step, this design is refined into
more concrete levels until a level is reached that requires no further refinement.
3
Example:
Q.3 Note on Flowchart.
Flowcharts are the graphical representation of the algorithms.
Using the algorithms and flowcharts the programmers can find out the bugs in the programming
logic and then can go for coding Flowcharts can show errors in the logic and set of data can be
easily tested using flowcharts.
Symbols used in Flowchart
↑↓
Flow lines are used to indicate the flow of data. The arrow heads
are important for flowlines The flowlines are also used to connect
the different blocks in the flowchart
These are termination symbols. The start of the flowchart is
represented by the name of the module in the ellipse and the end
of the flowchart is represented by the keywords End or Stop or
Exit
The rectangle indicates the processing. It includes processing. It
includes calculations, opening/ closing file.
Parallelogram indicates input and output
The diamond indicates the decision. It has one entrance and two
exits. One exit indicates the true and other indicates the false.
4
◯
The on-page connector connects the two different sections on the
same page. A letter is written inside the circle. The off-page
connector connects the two different sections on the different
pages.
Q.4 What is Indentation?
In number of programming languages such as C, C++, Java curly braces { } for the purpose
of defining a block of code. In Python for this purpose indentation is used.
A block of code (body of function, loop etc.) begins with indentation and ends with the
first un-indented line.
Usually four whitespaces are used for indentation purposes and most of the times preferred
over tabs.
Ex. no=10
if(no==10)
print(“No is 10”)
Q.5 Short note on operator
1. Arithmetic operators: Arithmetic operators are used to perform mathematical operations like
addition, subtraction, multiplication and division.
OPERATOR DESCRIPTION SYNTAX
+ Addition: adds two operands x+y
- Subtraction: subtracts two operands x–y
* Multiplication: multiplies two operands x*y
/ Division (float): divides the first operand by the second x/y
// Division (floor): divides the first operand by the second x // y
Modulus: returns the remainder when first operand is
% divided by the second x%y
[Link] Operators: Relational operators compares the values. It either returns True or
False according to the condition.
5
OPERATOR DESCRIPTION SYNTAX
> Greater than: True if left operand is greater than the right x>y
< Less than: True if left operand is less than the right x<y
== Equal to: True if both operands are equal x == y
!= Not equal to - True if operands are not equal x != y
Greater than or equal to: True if left operand is greater than
>= or equal to the right x >= y
Less than or equal to: True if left operand is less than or
<= equal to the right x <= y
[Link] operators: Logical operators perform Logical AND, Logical OR and Logical NOT
operations.
OPERATOR DESCRIPTION SYNTAX
and Logical AND: True if both the operands are true x and y
or Logical OR: True if either of the operands is true x or y
Not Logical NOT: True if operand is false not x
4. Bitwise operators: Bitwise operators acts on bits and performs bit by bit
operation.
OPERATOR DESCRIPTION SYNTAX
& Bitwise AND
x&y
Bitwise OR
| x|y
~ Bitwise NOT ~x
6
^ Bitwise XOR x^y
>> Bitwise right shift x>>
<< Bitwise left shift x<<
[Link] operators: Assignment operators are used to assign values to the variables.
OPERATOR DESCRIPTION SYNTAX
Assign value of right side of expression to left side
= operand x=y+z
Add AND: Add right side operand with left side
+= operand and then assign to left operand a+=b a=a+b
Subtract AND: Subtract right operand from left
-= operand and then assign to left operand a-=b a=a-b
Multiply AND: Multiply right operand with left
*= operand and then assign to left operand a*=b a=a*b
Divide AND: Divide left operand with right operand
/= and then assign to left operand a/=b a=a/b
Modulus AND: Takes modulus using left and right
%= operands and assign result to left operand a%=b a=a%b
Divide(floor) AND: Divide left operand with right
operand and then assign the value(floor) to left
//= operand a//=b a=a//b
Exponent AND: Calculate exponent(raise power) a**=b
**= value using operands and assign value to left operand a=a**b
Performs Bitwise AND on operands and assign value
&= to left operand a&=b a=a&b
Performs Bitwise OR on operands and assign value
|= to left operand a|=b a=a|b
7
Performs Bitwise xOR on operands and assign value
^= to left operand a^=b a=a^b
Performs Bitwise right shift on operands and assign a>>=b
>>= value to left operand a=a>>b
Performs Bitwise left shift on operands and assign a <<= b
<<= value to left operand a= a << b
6. Special operators: There are some special type of operators like-
○ Identity operators-
is and is not are the identity operators both are used to check if two values are
located on the same part of the memory. Two variables that are equal does not
imply that they are identical.
○ is True if the operands are identical
is not True if the operands are not identical
Q. 6 Explain Data Types.
Data types are used to define type of variable.
1. Numeric:
Integer: It holds signed integers Ex. 222
Long: It holdLong integers Ex. 123456789L
Float: Holds floating point precision number. Ex. 23.21
Complex: Holds complex number. Ex: 5+6j
2. String: String is a collection of characters. In python,
we can use single quote, double quote or triple quote to define a string.
+ operator is used for string concatenation and * operator is used for repetition
str=”AISSMS”.
3. List: It holds different types of data in list, enclosed in square brackets [ ].
Items separated with commas.
List is mutable, i.e. we can add, delete or updates list elements.
list1=[10, 20, ’aaa’, ’bbb’, 33.43, 22.32]
4. Tuple: It holds different types of data in list, enclosed in parenthesis ( ).
Items separated with commas.
Tuple is immutable. i.e. we cannot add, delete or updates tuple elements
Tuple is Read-only.
Ex. Tup1= (10, 20, ’aaa’, ’bbb’, 33.43, 22.32)
5. Dictionary: It is a collection of elements in the form of key-value pair. Elements
are enclosed in curly brackets.
Ex. dict1={ 1:”Red”, 2: “Blue”, 3:”Green”, 4: “White”, 5:”Black” }
8
Q.7 What is Identifier? naming conventions of identifiers
Identifier is a collection of alphanumeric characters. Identifiers are used to give name to the
programming elements like variable , arrays, functions, classes etc.
Rules:
1. Each variable starts with an alphabet or underscore and then followed by any number of
alphabets, digits or underscore.
2. Other than underscore no special symbol is allowed to form the variable name.
3. Variable shouldn't start with digit.
4. In a program variable name must be unique.
5. Variables are case sensitive so the area, AREA and Area considered as different
variables.
6. A variable should not contain any comma or blank space.
7. Variable name should not same as the keywords otherwise compiler will generate an
error
8. The length of a variable varies from language to language. But it is a good practice to
keep the variable name short and specific
Ex. Correct identifier: sum, area_of_circle, number1
In Correct identifier: case, total_of_a&b,1number
Q.8 Explain Problem Solving Steps.
1. Identify the problem:
The first step toward solving a problem is to identify the problem.
need to make sure you identify the problem before you start solving it. If you don’t know
what the problem is, you cannot solve it.
2. Understand the problem.
You must understand what is involved in the problem before you can continue
toward the solution. This includes understanding the knowledge base of the person or
machine for whom you are solving the problem. Detailed set of instructions to tell someone
how to find a restaurant in your city if he has a limited knowledge of the city than if he
knows it well.
3. Identify alternative ways to solve the problem.
This list should be as complete as possible. You might want to talk to other people
to find other solutions than those you have identified. Alternative solutions must be
acceptable ones.
4. Select the best way to solve the problem from the list of alternative solutions:-
9
In this step, you need to identify and evaluate the pros and cons of each possible
solution before selecting the best one. In order to do this, you need to select criteria for the
evaluation. These criteria will serve as the guidelines for evaluating each solution.
5. List instructions that enable you to solve the problem using the selected
Solution:-
These numbered, step-by-step instructions must fall within the knowledge
base set up in step 2. No instruction can be used unless the individual or the machine can
understand it. This can be very limiting, especially when working with computers.
6. Evaluate the solution:-
To evaluate or test a solution means to check its result to see if it is correct, and to see if it
satisfies the needs of the person(s) with the problem. If the result is either incorrect or
unsatisfactory, then the problem solver must review the list of instructions to see that they
are correct or start the process all over again.
Take the problem of what to do this evening.
1. Identify the problem. How do the individuals wish to spend the evening?
2. Understand the problem. With this simple problem, also, the knowledge base of
the participants must be considered. The only solutions that should be selected are
ones that everyone involved would know how to do. You probably would not select
as a possible solution playing a game of chess if the participants did not know how
to play.
3. Identify alternatives.
a. Watch television.
b. Invite friends over.
c. Play video games.
d. Go to the movies.
e. Play miniature golf.
f. Go to the amusement park.
g. Go to a friend’s party.
The list is complete only when you can think of no more alternatives.
4. Select the best way to solve the problem.
a. Weed out alternatives that are not acceptable, such as those that cost too
much money or do not interest one of the individuals involved.
b. Specify the pros and cons of each remaining alternative.
c. Weigh the pros and cons to make the final decision. This solution will be the best
alternative if all the other steps were completed well.
5. Prepare a list of steps (instructions) that will result in a fun evening.
6. Evaluate the solution. Are we having fun yet? If nobody is having fun, then the
planner needs to review the steps to have a fun evening to see whether anything can
be changed, if not then the process must start again.
Q.9 Algorithm to swap 2 values
10
Step 1: START
Step 2: declare x,y,z
Step 3: copy value of x to temp
Step 4: copy value of y to x
Step 5: copy value of temp to y
Step 1: STOP
Q. 10 Algorithm to find greater number among 2 number
Step 1: START
Step 2: Read no1 and no2
Step 3: if no1 > no2 then
Display no1 is largest no
Otherwise
Display no2 is largest no
Step 4: STOP