Featured

    Featured Posts

Function In Python Part-3


Returning values from functions
§  A function which wants to return some result after performing the task such type method is known as return type function.

§  Python functions can return values of any type via the return keyword
§  Functions can return a value that you can use directly.

def give_me_ten():
    return 10

#Now let’s call the defined  give_me_ten() function:
ten=give_me_ten()
print(ten)

#  OR let’s call the defined  give_me_ten() function with-in print function:
print(give_me_ten())

or use the value for any operations:

def give_me_ten():
    return 10

#Now let’s call the defined  give_me_ten() function:
print(give_me_ten()+20)

§ If return is encountered in the function the function will be exited immediately and subsequent operations will not be evaluated

def give_me_ten():
    return 10
    print('This statement will not be printed. Ever.')


#Now let’s call the defined  give_me_ten() function:
print(give_me_ten())

§ You can also return multiple values (in the form of a tuple):

def give_me_two_ten():
    return 10,10 # Returns two 10

#Now let’s call the defined  give_me_ten() function:
first, second = give_me_two_ten()
print(first) # Out: 5 
print(second) # Out: 5


§  A function with no return statement implicitly returns None. 
§  Similarly, a function with a return statement, but no return value or variable returns None




Function In Python Part-2


ü Python has many built-in functions like print(), input(), len(). Besides built-ins you can also create your own functions to do more specific jobs—these are called user-defined functions.


How We can Defining and calling simple functions In Python

def function_name(parameters):
       statement(s)
Points to remember
§  Use the def keyword to define a function, followed by the name of the function. The name of the function should be followed by parentheses ().

§  Function parameters are optional. The parameter names must be with in parentheses separated by commas.


#Here’s an example of a simple function definition

def greet():
    print('Hello Python')

#Now let’s call the defined greet() function:
greet()

Type of declaration of methods based on return type and arguments:

Components of a Function


Basically, there are two types of arguments:
Actual arguments
Formal arguments
Formal arguments
§  The variables declared within the parentheses following the function name in a function declaration or definition are known as Formal arguments
Actual arguments
§  the values that are passed within the parentheses of a function call are known as Actual arguments.

1.     Method with out return type  and without arguments.

#Here’s an example of a simple function definition

def add():
    a=10
    b=20
    c=a+b
    print('Sum= ',c) 

#Now let’s call the defined add() function:
add()

2.     Method without return type and with arguments.

§  parameters is an optional list of identifiers that get bound to the values supplied as arguments when the function is called.

§  A function may have an arbitrary number of arguments which are separated by commas.

#Method with out return type and with arguments.

def add(a,b):
    c=a+b
    print('sum= ',c)

#Now let’s call the defined add() function:
add(10,20)

3.     Method with return type  and without arguments.

§  A function which doesn’t wants to return some result after performing the task such type method is known as non-return type function.
§  A function which wants to return some result after performing the task such type method is known as return type function.
§  Python functions can return values of any type via the return keyword.

#Method with return type and with out arguments.
def add():
    a=10
    b=20
    c=a+b
return c

#Now let’s call the defined add() function:
res=add()
print('sum= ',res)

§  You'll notice that unlike many other languages, you do not need to explicitly declare a return type of the function. 

§  Python functions can return values of any type via the return keyword. One function can return any number of different types!

#Method with return type  and without arguments.

def many_types(x):
    if x < 0:
        return "Hello!" 
    else:
        return 0


#Now let’s call the defined many_types function:
print(many_types(1)) 
print(many_types(-1)

# Output: 
Hello!

4.     Method with return type and with arguments.

#Method with  return type and with arguments.
def add(a,b):
    c=a+b
    return c

#Now let’s call the defined add() function:
res=add(10,20)
print('sum= ',res)

Function In Python Part-1




ü Function is a sub block that contains set of lines of Code.

ü A function is a block of reusable code. A function allows us to write code once and use it as many times as we want just by calling the function.

ü Functions in Python provide organized, reusable and modular code to perform a set of specific actions

ü Python allows us to divide a complex program into the basic building blocks known as function.

ü The function contains the set of programming statements enclosed by {}

ü A function can be called multiple times to provide reusability and modularity to the python program.

Python Break, Continue Statement With while Loop

The break Statement In Python:
§  break is a statement which is used to break (terminate) the loop execution and program’s control reaches to the next statement written after the loop body.
§  It brings control out of the loop

Take input from user infinite times but when user enter zero or negative value program will be terminated.

#infinite loop
while True:
    n=int(input('enter number'))
    if n<0 or n==0:
        print('Terminating the Loop..')
        break
    print('Number is: ',n)
print('Welcome OutSide the Loop')
Output:
enter number10
Number is:  10
enter number20
Number is:  20
enter number30
Number is:  30
enter number0
Terminating the Loop..
Welcome OutSide the Loop

Write a python program to successively removes items from a list using .pop() until it is empty:
langs_list=['java','python','c','sql','angular']
while True:
    if not langs_list:
        break
    print(langs_list.pop())
Output
angular
sql
c
python
java
Note:-

§  When a becomes empty, not a becomes true, and the break statement exits the loop.


The continue Statement In Python:
§  The continue statement is used to skip the rest of the code inside a loop for the current iteration only.
§  The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.
§  Syntax:





Write a python program to skip all even number from 1 to n.
n=int(input('enter number'))
i=0
while i<=n:
    i+=1
    if i%2==0:
        continue
    print(i,end='')
print('\nwelcome outside loop!.')
Output
enter number10
1357911
welcome outside loop!.

Python break, continue statement with for loop

The break Statement In Python:
§  break is a statement which is used to break (terminate) the loop execution and program’s control reaches to the next statement written after the loop body.
§  It brings control out of the loop
Syntax:


for i in range(1,10):
    if i==5:
        break
    print(i)
print('Welcome Out of loop')
Output:
1
2                                                           
3
4
Welcome Out of loop
Explanation
§  In this program, we iterate number from 1 to 10 through range and We check if the number is "i", upon which we break from the loop. Hence, we see in our output that all the number up till "i" gets printed. After that, the loop terminates.

Write a Python program to enter any string Print all the letters that are present before the vowel in given word
word=input('enter any word')
for w in word:
    if w in 'aeiou':
        break;
    print(w,end='')
Output:
enter any wordpython
pyth

The continue Statement In Python:
§  The continue statement is used to skip the rest of the code inside a loop for the current iteration only.
§  The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.
§  Syntax:








Example:
for i in range(1,10):
    if i==5:
        continue
    print(i,end='')
print('\nwelcome outside loop!.')
Output:
12346789
welcome outside loop!.


Explanation
§  In this program, we iterate number from 1 to 10 through range and We check if the number is "i", upon which we break from the loop. Hence, we see in our output that all the number up till "i" gets printed. After that, the loop terminates.


Write a Python program to enter any string Print all the letters that are present before the vowel in given word
word=input('enter word')
word=word.lower()
for w in word:
    if w in 'aeiou':
        continue
    else:
        print(w,end='')    
Output:
enter wordpython
pythn


www.CodeNirvana.in

Powered by Blogger.

About

Site Links

Popular Posts

Translate

Total Pageviews