python defs code example

Example 1: python def

# You can make a function by using the [def] keyword
def foo():
	print("Hello")

# You can run it by calling it as such
foo()

def add(numA, numB):
  print(numA+numB)
  # or 
  return numA+numB

num = add(1, 5)
print(num)

Example 2: def function in python

OK, basically def function() is a block where you will have one task that you can repeat all over again in the code to make it look short and clean

Example 3: how to use def in python

def functionName(variable):
  //function content

Example 4: functions in python

#Functions
#Functions are followed by the 'def' keyword
#Name your function
def myfunc():
  a = 'This is a func'
  
#Calling the function  
myfunc()
print(myfunc())

Example 5: explain def in python

def greet(name):
    """
    This function greets to
    the person passed in as
    a parameter
    """
    print("Hello, " + name + ". Good morning!")

Example 6: python def

#Use this to shortern a task that you may want to repeat
#Used for making custom commands
def paste(a, b)
	for i in b:
    	print(a)
      
      
while True:
  paste("Python is Cool!!!", 3)
  
#Output:
#Python is Cool!!!
#Python is Cool!!!
#Python is Cool!!!





## - ANOTHER EXAMPLE - ##





happy = "NEUTRAL"

def yes():
  happy="TRUE"
  print("Thank you!")
  
def no():
  happy="FALSE"
  print("That's not nice!")
  
answer=str(input("Do you like my new shoes?   (y/n)  ")
if answer=="yes" or "y" or "Yes" or "Y" or "YES":
  yes()
elif answer=="no" or "n" or "No" or "N" or "NO":
  no()

Tags:

Misc Example