0

Why in need def in python

Hello guys i want to know why i need to do def cuz i can do everything without def

7th Mar 2021, 2:03 AM
L7M5
L7M5 - avatar
3 Respostas
+ 1
It sounds like you're asking why you need functions in Python since "def" is needed to define a function. How big have your Python programs become? I can see your confusion if the longest program you ever wrote was under 100 lines. Larger programs get far better organized and clearer when functionality is broken into reusable and clearly named units like functions, classes, modules, and packages. If you had a few 1000 lines of code or more than one .py file, not using a function would mean your Python code is a nearly incomprehensible mess.
7th Mar 2021, 3:14 AM
Josh Greig
Josh Greig - avatar
+ 1
You need def so that you can create your own function. Maybe you are not yet making your own function however you're using functions all the time. print( ), sum( ), sqrt( ) etc. all are built-in python functions. Why do you use them? Because it doesn't make sense to write all the code inside print function each time you want to print "hello world" or something to the screen. For the same reason and more, when you're creating and managing your programming project, you should make and use functions so that you can reuse them to save your time.
7th Mar 2021, 4:47 AM
Abir Hasan
Abir Hasan - avatar