0% found this document useful (0 votes)
6 views11 pages

Unit 3

Hkfy5ueve75ev4es4o7prp7puss ulsuppujjjjj Oh 5t

Uploaded by

vbnji477
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)
6 views11 pages

Unit 3

Hkfy5ueve75ev4es4o7prp7puss ulsuppujjjjj Oh 5t

Uploaded by

vbnji477
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/ 11

GE8151 PROBLEM SOLVING AND PYTHON

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

2. What are the conditional statements used in python?


Conditional statement in python performs different computations or actions
depending on whether a specific Boolean constraint evaluates to true or false. The
s:

conditional statements are


-if
tp

-if else
-elif
ht

-nested if

3. Define if…else statements in python.


If statement is used for decision making. It will run the body of code only when
IF statement is true. When you want to justify one condition while the other condition
is not true. Then you use “if statement”
Syntax:
-if expression:
Statement
Else:
Statement

4. Write the syntax for ternary operator in python?


Ternary operator is also known as conditional expression or operator that
evaluate something based on a condition being true or false it simply allows to test a
condition in a single line replacing the multiline if-else making the code compact.
Syntax: [on_true]if[expression]else[on_false]
Eg: a if (a<b)else b
#program to demonstrate conditional operator
a, b = 10,20
#copy value of a in min if a<b else copy b

/
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

In python 2 iterations are there


While loop:
s:

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

7. Define range ( ) function and its syntax.

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)

8. Define while loop.

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:

For iterating var in sequence:


tp

For iterating var in sequence:

Statements(S)
ht

Syntax for nested while loop

While expression:

While expression:

Statement(S)

Statement(S)

10. What is python break statement?


When a break statement is encountered inside a loop the loop is immediately
terminated and the program control resumes at the next statement following the loop.
Example:
For letter in ‘python’:
If (letter ==’h’):
Continue
Print(‘current letter:’letter)
Output: python

11. What is python continue statement?


The continue statement works somewhat like a break statement. Instead of
facing termination, it forces the next iteration of the loop to take place, skipping any

/
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

print(“this is local variable”,a)


://

output: This is a local variable 10


s

Example:
tp

a = 10
ht

Def my_global( ):

print(“this is global variable”,a)

output: This is a global variable 10

13. What is recursive function and its limitations?


The process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called as recursive function.
Example:
Def function(n);
If n==;
Return 1
Else

14. What is conditional execution?


The ability to check the condition and change the behaviour of the program
accordingly is called conditional execution.

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

16. Explain while loop with example.


The statement inside the while loop is executed only if the condition is
evaluated to true.
Syntax:
While(condition);
Statements
Example:
#program to add natural numbers upto sum = 1+2+3+….+10
n=10
sum=0 #initialize sum and counter
i=1
while(i<=n);
sum=sum+i
i=i+1 #update counter
print (“the sum is”, sun)

17. Explain “for loop” with example

The general form of a for statement is

/
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)

19. Compare string and string slices.


A string is a sequence of character.
Example:
String Slices:
A segment of a string is called string slice. Selecting a slice is similar to
selecting a character.
Example:
>>>s=”Monty Python”
>>>print(s(0:5))
Monty
>>>print(s[6:12])
Python
20. Mention a few string methods.
• S.captilize( ) – capitalizes first character of string
• S.count(sub) – count number of occurance of sub in string
• S.lower( ) – converts a string to lower case
• S.split( ) – returns a list of words in string

/
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

22. Define string immutability.

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

Num1 = float(input(“enter first number:”))


Num2 = float(input(“enter second number:”))
If(num1==num2);
Print(“the numbersare equal”)
Elif(num1>num2);
Print(“num1 is greater”)
Else
Print(“num2 is greater”)

24. Write the merits of using functions in a program.


The advantages of using functions are:
• Reducing duplicate of code
• Decomposing complex problems into simpler pieces
• Improving clarity of the code
• Reduce of code
• Information hiding

25. Define fruitful functions in python.


In python function which returning some values means we say it is fruitful
functions.
26. What are the types of functions arguments?

/
• 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

• Remove( ) - removes an item from the list


//w

• Pop( ) - removes and returns an element at the given index


• Clear( ) - removes all items from the list
• Index( ) - returns the index of the first matched item
• Count( ) – returns the count of number of items passed as an argument
s:

• Sort( ) – sort items in a list in ascending order


tp

• Reverse( ) – reverse the order of items in the list


• Copy( ) – returns a shallow copy of the list
ht

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.

30. What will be the output of print str[2:5] if str=’hello world’?

o/p: [ ]0
31. Where does indexing starts in python?

In python the indexing starts with 0 index.

32. Define array with an example.

/
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])

print(a) o/p: 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

passed to the function at run-time so that


the function can do the designated task
w

using these values.


://

34. Write the rules to define a function?


s

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

Unit II: Data Types, Expressions, Statements


Data Types, Expressions, Statements | Introduction to Python | How to Write and Execute Python Program | Concept
of Interpreter and Compiler | Python Interpreter | Interactive and Script Modes | Debugging | Values and Types |
Variables, Expressions and Statements | Tuple Assignment | Indentation, String Operations | Functions | Illustrative
Programs | Anna University Two Marks Questions & Answers | Multiple Choice Questions

Unit III: Control Flow, Functions, Strings


Control Flow, Functions, Strings | Boolean Values | Operators | Input and Output | Conditional Statements | Iteration
| Fruitful Functions | Recursion | Strings | Lists as arrays | Illustrative Programs | Anna University Two Marks
Questions & Answers | Multiple Choice Questions

Unit IV: Lists, Tuples, Dictionaries


Lists, Tuples, Dictionaries | Lists | Tuples | Dictionaries | Advanced List Processing - List Comprehension | Illustrative
Programs | Anna University Two Marks Questions & Answers | Multiple Choice Questions

Unit V: Files, Modules, Packages


Files, Modules, Packages | Files | Command Line Arguments | Errors and Exceptions | Modules | Packages | Two Marks
Questions with Answers | Multiple Choice Questions

Common to all 1st Semester

HOME | EEE | ECE | MECH | CIVIL | CSE


1st Semester Anna University EEE- Reg 2021
Professional English – I
2nd Semester 3rd Semester
Matrices and Calculus
Probability and Complex
Professional English - II Functions
Engineering Physics
Statistics and Numerical Electromagnetic Fields
Engineering Chemistry Methods
Problem Solving and Physics for Electrical Digital Logic Circuits
Python Programming Engineering
Electron Devices and
Physics and Chemistry Basic Civil and Mechanical Circuits
Laboratory Engineering
Electrical Machines - I
Engineering Graphics
C Programming and Data
4th Semester Electric Circuit Analysis Structures
Environmental Sciences
and Sustainability 6th Semester
Transmission and 5th Semester
Distribution Protection and
Linear Integrated Power System Analysis Switchgear
Circuits
Power Electronics Power System
Measurements and Operation and Control
Instrumentation Control Systems
Open Elective – I
Microprocessor and
Microcontroller Professional Elective I
Professional Elective IV
Electrical Machines - II Professional Elective II
Professional Elective V
Professional Elective III
7th Semester Professional Elective VI
High Voltage Mandatory Course-I& Mandatory Course-
Engineering II& MC
Human Values and 8th Semester
Ethics
Elective –
Management Project Work /
Internship
Open Elective – II
Open Elective – III
Open Elective – IV
Professional Elective
VII Click on Clouds to navigate other department

https://www.poriyaan.in/

You might also like