0% found this document useful (0 votes)
7 views3 pages

Python Basics: Operators & Programs

The document provides an overview of Python, including its definition, types of operators, operator precedence, and core libraries. It also includes sample programs for calculating the area of a triangle, distance between two points, voting eligibility, and finding the greatest number among three inputs. Each section contains explanations and example outputs for clarity.

Uploaded by

ABHAY MAITY
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Python Basics: Operators & Programs

The document provides an overview of Python, including its definition, types of operators, operator precedence, and core libraries. It also includes sample programs for calculating the area of a triangle, distance between two points, voting eligibility, and finding the greatest number among three inputs. Each section contains explanations and example outputs for clarity.

Uploaded by

ABHAY MAITY
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. What is Python?

Python is a programming language that is widely used in web applications,


software development, data science, and machine learning (ML).
Developers use Python because it is efficient and easy to learn and can
run on many different platforms.

2. What are the Operators in Python? State types of operators.


Operators in Python are special symbols or keywords that perform
operations on one or more operands. Operands are the values or variables
on which the operators act.
Python supports the following types of operators:
 Arithmetic Operators:
Used to perform mathematical calculations.
o + (Addition)
o - (Subtraction)
o * (Multiplication)
o / (Division)
o % (Modulo)
o ** (Exponentiation)
o // (Floor Division)
 Assignment Operators:
Used to assign values to variables. They can also combine an arithmetic
operation with assignment.
o = (Assign)
o += (Add and assign)
o -= (Subtract and assign)
o *= (Multiply and assign)
o /= (Divide and assign)
o %= (Modulo and assign)
o **= (Exponentiation and assign)
o //= (Floor-divide and assign)
 Comparison (Relational) Operators:
Used to compare two values and return a Boolean result (True or False).
o == (Equal to)
o != (Not equal to)
o > (Greater than)
o < (Less than)
o >= (Greater than or equal to)
o <= (Less than or equal to)
 Logical Operators:
Used to combine conditional statements.
o and (Logical AND)
o or (Logical OR)
o not (Logical NOT)

3. What is Operator Precedence?


In an expression with multiple operators, operator precedence determines
the grouping of terms in an expression and decides how an expression will
be evaluated. Certain operators have higher precedence than others.

4. . What are the Core Libraries in Python?


Python has a rich collection of libraries contribute by many authors. Each
library is related to one domain and has a huge collection of functions and
sub-packages. According to the requirement, for accessing a particular
function related to a domain, the user can access library or specific
functions using import statement in Python.
Ex – NumPy, pandas, matplotlib, seaborn,etc,.

5. Write a program to calculate area of a triangle using Heron's formula.

(Hint: Heron's: formula is given as: area = sqrt (5* (S-a)*(S-b) *(S-c)))
a = float(input("Enter the first side of the triangle : "))
b = float(input("Enter the second side of the triangle : "))
c = float(input("Enter the third side of the triangle : "))
print (a,b,c)
S = (a+b+c) /2
area: = (S*(S-a)(S-b)(S-c))**0.5
print ("Area = " + str (area))

OUTPUT
Enter the first side of the triangle : 12
Enter the second side of the triangle : 18
Enter the third side of the triangle : 10
12.0 18.0 10.0
Area = 56.5685424949

6. Write a program to calculate the distance between two points.

x1 = (int(input("Enter the x coordinate of the first point : ")))


y1 = (int(input("Enter the y coordinate of the first point : ")))
x2 = (int(input("Enter the x coordinate of the second point : ")))
y2 = (int(input("Enter the y coordinate of the second point: ")))
distance = ((x2-x1)**2+(y2-y1)**2)**0.5
print( "Distance = ")
print(distance)
OUTPUT
Enter the x coordinate of the first point : 8
Enter the y coordinate of the first point : 9
Enter the x coordinate of the second point : 10
Enter the y coordinate of the second point : 12
Distance = 3.60555127546

7. Write a program to determine whether a person is eligible to vote or not. If


he is not eligible, display how many years are left to be eligible.

age = int(input("Enter the age : "))


if(age>=18):
print("You are eligible to vote")
else:
yrs = 18 - age
print("You have to wait for another " + str(yrs) +" years to cast your
vote")

OUTPUT
Enter the age : 10
You have to wait for another 8 years to cast your vote

8. Write a program to find the greatest number from three numbers.

num1 = int(input("Enter the first number : "))


num2 = int(input("Enter the second number : "))
num3 = int(input("Enter the third number : "))
if(num1>num2):
if(num1>num3):
print (num1, "is greater than", num2, "and", num3)
else:
print (num3, "is greater than", num1, "and", num2)
elif(num2>num3):
print (num2, "is greater than", num1, "and", num3)
else:
print("The three numbers are equal")

OUTPUT:
Enter the first number : 13
Enter the second number : 43
Enter the third number : 25
43 is greater than 13 and 25

9.

You might also like