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

Q1) What is problem? Explain six steps of problem solving?

Ans) Problem is defined as a situation or issue or condition which needs to solve to achieve the goal.
1) Identify the problem. The first step toward solving a problem is to identify the problem >What is the
specific problem? (This means you should determine what is that you want to change) >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>3) Identify alternative ways to solve the problem >Generate as many potential solutions as
possible >List the features for each possible solution > 4) Select the best way to solve the problem from
the list of alternative solutions >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 5) List the instructions using the selected solution. >These numbered, step-by-step instructions
must fall within the knowledge base set up in step 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.
Q2) Explain different features of python?.
Ans) Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This allows
the student to pick up the language quickly.>Easy-to-read − Python code is more clearly defined and visible
to the eyes.>Easy-to-maintain − Python's source code is fairly easy-to-maintain.>A broad standard library −
Python's bulk of the library is very portable and cross-platform compatible on UNIX, Windows, and Macintosh
>Interactive Mode − Python has support for an interactive mode which allows interactive testing and
debugging of snippets of code.>Portable − Python can run on a wide variety of hardware platforms and has
the same interface on all platforms.
Q3) Write an algorithm to swap two numbers?.
Ans) # Python program to swap two variables >x = 5>y = 10 ># To take inputs from the user >#x = input('Enter
value of x: ') >#y = input('Enter value of y: ') ># create a temporary variable and swap the values >temp = x >x
= y >y = temp >print('The value of x after swapping: {}'.format(x)) >print('The value of y after swapping:
{}'.format(y))
Q4) What are identifiers? List the rules to name an identifier.
Ans) Identifiers can be combination of uppercase and lowercase letters, digits or an underscore(_). ... >An
Identifier can not start with digit. ... >We can't use special symbols like !,#,@,%,$ etc in our Identifier.
>Identifier can be of any length.
Q5) What is modularization? Explain top down design approach & Bottom-Up Approach
Ans) Modularization is the technique of splitting a large programming task into smaller, separate, and
manageable subtasks. Like most modern programming languages, Python is a modular programming
language.> Top-down approach is essentially the breaking down of a system to gain insights into the
subsystems that makes it up. •In top down design we start a complex problem and go on dividing into
successively smaller subproblems. • The top down design strategy is also known as stepwise refinement.
>Bottom-Up Approach is one in which the smaller problems are solved, and then these solved problems are
integrated to find the solution to a bigger problem. Therefore, it uses composition approach.
Q6) History of python Language
Ans) Python was developed by Guido van Rossum in the late eighties and early nineties at the National
Research Institute for Mathematics and Computer Science in the Netherlands.>Python is derived from many
other languages, including ABC, Modula-3, C, C++, Algol-68, Small Talk, and Unix shell and other scripting
languages.>Python is copyrighted. Like Perl, Python source code is now available under the GNU General
Public License (GPL).>Python is now maintained by a core development team at the institute, although Guido
van Rossum still holds a vital role in directing its progress.
Q7) Pseudo code
Ans) The Pseudo code is neither an algorithm nor a program. It is an abstract form of a program. >It consists
of English like statements which perform the specific operations. >It is defined for an algorithm. >It does not
use any graphical representation. >In pseudo code, the program is represented in terms of words and phrases,
but the syntax of program is not strictly followed.
Q8) Explain flow-chart and algorithm with example
Ans) A flowchart is a graphical or symbolic representation of a process. •Is used to design and document
virtual complex processes to help the viewers to visualize the logic of process. • Symbols in flowchart are
linked together with arrows to show the flow of logic in the process

algorithm > •It is a sequence of instructions carried out to solve some problem. >•And in solving some
problem , series of actions are taken to reach to the solution

Q9) Explain different data types supported by Python


Ans) Python provides various standard data types that define the storage method on each of them.
The data types defined in Python are given below. 1)Numbers : Number stores numeric values.
Python creates Number objects when a number is assigned to a variable. For example;
>a = 3 , b = 5 #a and b are number objects >2)String : The string can be defined as the sequence of
characters represented in the quotation marks. In python, we can use single, double, or triple quotes
to define a string.>String handling in python is a straightforward task since there are various inbuilt
functions and operators provided. >3)List : Lists are the most versatile of Python's compound data
types. A list contains items separated by commas and enclosed within square brackets ([]). >To some
extent, lists are similar to arrays in C. One difference between them is that all the items belonging to
a list can be of different data type. >The values stored in a list can be accessed using the slice operator
( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the list and working their way to end-1.
>The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator
>4)Tuple : A tuple is similar to the list in many ways. Like lists, tuples also contain the collection of
the items of different data types. The items of the tuple are separated with a comma (,) and enclosed
in parentheses ().>A tuple is a read-only data structure as we can't modify the size and value of the
items of a tuple. >5) Dictionary : Dictionary is an ordered set of a key-value pair of items. It is like an
associative array or a hash table where each key stores a specific value. Key can hold any primitive
data type whereas value is an arbitrary Python object.>The items in the dictionary are separated with
the comma and enclosed in the curly braces {}>Example of Dictionary •a={1 : “Red”, 2: “Blue”, 3:
“Green”} • print(a) • print(a. keys()) • print(a. values())
Q10) List down types of operators in Python
Ans) Operators are used to perform operations on variables and values. >Python divides the operators in the
following groups: >Arithmetic operators >Assignment operators = num1=10 >Comparison operators ==
>Relational operators < > <= >= != >Logical operators && || ! >Identity operators >Membership operators
>Bitwise operators
Q11) Explain for loop & while loop with flow chart.
Ans) for loop :Provides a mechanism to repeat a task until a particular condition is True. >The for loop is
usually known as a determinate or definite loop because the programmer knows exactly how many times the
loop will repeat. >The number of times the loop has to be executed can be determined mathematically
checking the logic of the loop. >The for…in statement is a looping statement used in Python to iterate over a
sequence of objects.

While loop: Provides a mechanism to repeat one or more statements while a particular condition is True. •If
condition is true, only then the statements will be executed otherwise if the condition is false , the control
will jump to statement y, that is the immediate statement outside the while loop block. • A while loop is also
referred to as a top-checking loop since control condition is placed as the first line of the code. •If the control
condition evaluates to false, then the statements enclosed in the loop are never executed
Q12) Explain selection/conditional statements in Python.
Ans) The decision control statements usually jumps from one part of code to another depending on whether
a particular condition is satisfied or not. •They allow you to execute statements selectively based on certain
decision. • Such type of decision control statements are known as selection control statements or conditional
branching statements.

Q13) Describe the following terms with example i) break ii) continue iii) pass iv) range
Ans) i) break: To end a while loop prematurely, the break statement can be used. >When
encountered inside a loop, the break statement causes the loop to finish immediately. ii) continue:
Continue statement can only appear in the body of a loop. >When the compiler encounters continue,
then the rest of the statements in the loop are skipped and the control is unconditionally transferred
>to the loop-continuation portion of the nearest enclosing loop. >Its syntax is simple just type
continue keyword continue iii) pass: Used when a statement is required syntactically but no
command or code has to be executed. >It specifies a null operation or simply No Operation (NOP)
statement. >Nothing happens when the pass statement is executed. >Syntax of pass statement is
simple, just type the keyword pass as pass iv) range: The range () is a built-in function in Python that
is used to iterate over a sequence of numbers. >The syntax of range () is range(beg, end, [step]) >The
range() produces a sequence of numbers starting with >beg(inclusive) and ending with one less than
the number end. >The step argument is optional by default, every number in range is incremented
by 1 but we can specify a different increment using step.
Q14) i) Comment ii) Reserve Words iii) Indentation
Ans) i) Comment: They are non-executable statements in a program. • They are just to
describe the statements in the program code. • Comments helps us in making the code
readable and understandable by the programmer as well as the common man. • The
interpreter simply ignores the comments. •A hash sign (#) is used to start the comment. ii)
Reserve Words: •In every programming language there are certain words which have pre-
defined meaning. • Reserved words cannot be used for naming identifiers.
iii) Indentations •Whitespace at the beginning of the line is called indentation. • In python,
indentation is used to associate and group statements. • These group statements are used to
form block of statements. •All statements inside the block should be at the same indentation
level
Q15) Nested Loops
Ans) Python allows its users to have nested loops, that is, loops that can be placed inside
other loops. • Although this feature will work with any loop as well as for loop, but it is most
commonly used with the for loop, because this is easiest to control. • A for loop can be used
to control the number of times a particular set of statements will be executed. • Another
outer loop could be used to control the number of times that the whole loop is repeated.
Q.16) Write an algorithm and draw flowchart to check whether a given number is even or odd.
Ans)

Q. 17) Write python program using function to find whether number is odd or even.
Ans) x = 2
# x= 41
if x % 2 == 0:
print (x, "Is Even Number")
else:

print (x, "Is Odd Number")


Output:
2 Is Even Number
5)program to display mul plica on table of numbers from 1 to
1)Program for Fibonacci series 10

n_terms = int(input("Enter the number of terms: ")) for i in range(1, 11):

a, b = 0, 1 for j in range(1, 11):

print("Fibonacci series is") print(i*j, end=' ')

print(a, b, end=" ")


6)program for a number is prime or not
for i in range(2, n_terms):
num = int(input("Enter value of a number: "))
c=a+b
i=2
print(c, end=" ")
while i <= num - 1:
a=b
if num % i == 0:
b=c
print('Not a prime number')
break
2)Program for a factorial
i += 1
n=int (input("Enter a number"))
else:
fact=1
print("Prime number")
i=1
while i<=n: 6)pseudo code for factorial of a number

fact=fact *i Read number

i=i+1 fact = 1

print("factorial of a number",n,fact) i=1


While i <= number
3)Write a program to find the sum 1 to 10 number fact = fact * i
print("enter the value of n") i=i+1
n=int (input ( ) ) EndWhile
sum=0 Print fact
for i in range (1,n+1):
sum=sum+i
print ("the sum of ",n,"number is: ",sum )

4)program to display mul plica on table

print("Enter number for mul plica on table")


n = int(input())
for i in range(1, 11):
print(n, "X", i, "=", n*i)
rows = 5 n=5 for i in range(4, 0, -1): for i in range(0, 5):
for i in range(rows, 0, -1): for i in range(1, n+1): for j in range(0, i): for j in range(0, i+1):
for j in range(rows - i): print(' ' * (n-i), print("*", end=" ") print("*", end=" ")
end='')
print(" ", end="") print("\r") print("\r")
print('* ' * i)
for j in range(i): **** *
*
print("* ", end="") *** **
* *
print() ** ***
* * *
* * * * * * ****
* * * *
* * * * *****

* * *

* *

num = float(input("Enter a number: ")) num1 = 5.0 n = int(input("Enter a number: "))


if num > 0: num2 = 10.0 if n % 2 == 0:
print("The number entered is posi ve.") temp = num1 print("Even Number")
elif num < 0: num1 = num2 else:
print("The number entered is nega ve.") num2 = temp print("Odd Number")
else: print("\nA er swapping:")
print("The number entered is zero.") print("First number:", num1) EVEN ODD

print("Second number:", num2)


+ve/-ve/0 SWAP TWO NUMBERS

num1 = int(input("Enter first number: ")) numbers = list(range(1, 11))


num2 = int(input("Enter second number: ")) sum_numbers = sum(numbers)
if num1 > num2: average_numbers = sum_numbers / len(numbers)
print("NUM1 is the largest number") print(f"The sum of the first 10 numbers is {sum_numbers}.")
else: print(f"The average of the first 10 numbers is {average_numbers}.")
print("NUM2 is the largest number")
LARGER NUMBER SUM AND AVG. FIRST 10 NUMBERS

You might also like