0% found this document useful (0 votes)
56 views

UNIT III Hint

1. This document discusses control flow and functions in Python. It covers boolean values, operators, conditional statements like if, else, and elif. It also covers looping statements like while and for loops, as well as nested loops. Loop control statements like break, continue, and pass are explained. Finally, it discusses fruitful functions, return values, parameters, and arguments. 2. The key conditional statements are if, else, and elif, which allow for different code blocks to execute depending on if a condition is true or false. Looping statements like while and for are used to repeatedly execute a block of code. Nested loops contain loops within other loops. Loop control statements alter normal loop execution. 3. Fruit

Uploaded by

vengai
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)
56 views

UNIT III Hint

1. This document discusses control flow and functions in Python. It covers boolean values, operators, conditional statements like if, else, and elif. It also covers looping statements like while and for loops, as well as nested loops. Loop control statements like break, continue, and pass are explained. Finally, it discusses fruitful functions, return values, parameters, and arguments. 2. The key conditional statements are if, else, and elif, which allow for different code blocks to execute depending on if a condition is true or false. Looping statements like while and for are used to repeatedly execute a block of code. Nested loops contain loops within other loops. Loop control statements alter normal loop execution. 3. Fruit

Uploaded by

vengai
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/ 2

UNIT III CONTROL FLOW, FUNCTIONS

1. Boolean values: It returns True or False. Conditions are check with the help of = = comparison operator or relational operator
Eg: if(5==5): print(“Hello”) Output: Hello
2. Operators: An operator is a symbol that specifies an operation to be performed on the operands.
Eg: a+b (a and b are operands, + is the operator) – PEMDAS rule is followed for operator precedence
Types of operators – Arithmetic operator(+,-,*,/,%,**,//), Comparison or Relational operator (!=,==,<=,>=,<,>), Logical operator(and, or,
not), Assignment operator(=,+=,-=,*=,/=,%=,**=,//=), bitwise operator(&, |, ^,<<,>>,~), Membership operator(in, not in),
Identity operator(is,is not), Unary operator(-,+), Boolean operator(and,or,not) Eg: a=10; b=20; c=a+b; print(c)
3. Conditionals and Control statements/ Decision making statements: The sequence of instructions are controlled by condition
The if statement is a Syntax: Example:
decision making statement. if (condition is true): x=int(input())
It is used to control the flow True statement if( x > 0):
Conditional (if)
of execution to test logically print (x,” is positive number")
whether the condition is true
or false
It is two way decision Syntax: Example:
making statements. It if (condition is true): x=int(input())
executes some statements if True statement if (x%2 == 0):
Alternative (if-else) the condition is true and else: print(x, "is even number")
executes some other False statement else:
statements, when the print(x, "is odd number")
condition is false.
It is multi way decision Syntax: Example:
making statement. The elif if(condition1 is true): x=int(input()); y=int(input())
statement allows you to statement if (x < y):
check multiple expressions elif(condition2 is true): print (x, "is less than", y)
Chained conditional
for TRUE and execute a statement elif( x > y):
(if-elif-else)
block of code as soon as elif(condition3 is true): print( x, "is greater than", y)
one of the conditions statement else:
evaluates to TRUE else: print (x, "and", y, "are equal")
statement
One conditional can also be Syntax: Example:
nested within another. if(condition is true): If(x == y):
statement Print(x, "and", y, "are equal")
else: else:
Nested conditionals
if(condition): if(x < y):
statement print(x, "is less than", y)
else: else:
statement print(x, "is greater than", y)
4. Iteration / Looping statements
Iteration: Repeated execution of a set of statements using either a recursive function call or a loop.
loop: A statement or group of statements that execute repeatedly until a terminating condition is satisfied.
Looping states: 1.Initialization of condition variable 2.Test control statement 3.Execute the loop 4.Updating the condition variable
It is an Entry controlled loop. Syntax: Example:
It is a repetitive control while(condition): d1=int(input(“1st number”))
structure, used to execute body of the loop d2=int(input(“2nd number”))
remainder=d1%d2
while loop the statements within the ..
while(remainder!=0):
body, until the condition update condition d1,d2=d2,remainder
becomes false remainder=d1%d2
print(“GCD is :”,d2)
It is another repetitive Syntax: Example:
control structure, and is for iterating_var in sequence: from array import *
used to execute a set of statements a=array(‘i', [1,2,3,4,5])
for loop instructions repeatedly, until sum=0
for i in a:
the condition becomes false sum=sum+i
print(sum)
The loop within the loop is Syntax: Example:
nested loop. Two or more for iterating_var in sequence: a=[[1,2,3],[4,5,6]]
Nested loop while/for statements are for iterating_var in sequence: for i in range(0,3):
included in the body of the statements for j in range(0,3):
loop. print(a[i][j])
5. Loop control statements – It change execution from its normal sequence.
It is used to terminate the Syntax: Example:
loop statement immediately. while(condition): n=1
break is usually associated if(condition): while(n>0):
break with an if statement break print n
n=n+1
if(n==5):
break
It returns the control to the Syntax: n=10
beginning of the loop, It skip while(condition): while(n>0):
the remainder of its body of if(condition): print n
continue n=n-1
the loop continue
if(n==5):
continue
It is a null operation, nothing Syntax: Example:
happens when it executes. It while(condition): n=10
is useful to check the code if(condition): while(n>0):
print n
pass syntactically pass
n=n-1
if(n==5):
pass
print(“pass block”)
6. Fruitful functions: Functions that return values is called fruitful functions.
Return values: Inside the function you may return single or multiple values
Function Parameters: The names given in the function definition are called parameters
Function argument: Whereas the values you supply in the function call are called arguments.
def add(a,b): def add(a,b): def fun(a,b): Output
c=a+b return a+b if(a>b): Answer 50
return 1
return c else:
Answer 50
return 0 Answer 0
result=add(10,40) result=add(10,40) result=add(10,40)
print(“Answer”, result) print(“Answer”, result) print(“Answer”, result)

7. Scope of variable: Local variable is not related in any way of other variables with same name in outside function
Local scope: Variables that are defined inside a function a=5 # global variable Output
Global scope: Variables that are defined outside a function def fun(): 3
a=3 # local variable 5
print(a)
fun()
print(a)
8. Function composition:
A Function can call one function from within another. def fun2(): Output
print("programming") python
def fun1( ): programming
print("python")
fun2()
fun1( )

def factorial(n): Factorial Program Output


if(n==0): 120
9. Recursion: return 1
else:
It is the process of calling the same function itself
return(n*factorial(n-1))
again and again until some condition is satisfied. result=factorial(5)
print(result)

10. Strings: String is a collection of characters delimited by ‘single’ or “double quotes”. Eg: fruit="banana"; print(fruit)
String operators: + string concatenation, * repetition, [ : ] string slice, in and not in – membership, % Format string
Eg: str1="Hello"; str2="Python"; print(str1+str2) Output: HelloPython
Eg: str1="Hello"; print(str1*2); print(str1[0:3]) Output: HelloHello Hel
String slices: A segment of a string is called a slice [ : ] Selecting a slice is similar to selecting a character. Index value starts at 0 and -1
from the end. Eg: s="Peter, Paul, and Mary"; print s[0:5]; print s[7:11]; print s[17:21] Output: Peter Paul Mary
String Immutability: Strings are immutable, which means you can't change an existing string.
Eg: s="Hello, world!"; s[0]='J'; print(s) Output: 's' object does not support item assignment
The best you can do is create a new string that is a variation on the original:
Eg: s="Hello, world!"; n='J'+s[1:]; print(n) Output: Jello, world!
11. String functions and methods:
Functions/ Description Example Output
Methods
len( ) Returns the number of characters in a string fruit = "banana"; len(fruit) 6
== String comparison. It checks two strings are equal or not fruit="banana"; Yes equal
if(fruit=="banana"):
print("Yes equal")
find( ) Returns the index of character print(string.find("banana", "na") 2
split( ) It splits single multi-word string into a list of individual words, book="problem solving and python ['problem', 'solving', 'and',
removing all white spaces between them. programming"; book.split(); 'python', 'programming']
join( ) Concatenate two strings, add white space to string book="python"; print(" ".join(book)) python
replace( ) Original string is updated with some replacement b="python program"; java program
print(b.replace("python","java"))
upper( ) Original string converted in to Upper case b="python programming"; b.upper(); 'PYTHON
PROGRAMMING'
lower( ) Original string converted in to Lower case b="Python programming"; b.lower(); ‘python programming’

12. String module: The string module contains useful functions that manipulate strings. As usual, we have to import the module before we can
use it. Eg: import string; fruit="banana"; print(string.find("banana", "na")) Output: 2
from array import * Output
13. Lists as arrays:
a=array(‘i', [1,2,3,-4,5]) 1
An array is a collection of elements of same datatype along with list. print(“The array elements are:”) 2
‘i’-signed integer, ‘I’-unsigned integer, ‘u’-alphabet for i in a: 3
Advantages: To store & process a group of elements easily. print i -4
Syntax: arrayname=array(type code,[elements]) Eg: a=array(‘I’,[1,2,3,4,5]) 5

You might also like