Skip to content

Instantly share code, notes, and snippets.

@Ayy-Pee
Last active January 4, 2023 09:16
Show Gist options
  • Save Ayy-Pee/2faecdbabfcb379b7b3474d98a58db10 to your computer and use it in GitHub Desktop.
Save Ayy-Pee/2faecdbabfcb379b7b3474d98a58db10 to your computer and use it in GitHub Desktop.
Python functions that you can add to your code for basic calculations.
#Function to find if the number is Even or Odd
def evenodd(a):
if(a%2==0):
return(a,"is Even")
else:
return(a,"is Odd")
#Fuction to find if the number is Prime or Not
def prime(a):
x=True
count=2
while(count<=a/2):
if(a%count==0):
x=False
break
count+=1
if(x==True):
return(a,"is Prime number.")
else:
return(a,"is not a Prime number.")
#Function to add given numbers
def add(*numbers):
x=0
for i in numbers:
x=x+i
return(x)
#Function to multiply given numbers
def mul(*numbers):
x=1
for i in numbers:
x=x*i
return(x)
#Function to subtract 2 given numbers
def sub(a,b):
x=a-b
return(x)
#Function to divide 2 given numbers
def div(a,b):
x=a/b
return(x)
#Function to find avg of given numbers
def avg(*numbers):
x=0
for i in numbers:
x=x+i
return(x/len(numbers))
#Function to find factorial of a give number
def factorial(a):
if(a==0 or a==1):
return 1
else:
return a*factorial(a-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment