DEV Community

Guru prasanna
Guru prasanna

Posted on

Python Day 2 -Functions-Meaning & types, Data type

name = input("What is your name?")
print("Welcome to Python", name)
Enter fullscreen mode Exit fullscreen mode

Use text editor to type syntax and save with extension .py and then to run this syntax in desktop page right click and select Open in Terminal

Image description

To run the program enter python3 (file name).py,Then click enter and enter a name.So that program will run.

Input
name = input("What is your name?")
print("Welcome to Python", name)

Output
What is your name?Guru
Welcome to Python Guru
Enter fullscreen mode Exit fullscreen mode

Functions:செயல்கூறு(Actions)
What is function?
Set of instructions for achieving a specific task with a name.
Example:
Watching_Meyyazhagan:
1. Laptop On
2. Netflix
3. Meyyazhagan
4. Play
Now in this above example for watching meyyazhagan movie the set of instructions are to be done like laptop on, open netflix, click meyyazhagan, Then click play button, But for instant we can say watching meyyazhagan which is a name includes all these set of actions.

()-Brackets represent some action is to be done. Inside brackets if anything is specified it is called as arguments or parameters,which is used to perform that function(action).Even without any arguments also a function can be performed.

ex:
eat(food)
sleep()
walk()
cook(rice, water)
swim(dress, pool)
write_exam(hallticket, pen, paper, pencil, scale, rubber, question_paper)

*Supporting Arguments: Comma Separated arguments.

input(),print()-Before brackets what action to be done will be mentioned in this input,print are functions to be performed.

Why function is required?
It is reusable code.No need to mention the full actions again and again we can use just one function.As already said example of watching meyyazhagan contains various actions to be done but we can just say watching_meyyazhagan.

Functions criteria:

  1. Function name should be meaningful.
  2. It should action names not just object name like laptop.
  3. It should not be in numbers.
  4. It should not contain space.
  5. Only "_" special character is allowed, not other special characters.

Types of functions:
predefined-Set of functions which are predefined while creating this language.Ex:input(),print().
Userdefined-set of functions which we can define whenever required.
Ex:

#Function Definition
def calculate(no1,no2):#parameter / argument
    print(no1+no2)
    print(no1-no2)
    print(no1*no2)
    print(no1/no2)

no1 = 120
no2 = 40
calculate(no1,no2) #Function Calling Statement

Enter fullscreen mode Exit fullscreen mode

The above mentioned user defined function can also be done without user defined function "calculate" as following.

no1 = 100
no2 = 5
print(no1+no2)
print(no1-no2)
print(no1*no2)
print(no1/no2)
Enter fullscreen mode Exit fullscreen mode

But the problem is in this every time we have to enter the action to be done. If we have defined function it will be easy to perform by python.

Top comments (0)