Login
📚 Python for Introductory Statistics: Lab Workbook
Chapters ▾

3.2 Python Functions

Watch demo video

Example: Given a,b,c, and the quadratic equation ax2+bx+C=0. write a function to solve such equations. See answer in the code-cell below.

def quadra(a,b,c):
  a,b,c = float(a),float(b),float(c)   # three lines in one
  if b**2-4*a*c < 0:
    return print("No real solutions")
  elif b**2-4*a*c == 0:
    x1 = -b/(2*a)
    return print("the solution is", x1)
  else:
    x1 = (-b-(b**2-4*a*c)**.5)/(2*a)
    x2 = (-b+(b**2-4*a*c)**.5)/(2*a)
    return print("the solutions are: ",x1," and ",x2)

Lab 3.2.1

Use the quadra(a,b,c) function above to solve x2x+10=0.

view answer

Lab 3.2.2

Use the quadra(a,b,c) function to solve x2+x+1=0.

view answer

Adapted from Python for Introductory Statistics: Student's Lab Workbook, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.