Functions
A functions is a block organized and re-usable code and use to perform certain action.Pyhthon comes with lot of built in functions such as
print("hello world")
str(3) == "3"
int("5") == 15
username = input("Enter the user's name: ")
So now let's create our own functions.So for this you can use PyCharm editor I will provide the code here and you can run it directly using pycharm.
functions.py :
students = []
def get_students_titlecase():
student_titlecase = []
for student in students:
students_titlecase = student.title()
return students_titlecase
def print_students_titlecase():
students_titlecase = []
for student in students:
students_titlecase=student.title()
print(students_titlecase)
def add_student(name) :
students.append(name)
student_list = get_students_titlecase()
add_student("Abhinaw")
So actually I have by knowingly added some repeated code in functions which can be removed.Lets see
def print_students_titlecase():
students_titlecase = get_students_titlecase()
print(students_titlecase)
So if you write functions for single responsibility then it will be very helpful to write Unit Testing cases and code will be organized too.
No comments:
Post a Comment