Python for Introductory Statistics: Lab WorkbookXYZ Homework Edition

⇩ Download ▾

3.2 Python Functions

Watch demo video

Example: Given a,b,ca, b, c, and the quadratic equation ax2+bx+C=0ax^2+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 −x2−x+10=0-x^2-x+10=0.

view answer

Lab 3.2.2

Use the quadra(a,b,c) function to solve x2+x+1=0x^2+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.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or any other accessibility standard. Every page is built against an automated accessibility gate, and our target for the published editions is WCAG 2.2 Level AA. If something is unusable, please tell us.