Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Python basics Topclass revision notes with mcqs and programs.

Rating
-
Sold
-
Pages
68
Uploaded on
14-08-2025
Written in
2025/2026

Hey!! I am Divit Sarkar,a student of IIT Kharagpur.I have uploaded my Python top class revision notes with all types of programs It also consists of mcqs and questions and answers which will definitely help with your exam preparation and will help you to score good marks.So pls have a look at it!!

Show more Read less
Institution
Course

Content preview

1. Python Program to Find the Square Root

For positive numbers

# Python Program to calculate the square root

# Note: change this value for a different result
num = 8

# To take the input from the user
#num = float(input('Enter a number: '))

num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))



For real or complex numbers

# Find square root of real or complex numbers
# Importing the complex math module
import cmath

num = 1+2j

# To take input from the user
#num = eval(input('Enter a number: '))

num_sqrt = cmath.sqrt(num)
print('The square root of {0} is
{1:0.3f}+{2:0.3f}j'.format(num,num_sqrt.real,num_sqrt.imag))



In this program, we use the sqrt() function in the cmath (complex math) module.
Note: If we want to take complex number as input directly, like 3+4j , we have to use
the eval() function instead of float() .

The eval() method can be used to convert complex numbers as input to
the complex objects in Python.

,2. Python Program to Calculate the Area of a Triangle
If a , b and c are three sides of a triangle. Then,

s = (a+b+c)/2
area = √(s(s-a)*(s-b)*(s-c))

Source Code

# Python Program to find the area of triangle

a=5
b=6
c=7

# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)



In this program, area of the triangle is calculated when three sides are given
using Heron's formula.
If you need to calculate area of a triangle depending upon the input from the
user, input() function can be used.


3. Python Program to Solve Quadratic Equation
The standard form of a quadratic equation is:


ax2 + bx + c = 0, where
a, b and c are real numbers and
a≠0

The solutions of this quadratic equation is given by:

,(-b ± (b ** 2 - 4 * a * c) ** 0.5) / 2 * a

Source Code

# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module
import cmath

a=1
b=5
c=6

# calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))


Output

Enter a: 1
Enter b: 5
Enter c: 6
The solutions are (-3+0j) and (-2+0j)

We have imported the cmath module to perform complex square root. First, we
calculate the discriminant and then find the two solutions of the quadratic equation.


4. Python Program to Swap Two Variables

Source Code: Using a temporary variable

# Python program to swap two variables

x=5
y = 10

, # To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')

# create a temporary variable and swap the values
temp = x
x=y
y = temp

print('The value of x after swapping: {}'.format(x))
print('The value of y after swapping: {}'.format(y))

Output

The value of x after swapping: 10
The value of y after swapping: 5

In this program, we use the temp variable to hold the value of x temporarily. We then
put the value of y in x and later temp in y . In this way, the values get exchanged.
Source Code: Without Using Temporary Variable

In Python, there is a simple construct to swap variables. The following code does the
same as above but without the use of any temporary variable.


x=5
y = 10

x, y = y, x
print("x =", x)
print("y =", y)

If the variables are both numbers, we can use arithmetic operations to do the same. It
might not look intuitive at first sight. But if you think about it, it is pretty easy to figure
it out. Here are a few examples

Addition and Subtraction

x=x+y
y=x-y
x=x-y

Written for

Institution
Secondary school
Course
School year
5

Document information

Uploaded on
August 14, 2025
Number of pages
68
Written in
2025/2026
Type
Class notes
Professor(s)
Saikat saha
Contains
All classes

Subjects

$3.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
divitsarkar

Get to know the seller

Seller avatar
divitsarkar
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
8 months
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions