Basics of Python-13

Hello, I'm Priya Chakraborty, a dedicated B.Tech student in Electrical Engineering at Siliguri Institute Of Technology. I'm an enthusiastic learner, constantly seeking to expand my skill set and knowledge base. With a solid foundation in programming languages such as C, R, Python, and MySQL, I'm poised to tackle complex technical challenges.
But my passions extend beyond the realm of engineering. I'm also an aspiring content writer, driven by a curiosity to communicate ideas, both technical and non-technical, in a way that captivates and educates.
My journey is defined by a relentless pursuit of self-improvement, coupled with strong communication skills. I believe in the power of continuous learning and the importance of sharing knowledge.
Day-13
Function :
Python function is a block of code to make the code easier and to do the specific task.
there are two types of functions in python -:
Built-in function
User-defined function
Basics syntax of function :
def function_name(parameter):
statement
return expression
Built-in Functions in python:-
This type of function in python comes along with python software automatically.
some examples of built-in functions are :
print()- used to print values to the console
len()- used to get the length of a string, list, tuple, dictionary, or set
type()- used to get the type of a variable
input()- used to get user input from the console
int()- used to convert a string or float to an integer
float()- used to convert a string or integer to a float
str()- used to convert a variable to a string
range()- used to generate a range of numbers
list()- used to convert a sequence (e.g. a tuple) to a list
max()- used to get the maximum value in a sequence.
User-defined Functions in python :
User-defined functions can be developed on their own.
An example of a user-defined function is:
- Function to add two numbers:
def add_numbers(x, y):
return x + y
the basic syntax is :
def function_name(parameter):
Let's solve a few problems on function:
- Write a python program to find odd and even numbers by using functions in python -
def is_even(num):
if num % 2 == 0:
return True
else:
return False
n = int(input("Enter a number: "))
if is_even(n):
print(n, "is even")
else:
print(n, "is odd")
output :


- Write a python program to print a calculator program by using the python function -
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Cannot divide by zero"
else:
return x / y
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
else:
print("Invalid input")
output :

More problems :
Write a function to find the maximum of three numbersWrite a function to check if a given year is a leap yearWrite a function to compute the factorial of a given numberWrite a function to reverse a stringWrite a function to check if a given string is a palindrome
In my next blog, you will get to know about the oops concept in python.
Thank you !!




