Solve a quadratic equation with one variable using Python.

This article will guide you on how to solve a quadratic equation with one variable ax^2 + bx + c = 0 (a, b != 0) using Python, an equation you learned in 9th grade.

This article will guide you on how to solve a quadratic equation with one variable

using Python, an equation you learned in 9th grade. Before we start writing the program in Python, we must first know how to solve a quadratic equation:

 

How to solve a quadratic equation with one variable

Solving a quadratic equation means finding the values ​​of x such that when x is substituted into the equation, the result is satisfied

.

Step 1: Calculate Δ=b 2 -4ac

Step 2: Compare Δ with 0

  • Δ < 0 => equation (1) has no solution
  • Δ = 0 => equation (1) has a double root

  • Δ > 0 => equation (1) has 2 distinct solutions, we use the following solution formula :

and

 

Solve quadratic equations with one variable using Python.

Step 1 : Allow the user to input three numbers: a, b, c, with the condition that a and b ≠ 0.

Step 2 : Calculate Delta (Δ)

Step 3 : Based on the analysis of the quadratic equation above, we can use the following formula to calculate the roots of the quadratic equation:

x = (-b ± sqrt(b^2 - 4ac)) / 2a where  sqrt() is the function for calculating the square root.

Python code to solve a quadratic equation with one variable:

import math print("Giải phương trình bậc 2: ax2 + bx + c = 0 (a, b khác 0)") print("Bạn đang làm bài tập Python trên QuanTriMang") print("============") # Nhập số a, b và kiểm tra điều kiện khác 0 a = float(input("Mời bạn nhập hệ số a: ")) while True: if a == 0: a = float(input("Số a phải khác 0. Mời nhập lại số a: ")) else: break b = float(input("Mời bạn nhập hệ số b: ")) while True: if b == 0: b = float(input("Số b phải khác 0. Mời nhập lại số b: ")) else: break # Nhập số c c = float(input("Mời bạn nhập hệ số c: ")) # Tính Delta delta = b**2 - 4 * a * c # Tìm nghiệm của phương trình if delta < 0: print("Phương trình vô nghiệm") elif delta == 0: print("Phương trình có nghiệm kép x1 = x2 = ", -(b / (2 * a)) ) else: print("Phương trình có hai nghiệm phân biệt:") print("x1 = ", (-(b) + math.sqrt(delta))/(2*a) ) print("x2 = ", (-(b) - math.sqrt(delta))/(2*a) )

Try running the code above using QuanTriMang's Python Online tool and see the results! Here are some examples you can try:

  • (a=1, b=-3, c=2)
  • (a=1, b=1, c=-6)
Related posts
  • Multiple choice test on Python - Part 11

    python's multiple-choice questions series help you update and review useful knowledge for your work and learn your python programming language.
  • Multiple choice quiz about Python - Part 1

    python is an object-oriented, high-level, powerful, easy-to-learn programming language. to help you read more interesting knowledge about python programming language, below the network administrator will send you a very interesting multiple choice questionnaire on this topic. please try.
  • Multiple choice quiz about Python - Part 4

    continue python's thematic quiz, part 4 goes back to the topic core data type - standard data types in python. let's try with quantrimang to try the 10 questions below.
Other Python articles
  • The pass command in Python

    trong bài trước bạn đã biết cách dùng lệnh continue và lệnh break trong python để thay đổi vòng lặp. trong phần này chúng ta sẽ tiếp tục một lệnh khác, lệnh pass trong python, dùng như một placeholder (trình giữ chỗ) cho việc thực thi các hàm, vòng lặp,... trong tương lai.
  • Numeric data types in Python

    kiểu dữ liệu số trong python là gì? cách dùng kiểu dữ liệu số trong python như thế nào? hãy cùng tipsmake.com.com tìm hiểu nhé!
  • How to write commands, indent, and comments in Python

    chú thích trong python là gì? bài viết sẽ cho bạn biết mọi điều về cách viết ghi chú trong python.
  • Multiple choice test on if else and loops in Python

    trắc nghiệm python đại học là điều bạn đang kiếm tìm? bài viết sẽ tổng hợp cho bạn những bài trắc nghiệm về if else và vòng lặp trong python.
  • How to calculate the area of ​​a square in Python

    hướng dẫn bên dưới giúp bạn tìm diện tích của một hình vuông cho trước trong python bằng các phương pháp khác nhau.
  • How to calculate the area of ​​a circle in Python

    nhiệm vụ tính diện tích hình tròn trong python bao gồm lấy bán kính làm đầu vào, áp dụng công thức toán học để tính diện tích hình tròn và hiển thị kết quả.
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup