0% found this document useful (0 votes)
43 views5 pages

Pyhton Basics (PT 2)

QUESTION BANK FOR ANNA UNIVERSITY EXAMS

Uploaded by

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

Pyhton Basics (PT 2)

QUESTION BANK FOR ANNA UNIVERSITY EXAMS

Uploaded by

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

PROBLEM-SOLVING AND PYTHON PROGRAMMING GE3151

UNIT 3
TWO (2) MARK QUESTION BANK
1. Write a Python program to accept two numbers and the greatest and
print the result.
num1, num2 = 20 , 30
if num1>num2:
print(num1)
else:
print(num2)

o/p
30

2. What is recursion?
recursion is a method of solving a computational problem where the
solution depends on solutions to smaller instances of the same problem.
Recursion solves such recursive problems by using functions that call
themselves from within their own code.

3. Write down the syntax of if and if-else statements.


Syntax of if statement
if (condition):
Body of if statement
Syntax of if-else statement
if (condition):
Block of code if the condition is True
else:
Block of code if the condition is False

4. With the help of an example, define an array and create an array to print
your name.
An array is a collection of items stored at contiguous memory locations.
The idea is to store multiple items of the same type together. This makes
it easier to calculate the position of each element by simply adding an
offset to a base value, i.e., the memory location of the first element of
the array (generally denoted by the name of the array).

5. Do loop statements in Python have an else clause? When will it be


executed?
Python allows the users to use the else condition with for loops. Further,
the else block just after for/while executes only when the loop does not
terminate by a break statement. This type of else comes into use only if
there is an if condition present inside the loop which somehow is
dependent on the loop variable.
Python supports an else clause in for and while loops. The else body is
executed only if no break is encountered during the execution of the
associated loop body. In one of the scenarios where this can be quite
useful is when you are searching for a particular value in the loop and
the value is not found.

6. Write a Python program to display a list of strings using the range ()


function.
7. Comment with an example of the use of local and global scope variables
with the same identifier name.
Python Global variables are those which are not defined inside any
function and have a global scope whereas Python local variables are
those which are defined inside a function and their scope is limited to
that function only.
In other words, we can say that local variables are accessible only inside
the function in which it was initialized whereas the global variables are
accessible throughout the program and inside every function.
Example
# This function has a variable with
# name same as s.
def f():
s = "Me too."
print(s)

# Global scope
s = "I love Geeksforgeeks"
f()
print(s)
Output
Me too.
I love Geeksforgeeks

8. Write a Python program to accept two numbers, multiply them, and print
the result.
num_1 = int(input(“Enter the value of num_1:”))
num_2 = int(input(“Enter the value of num_2:”))
product = num_1 * num_2
print("Product of {} and {} is {}".format(num_1, num_2,product))

Output
Enter the value of num_1:2
Enter the value of num_2:3
Product of 2 and 3 is 6

9. Present the flow of execution for a while statement.


More formally, here is the flow of execution for a while statement:

1. Evaluate the condition, yielding False or True.


2. If the condition is False, exit the while statement and continue
execution at the next statement.
3. If the condition is True, execute each of the statements in the
body and then go back to step 1.

10.Write a Python program to add two matrices.


# Program to add two matrices using nested loop

X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]

Y = [[9,8,7],
[6,5,4],
[3,2,1]]

result = [[0,0,0],
[0,0,0],
[0,0,0]]

# iterate through rows


for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]

for r in result:
print(r)
Output
[10, 10, 10]
[10, 10, 10]
[10, 10, 10]

11.Write a simple function to multiply two numbers in Python.


def product(a,b): #function for multiplication
multiply=a*b;
return multiply; #return value
num1=int(input("input the number one: "))#input from user for num1
num2=int(input("input the number one: "))#input from user for num2
print("The product is",product(num1,num2))#call the function

Output
input the number one: 24
input the number one: 12
The sum is 288

12.What is the purpose of a pass statement?


The pass statement is used as a placeholder for future code.When the
pass statement is executed, nothing happens, but you avoid getting an
error when empty code is not allowed.Empty code is not allowed in
loops, function definitions, class definitions, or in if statements.

13.What is a linear search?


linear search or sequential search is a method for finding an element
within a list. It sequentially checks each element of the list until a match
is found or the whole list has been searched.
14.Define function composition
Function composition is the way of combining two or more functions in
such a way that the output of one function becomes the input of the
second function and so on. For example, let there be two functions “F”
and “G” and their composition can be represented as F(G(x)) where “x”
is the argument and output of G(x) function will become the input of F()
function.

15.What is the use of [:]and * operator in python?


The slice operator is unique to python and makes our lives as
programmers MUCH easier. It is somewhat a combination between the
range function and indexing. You can think of a slice as a section (or part)
or a collection. We can takes "slices" of lists, strings and tuples. The basic
syntax for a slice is square brackets with colons and integers inside
"[0:1:2]".

myStr[start:stop:step]

The * symbol is commonly used to indicate multiplication, however, it


becomes the repetition operator when the operand on the left side of
the * is a tuple, string or list. The repetition operator duplicates a tuple,
string or list and links all of them together. Even though tuples are
immutable, this can be extended to them.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy