Python Tutorial - 10. Functions

preview_player
Показать описание
In today’s session, we will talk about “functions” in python. The video will explain the introduction of “functions”, why “functions” are needed, encapsulate code in a function, default arguments, the difference between “local and global variables” and “document strings”.

Topics that are covered in this Python Video:
0:16 Functions introduction
1:14 why functions are needed
3:38 encapsulate code in function
3:44 Define function
11:38 Name argument
12:11 Global vs Local Variables
13:56 Default variable
15:29 Document strings

Next Video:

Рекомендации по теме
Комментарии
Автор

If I could give thousands thumbs up, I Would do definitely!!!! I watched many many different videos about Functions but I didn't understand how it work, but this video explain everything

shipaak
Автор

#Qn1
a=int(input("enter base "))
b=int(input("enter height "))
def area(h, b)
triangle=(b*h)/2
return triangle
n=area(a, b)
print ("area of the triangle is:", n)

afrozkhan.
Автор

Q1:
def calculate_area(base, height):
area=1/2*base*height
return area




area_triangle=calculate_area(5, 7)



print("area of triangle is:", area_triangle)

Result: area of triangle is: 17.5

harshisree
Автор

As an absolute, absolute absolute beginner, I like the dishwasher comparison. Thanks

BN-hynd
Автор

>>> list1=[20, 30, 40]
>>> list2=[50, 60, 70]
>>> total=0
>>> def addt(a):
... global total
... for i in a:
... total+=i
... print(total)
...
>>> addt(list1)
90

weshop
Автор

Q2 solution
def calculate_area(base, height, shape='triangle'):
if shape == 'triangle':
area = 0.5*base*height
return area
elif shape == 'rectangle':
area_r = base *height
return area_r
else:
return area

print(calculate_area(5, 5))

miriamogembo
Автор

For Question No 3:

pattern = 0
def print_pattern(a):
for i in range(a):
for j in range(i + 1):
pattern = print("*", end=" ")
print()
return (pattern)


p = print_pattern(3)


I think it works :-)

devimalini
Автор

Thank you very much for these amazing lessons. I am able to understand each topic in Python. :D

mics
Автор

Thankyou so much for all this knowledge... You made it so easy... God bless u...

sobhagyashri
Автор

All the exercises below are giving desired output.


Exercise 1:
def area(b, h):
Tarea=1/2*b*h
return Tarea
b=int(input("Enter base: "))
h=int(input("Enter height: "))
ar=area(b, h)
print("base :", b)
print("height :", h)
print("area: ", ar)


Exercise 2:
def area(a, b, h):
if a==0:
Tarea = 1 / 2 * b * h
elif a==1:
Tarea = b * h
else:
print("Invalid Response, Default area will be 0")
Tarea = 0
return Tarea
a=int(input("Enter 0 for triangle and 1 for rectangle : "))
b=int(input("Enter base: "))
h=int(input("Enter height: "))
ar=area(a, b, h)
print("base :", b)
print("height :", h)
print("area: ", ar)


Exercise 3:
def star(n):
for i in range(1, n+1):
for j in range(1, i+1):
print("*", end= " ")
print()
return
n=int(input("Enter number of lines: "))
s=star(n)

indianonly
Автор

n = int(input("enter no of rows "))
for i in range(0, n)
for j in range(0, i+1):
print("*", end="")
print("\r")

afrozkhan.
Автор

Thank you for an excellent video that was very interesting to watch and clearly presented.

francismannion
Автор

This was extremely helpful.  Thank you for posting this.

BoxerDogs
Автор

1)def calculate_area(b, h):
area=(b*h)/2
return area
a=int(input('enter the base of tri:'))
b=int(input('enter the height of tri:'))
print ('The area is :', calculate_area(a,b))


2)def calculate_area(b, h, s=None):
if s == 4:
return b*h
elif s == 3:
return (b*h)/2
else:
return (b*h)/2
a=int(input('enter the base/lenght:'))
b=int(input('enter the height/breadth:'))
n=input('enter the no of sides:')
print ('The area is :', calculate_area(a,b,n))


3)def print_pattern(arg, n):
for i in range(arg+1):
print (i*n)
a=int(input('enter the int:'))
b=str(input('enter the string:'))
print( print_pattern(a, b))

pavi_thra_gowda
Автор

Hi Your explanation is one of the simplest to understand, Great keep it up. I have few suggestions and reqeuests1. Voice of few videos are not audible, can you do something. 2.Pl do more videos on Natural Language Processing (NLP) 3. Pl post more videos on machine learning algorithms. 3. Pl post more videos on R language 4. Kindly also post more videos on Python advance topics such as tkiner, Django ...etc. 5. Kindly post more videos on mongodb. Once again thanks for wonderful knowledgeable videos specially for Indian students. It is giving very much clarity. Can you suggest some good books or websites to explore python more ?

paragjp
Автор

pls also post the answers for every exercise, so that we can correct our mistakes and learn how easily we can write the code .

joshchowdary
Автор

thx bro finally i got how functions really work :)

estebanlopez
Автор

do you know how to solve for ascending order?

A function called IsAscending() will take in three integers, num1, num2 and num3 as parameters. The function will return the Boolean value of True if the numbers are in ascending order. Otherwise, the function will return False.

>>> IsAscending(1, 5, 9)
True
>>>IsAscending(1, 9, 5)
False

P-
Автор

please i need to check and download the answers of exercises, mentioned link don't contain the answers

mohamedsherif
Автор

Good day mister. How do i print the number 2 problem? i want to input the value on dimension and shape so i i can see how its work

shuwenwenshu