Unit 3
Unit 3
PROGRAMMING
UNIT-III
/
in
CONTROL FLOW, FUNCTIONS
n.
PART-A
aa
1. Define Boolean data type?
The Boolean data type is either True or False. In python, Boolean variables are
defined by the True and False keywords.
riy
>>>a=True
>>>type(a)
o
<class’bool’>
>>>b=False
>>>type(b)
.p
w
<class’bool’>
The output<bool’bool’> indicates the variable is a Boolean data type. True and
w
False must have an Upper Case first letter. Using a lowercase true returns an error.
//w
-if else
-elif
ht
-nested if
/
Min=a if a<b else b
in
Print(min)
n.
5. Define chained conditionals.
aa
Sometimes there are more than two possibilities and we need more than two
branches. One way to express a computation: like that is a chained conditional:
iy
Ifx<y:
Prin t(‘x is less than y’)
or
Elif x>y:
Print (‘x is greater than y’)
.p
Else:
Print(‘x and y are equal’)
w
Elif is an abbreviation of “else if”. Again, exactly one branch will be executed.
w
6. Define iteration.
Repeated execution of a set of statements is called iteration.
//w
The statements inside the while loop is executed only if the condition is
evaluated to true.
tp
Syntax:
While(condition)
ht
statements
For loop:
The general form of a for statement is
Syntax:
For variable in sequence:
Code block
The range ( ) function generated the integer numbers between the given start
integer to the stop integer, which is generally used to iterate over with for loop. The
range ( ) function returns a sequence of numbers, starting from 0 by default and
increments by 1 (by default), and end at a specified number.
Syntax:
Range(start,stop,step)
eg: for in range(1,10,2):
print(i)
output: 1,3,5,7,9
Parameters:
/
Start: element from which sequence constructed has to start(default)
in
Stop: element number at which numbers in sequence have to end (exclusive)
n.
Step: can be +ve or -ve number, denoting the elements need to be skipped during
aa
filling of list(default1)
riy
In python, while loop is used to execute a block of statements repeatedly until
a given condition is satisfied. And when the condition becomes false, the line
immediately after the loop in program is executed.
o
Syntax: .p
While(expression):
Statement(S)
w
9. Write the syntax for nested for loops and nested while loop statements:
w
If one looping statement is present inside another looping statement then it is called as
//w
nester loops.
s:
Statements(S)
ht
While expression:
While expression:
Statement(S)
Statement(S)
/
code in between.
.in
Example:
For letter in ‘python’:
n
If(letter==’h’):
Continue
aa
Print(‘current letter:’letter) output: python
iy
12. Write the scope of the variable.
The scope of a variable refers to the places that we can see or access a
or
variable. If we define a variable on the top of the script or module, the variable is called
global variable. The variables that are defined inside a class or function is called local
.p
variable.
w
Example:
w
Def my_local( ):
a = 10
w
Example:
tp
a = 10
ht
Def my_global( ):
Example:
If statement:
/
The simplest form of if statement is
.in
Syntax:
n
If(test_expression);
aa
Statement
Example: x=4
iy
If(x>0);
or
Print(“x is positive”) output: x is positive
.p
The Boolean expression after if is called the condition. If it is true, then the
indented statement gets executed. If not, nothing happens.
w
15. What is alternative execution?
A second forth of if statement is alternative execution, that is, if ….else, where
w
there are two possibilities and the condition determines which one to execute.
//w
Syntax:
If(Test_expression);
Statement
s:
Else
Statement
tp
Example x=4
If(x%2==0);
ht
Print(“x is even”)
Else
Print(“x is odd”) output:x is even
/
in
Syntax:
n.
For variable in sequence
aa
Block
Example:
iy
X=4
or
For I in range(0,x)
Print( ) output:0,1,2,3
.p
18. Explain function composition.
w
Function composition is called as nested function if one function call is present
inside other function, we say it is function composition.
w
Example:
//w
Def power(b,p);
Y=b*p;
Return y
s:
Def square(x);
a=power(x,2)
tp
return a
n=int(input(“enter the value”))
ht
result=square(n)
print(result)
/
21. What are string methods?
in
A method is similar to a function – it takes arguments and returns a value – but the
n.
syntax is different. For example: the method upper takes a string and returns a new
string with all uppercase letters.
aa
Instead of the function syntax upper(word), it uses the method syntax word.upper( )
>>>word – “banana”
>>>new_word=word.upper( )
iy
>>>print(new_word)
BANANA
or
.p
Python strings are immutable ‘a’ is not a string. It is a variable with string value.
We can’t mutate the string but can change what value of the variable to a new string.
w
Program Output
w
a=”foo” #foofoo
b=a #foo
//w
a=a+a
# a point to the new string “foofoo”, but b It is observed that ‘b’ hasn’t changed
points to the same old “foo”. even though ‘a’ has changed.
s:
Print(a)
Print(b)
tp
23. Write a python program to accept two numbers and find the largest among them.
ht
/
• Required Arguments
in
• Default arguments
• Keyword arguments
n.
• Variable-length Arguments
aa
27. What are the various parameter passing techniques?
• Pass by value
iy
• Pass by reference
or
28. List some of the methods to list?
Python list methods
.p
• Append( ) – Add an element to the end of the list
• Extend( ) – Add all elements of a list to the another list
w
• Insert( ) – insert an item at the defined index
w
29. State the difference between linear search and binary search.
Linear search is used for finding a target value within a list. It’s also called
sequential search that because it sequentially checks each element of the list for the
target value until a match is found or until all the elements have been searched. Linear
search also runs in at worst linear time and makes 0(n) comparisons.
Binary Search is also called half-interval search or binary chop. It’s a search
algorithm that find the position of a target value by repeatedly dividing the search
interval in half. And the whole array begins with an interval covering. It compares the
target value to the middle element of the array. If they are an unequal half array to be
searched then it is eliminated and the search continues on the remaining half until it is
successful. It runs in at worst algorithm time, making 0(log n) comparisons.
o/p: [ ]0
31. Where does indexing starts in python?
/
An array is a special variable, which can hold more than one value of same
.in
type at a time. We need to import array module to create arrays. For example:
n
Import array
aa
a=array.array(‘d’,[1.1,3.5,4.5])
iy
33. Difference between Arguments and parameters in python?
or
Arguments.p Parameters
Arguments are the value passed for Parameters are the variables that are
these parameters while calling a defined or used inside parentheses while
w
function. defining a function.
Arguments are the values thar are
w
Function blocks begin with the keyword def followed by the function name and
tp
parentheses.
ht
Problem Solving and Python Programming (GE3151) – Reg 2021
Unit I: Computational Thinking and Problem Solving
Computational Thinking and Problem Solving | Fundamentals of Computing | Identification of Computational Problems |
Algorithms | Building Blocks | Notation | Algorithmic Problem Solving | Simple Strategies for Developing Algorithms |
Illustrative Problems | Anna University Two Marks Questions & Answers | Multiple Choice Questions and Answers
https://www.poriyaan.in/