0% found this document useful (0 votes)
17 views55 pages

Python Installation and Basic Operations Guide

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

Python Installation and Basic Operations Guide

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

4 Python

Final practical exam

[Link] up or install or configure the python editor VS code or pycharam or IDLE


Installing and Setting up Visual Studio Code (VS Code)
[Link]:
To install and set up Visual Studio Code for Python programming in Windows.
:
1. Open a web browser and type “Download Visual Studio Code.”
2. Click the first link — [Link]
3. Click Download for Windows.
4. After it downloads, open the setup file. Steps
5. Keep clicking Next → Next → Install → Finish.
6. Open Visual Studio Code from your desktop.
7. Click on the Extensions icon (four small squares) on the left.
8. Type “Python” in the search box and click Install on the Microsoft extension.
9. Click File → New File, then save it as [Link].890
10. Type a small code:
11. print("Hello from VS Code!")
12. Click on the Run button at the top to see the output.
Result:
VS Code is successfully installed and set up for Python programming in Windows.

[Link] and Setting up PyCharm


Aim:0
To install and set up the PyCharm IDE for Python development in Windows.
Steps:
1. Open a web browser and type “Download PyCharm for Windows.”
2. Click the first link — [Link]
3. Click Download under Community Edition (Free).
4. Open the downloaded setup file.
5. Click Next → Next → Install → Finish.
6. Open PyCharm after installation.
7. Click New Project, select the location, and click Create.
8. Inside the project, right-click → New → Python File.
9. Name it [Link].
10. Type:
11. print("Welcome to PyCharm!")
12. Click Run or press Shift + F10 to execute.
Result:
PyCharm is successfully installed and ready for Python programming in Windows.

[Link] and Setting up Python IDLE


Aim:
To install and set up Python IDLE editor in Windows.
Steps:
1. Open any web browser (like Google Chrome).
2. In the search bar, type “Download Python for Windows.”
3. Click on the first link — [Link]
4. Click on Download Python (latest version) for Windows.
5. After it downloads, open the setup file.
6. Tick the box “Add Python to PATH.”
7. Click on Install Now and wait for it to finish.
8. Open IDLE (Python) from the Start Menu.
9. Type a sample program:
10. print("Hello, Python!")
11. Press F5 or select Run → Run Module to execute.
Result:
Python and IDLE are successfully installed and ready to use in Windows.
2. Input/output
[Link]: Program to Perform Arithmetic Operations in Python
Step 1: Start the program.
Step 2: Initialize two variables a and b with values 10 and 9.
Step 3: Calculate the sum of two numbers using c = a + b.
Step 4: Display the result using the print statement.
Step 5: Calculate the subtraction using c = a - b.
Step 6: Display the subtraction result.
Step 7: Calculate the multiplication using c = a * b.
Step 8: Display the multiplication result.
Step 9: Calculate the division using c = a / b.
Step 10: Display the division result.
Step 11: Calculate the modulus using c = a % b to get the remainder.
Step 12: Display the modulus result.
Step 13: Calculate the floor division using c = a // b.
Step 14: Display the floor division result.
Step 15: Calculate the exponent using c = a ** b.
Step 16: Display the exponent result.
Step 17: Stop the program.

a=10
b=9
c=a+b
print("The sum of two number is : ",c)

a=10
b=9
c=a-b
print("The sub of two number is : ",c)

a=10
b=9
c=a*b
print("The mul of two number is : ",c)

a=10
b=9
c=a/b
print("The div of two number is : ",c)

a=10
b=9
c=a%b
print("The modules of two number is : ",c)

a=10
b=9
c=a//b
print("The floor division of two number is : ",c)

a=10
b=9
c=a**b
print("The Exponent of two number is : ",c)
op:-
The sum of two number is : 19
The sub of two number is : 1
The mul of two number is : 90
The div of two number is : 1.1111111111111112
The modules of two number is : 1
The floor division of two number is : 1
The Exponent of two number is : 1000000000

[Link]: Program to Perform Arithmetic Operations Using Input and Output


Statements
Step 1: Start the program.
Step 2: Read two numbers from the user using the input() function and convert them to
integers using int().
Step 3: Perform addition of the two numbers using c = a + b.
Step 4: Display the result using the print() statement.
Step 5: Perform subtraction using c = a - b and display the result.
Step 6: Perform multiplication using c = a * b and display the result.
Step 7: Perform division using c = a / b and display the result.
Step 8: Perform modulus operation using c = a % b and display the result.
Step 9: Perform floor division using c = a // b and display the result.
Step 10: Perform exponent operation using c = a ** b and display the result.
Step 11: Stop the program.
a=int(input("Enter a Variable : ",))
b=int(input("enter a variable : ",))
c=a+b
print("The sum of two number is : ",c)

a=int(input("Enter a Variable : ",))


b=int(input("enter a variable : ",))
c=a-b
print("The sub of two number is : ",c)

a=int(input("Enter a Variable : ",))


b=int(input("enter a variable : ",))
c=a*b
print("The mul of two number is : ",c)

a=int(input("Enter a Variable : ",))


b=int(input("enter a variable : ",))
c=a/b
print("The div of two number is : ",c)

a=int(input("Enter a Variable : ",))


b=int(input("enter a variable : ",))
c=a%b
print("The modules of two number is : ",c)

a=int(input("Enter a Variable : ",))


b=int(input("enter a variable : ",))
c=a//b
print("The floor division of two number is : ",c)

a=int(input("Enter a Variable : ",))


b=int(input("enter a variable : ",))
c=a**b
print("The exponent of two number is : ",c)

Enter a Variable : 13
enter a variable : 14
The sum of two number is : 27
Enter a Variable : 20
enter a variable : 4
The sub of two number is : 16
Enter a Variable : 98
enter a variable : 8
The mul of two number is : 784
Enter a Variable : 77
enter a variable : 87
The div of two number is : 0.8850574712643678
Enter a Variable : 45
enter a variable : 45
The modules of two number is : 0
Enter a Variable : 76
enter a variable : 3
The floor division of two number is : 25
Enter a Variable : 47
enter a variable : 35
The exponent of two number is :
33375288443717156087424983475677821467175245957737145158543

[Link] Area of Triangle


Algorithm:
1. Start
2. Read base and height from user
3. Calculate area = ½ × base × height
4. Display the result
5. Stop
#area of triangle
base=float(input("Enter the base value of triangle : "))
height=float(input("Enter the height value of triangle : "))
area=0.5*base*height
print("The area of triangle is : ",area)
op:
Enter the base value of triangle : 10
Enter the height value of triangle : 12
The area of triangle is : 60.0

[Link] Area of Circle


Algorithm:
1. Start
2. Read radius from user
3. Calculate area = π × r²
4. Display the area
5. Stop
#area of circle
r=float(input("Enter the radius value of circle :"))
area=3.14*r**2
print("The area of circle is : ",area)
Op:
Enter the radius value of circle :4
The area of circle is : 50.24

[Link] Simple Interest


Algorithm:
1. Start
2. Read principal, rate, and time
3. Calculate simple interest = (P × T × R) / 100
4. Display the result
5. Stop
p=float(input("Enter the principle amount : "))
r=float(input("Enter the rate of interest : "))
t=float(input("Enter the time taken : "))
SI=p*r*t/100
print("The simple intrest is : ",SI)

op:
Enter the principle amount : 23
Enter the rate of interest : 43
Enter the time taken : 55
The simple intrest is : 543.95

[Link] of Rectangle
Algorithm:
1. Start
2. Read length and breadth
3. Calculate area = length × breadth
4. Display the area
5. Stop
Code:
length = float(input("Enter the length of rectangle: "))
breadth = float(input("Enter the breadth of rectangle: "))
area = length * breadth
print("Area of Rectangle:", area)
Output:
Enter the length of rectangle: 8
Enter the breadth of rectangle: 4
Area of Rectangle: 32.0

[Link] of Square
Algorithm:
1. Start
2. Read side
3. Calculate area = side × side
4. Display the result
5. Stop
Code:
side = float(input("Enter the side of square: "))
area = side * side
print("Area of Square:", area)
Output:
Enter the side of square: 5
Area of Square: 25.0

[Link] of Rectangle
Algorithm:
1. Start
2. Read length and breadth
3. Calculate perimeter = 2 × (length + breadth)
4. Display the result
5. Stop
Code:
length = float(input("Enter the length of rectangle: "))
breadth = float(input("Enter the breadth of rectangle: "))
perimeter = 2 * (length + breadth)
print("Perimeter of Rectangle:", perimeter)
Output:
Enter the length of rectangle: 8
Enter the breadth of rectangle: 4
Perimeter of Rectangle: 24.0

[Link] of Circle (Circumference)


Algorithm:
1. Start
2. Read radius
3. Calculate perimeter = 2 × π × r
4. Display the result
5. Stop
Code:
r = float(input("Enter the radius of circle: "))
perimeter = 2 * 3.14 * r
print("Circumference of Circle:", perimeter)
Output:
Enter the radius of circle: 7
Circumference of Circle: 43.96

[Link] of Parallelogram
Algorithm:
1. Start
2. Read base and height
3. Calculate area = base × height
4. Display the area
5. Stop
Code:
base = float(input("Enter the base of parallelogram: "))
height = float(input("Enter the height of parallelogram: "))
area = base * height
print("Area of Parallelogram:", area)
Output:
Enter the base of parallelogram: 6
Enter the height of parallelogram: 4
Area of Parallelogram: 24.0

[Link] of Rhombus
Algorithm:
1. Start
2. Read both diagonals (d1 and d2)
3. Calculate area = (d1 × d2) / 2
4. Display the area
5. Stop
Code:
d1 = float(input("Enter first diagonal: "))
d2 = float(input("Enter second diagonal: "))
area = (d1 * d2) / 2
print("Area of Rhombus:", area)
Output:
Enter first diagonal: 8
Enter second diagonal: 6
Area of Rhombus: 24.0

[Link] of Trapezium
Algorithm:
1. Start
2. Read base1, base2, and height
3. Calculate area = ½ × (base1 + base2) × height
4. Display the result
5. Stop
Code:
base1 = float(input("Enter base1: "))
base2 = float(input("Enter base2: "))
height = float(input("Enter height: "))
area = 0.5 * (base1 + base2) * height
print("Area of Trapezium:", area)
Output:
Enter base1: 6
Enter base2: 8
Enter height: 4
Area of Trapezium: 28.0

[Link] of Cube
Algorithm:
1. Start
2. Read side of cube
3. Calculate area = 6 × side²
4. Display the result
5. Stop
Code:
side = float(input("Enter the side of cube: "))
area = 6 * (side ** 2)
print("Area of Cube:", area)
Output:
Enter the side of cube: 4
Area of Cube: 96.0

[Link] Two Numbers (Using a Temporary Variable)


Algorithm:
1. Start
2. Input two numbers a and b
3. Swap using a temporary variable
4. Print the new values of a and b
5. Stop
Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
temp = a
a=b
b = temp
print("After swapping:")
print("a =", a)
print("b =", b)

[Link] Temperature from Celsius to Fahrenheit


Formula:
F = (C × 9/5) + 32
Code:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)

[Link] Days into Years, Weeks, and Days


Code:
days = int(input("Enter number of days: "))
years = days // 365
weeks = (days % 365) // 7
remaining_days = (days % 365) % 7
print("Years:", years)
print("Weeks:", weeks)
print("Days:", remaining_days)

[Link] Average of Three Numbers


Code:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
average = (a + b + c) / 3
print("Average of three numbers is:", average)

18. Convert Kilometers into Meters and Centimeters


Code:
km = float(input("Enter distance in kilometers: "))
meters = km * 1000
centimeters = meters * 100
print("Meters:", meters)
print("Centimeters:", centimeters)

[Link] Hours into Minutes and Seconds


Code:
hours = int(input("Enter time in hours: "))
minutes = hours * 60
seconds = minutes * 60
print("Minutes:", minutes)
print("Seconds:", seconds)

2. Conditional Statements
[Link] whether a character is a vowel or consonant
Algorithm:
1. Start
2. Read a letter from the user
3. Check if the letter is one of: a, e, i, o, u
4. If yes → print “Vowel”
5. Otherwise → print “Consonant”
6. Stop
Code:
letter= str(input("Enter a letter: "))
if letter in ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"):
print("The letter is vowel")
else:
print("The letter is consonant")
Output:
Enter a letter: e
The letter is vowel
or
Enter a letter: B
The letter is consonant

[Link] Check Whether a Number is Even or Odd


Algorithm:
1. Start
2. Input a number from the user and store it in the variable num.
3. Check if num % 2 == 0
• If True, then display “The number is Even”.
• Otherwise, display “The number is Odd”.
4. Stop
Program:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is Even")
else:
print("The number is Odd")
Output :
Enter a number: 5
The number is Odd
[Link]: Find the Largest of Three Numbers
Algorithm:
1. Start
2. Input three numbers a, b, and c.
3. If a > b and a > c, print “a is largest”.
4. Else if b > c, print “b is largest”.
5. Else, print “c is largest”.
6. Stop
Python Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b and a > c:
print("The largest number is:", a)
elif b>a amd b > c:
print("The largest number is:", b)
else:
print("The largest number is:", c)
Output:
Enter first number: 5
Enter second number: 9
Enter third number: 2
The largest number is: 9

[Link]: Check Positive, Negative or Zero


Algorithm:
1. Start
2. Input a number num.
3. If num > 0, print “Positive”.
4. Else if num < 0, print “Negative”.
5. Else, print “Zero”.
6. Stop
Python Code:
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")
Output:
Enter a number: -4
Negative number

[Link]: Grading System


Algorithm:
1. Start
2. Input marks.
3. If marks ≥ 90, print “Grade A”.
4. Else if marks ≥ 75, print “Grade B”.
5. Else if marks ≥ 50, print “Grade C”.
6. Else, print “Fail”.
7. Stop
Python Code:
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Grade: Fail")
Output:
Enter your marks: 82
Grade: B

[Link]: Check Leap Year or Not


Algorithm:
1. Start
2. Input a year.
3. If (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0), print “Leap year”.
4. Else, print “Not a leap year”.
5. Stop
Python Code:
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")
Output:
Enter a year: 2024
Leap year

[Link]: Check Voting Eligibility


Algorithm:
1. Start
2. Input age.
3. If age ≥ 18, print “Eligible to vote”.
4. Else, print “Not eligible to vote”.
5. Stop
Python Code:
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote")
else:
print("You are not eligible to vote")
Output:
Enter your age: 16
You are not eligible to vote

[Link]: Check Divisible by 5 and 11


Algorithm:
1. Start
2. Input a number.
3. If num % 5 == 0 and num % 11 == 0, print “Divisible by both”.
4. Else, print “Not divisible by both”.
5. Stop
Python Code:
num = int(input("Enter a number: "))
if num % 5 == 0 and num % 11 == 0:
print("Divisible by both 5 and 11")
else:
print("Not divisible by both 5 and 11")
Output:
Enter a number: 55
Divisible by both 5 and 11

[Link]: Electricity Bill


Algorithm:
1. Start
2. Input units consumed.
3. If units ≤ 100 → rate = ₹2/unit.
4. If 101–200 → first 100 at ₹2, rest at ₹3.
5. Above 200 → first 200 as above, rest at ₹5.
6. Calculate total bill and print it.
7. Stop
Python Code:
units = int(input("Enter electricity units used: "))
if units <= 100:
bill = units * 2
elif units <= 200:
bill = 100 * 2 + (units - 100) * 3
else:
bill = 100 * 2 + 100 * 3 + (units - 200) * 5
print("Total Bill Amount:", bill)
Output:
Enter electricity units used: 250
Total Bill Amount: 850

[Link]: Discount System


Algorithm:
1. Start
2. Input total purchase amount.
3. If amount ≥ 1000 → discount 10%.
4. Else if amount ≥ 500 → discount 5%.
5. Else → no discount.
6. Calculate and print final amount.
7. Stop
Python Code:
amount = float(input("Enter purchase amount: "))
if amount >= 1000:
discount = amount * 0.10
elif amount >= 500:
discount = amount * 0.05
else:
discount = 0
print("Discount:", discount)
print("Final Amount:", amount - discount)
Output:
Enter purchase amount: 800
Discount: 40.0
Final Amount: 760.0

Python Data Types


age=68
print(type(age))

number=703.5
print(type(number))

word="sakshi"
print(type(word))

letter="A"
print(type(letter))
a=True
print(type(a))
op:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'str'>
<class 'bool'>
Or

age = input("Enter your age: ")


print(type(age))
number = input("Enter a number: ")
print(type(number))
word = input("Enter a word: ")
print(type(word))
letter = input("Enter a letter: ")
print(type(letter))
a = input("Enter True or False: ")
print(type(a))

Loops in Python
Loops are used to repeat a block of code multiple times without writing it again and again.
1. for loop

• Print numbers from 1 to N

• Print even numbers or odd numbers

• Find factorial of a number

• Sum of first N numbers


• Check prime number

• Display multiplication table

Print 1 to 10 Numbers (for loop)


Algorithm:
1. Start
2. Repeat loop variable i from 1 to 10
3. Print i each time
4. Stop
Code:
for i in range(1, 11):
print(i)
Output:
1
2
3
4
5
6
7
8
9
10

Sum of First N Numbers


Algorithm:
1. Start
2. Read n
3. Set sum = 0
4. For i = 1 to n
→ sum = sum + i
5. Print sum
6. Stop
Program:
n = int(input("Enter a number: "))
sum = 0
for i in range(1, n + 1):
sum += i
print("Sum =", sum)
Output:
Enter a number: 5
Sum = 15

Types of Numbers (Basic Concepts)

Type Meaning Example

Natural numbers Counting numbers starting from 1 1, 2, 3, 4, 5…

Whole numbers All natural numbers + 0 0, 1, 2, 3, 4…

Even numbers Divisible by 2 (remainder 0) 2, 4, 6, 8…

Odd numbers Not divisible by 2 (remainder 1) 1, 3, 5, 7…

Prime numbers Greater than 1 and divisible only by 2, 3, 5, 7, 11…


1 and itse]lf

Composite numbers Greater than 1 and have more than 4, 6, 8, 9, 10…


2 factors

Perfect numbers Sum of factors (excluding itself) = 6 → 1+2+3=6


number

Armstrong numbers Sum of cubes of digits = number 153 →


orNarcissistic number 1³+5³+3³=153

Palindrome numbers Number reads same backward & 121, 1331,


forward 12321
Print Even Numbers from 1 to 10 (for loop)
Algorithm:
1. Start
2. Loop from 1 to 10
3. If number divisible by 2 → print it
4. Stop
Code:
for i in range(1, 11):
if i % 2 == 0:
print(i)
Output:
2
4
6
8
10

Prime Number Check (for loop)

Algorithm:

1. Start

2. Input a number and store it in variable num

3. Check if num > 1

If False, print “Number is neither prime nor composite” and stop

4. If True,

Repeat i from 2 to (num - 1)

For each value of i, check if num % i == 0

If condition is True, print “Number is not a prime number”


and exit the loop
If the loop completes without finding any divisor, print “Number is 0000a prime
number”

5. Stop

Code:

num = int(input("Enter a number: "))

if num > 1:

for i in range(2, num):

if num % i == 0:

print(num, "is not a prime number")

break

else:

print(num, "is a prime number")

else:

print(num, "is neither prime nor composite")Output:

Enter a number: 7

Prime number

To Find the Factorial of a Number using For Loop

Algorithum

1. Start

2. Input a number and store it in variable num

3. Initialize a variable fact = 1

4. Repeat a loop for i in range(1, num + 1):

o Multiply fact = fact * i

5. After loop ends, print the value of fact as the factorial of the number

6. Stop

Program:

num = int(input("Enter a number: "))


fact = 1

for i in range(1, num + 1):

fact = fact * i

print("The factorial of", num, "is", fact)

Output:

Enter a number: 5

The factorial of 5 is 120

Multiplication Table (for loop)

Algorithm:

1. Start

2. Read a number n

3. Repeat i from 1 to 10

4. Print n * i

5. Stop

Code:

n = int(input("Enter a number: "))

for i in range(1, 11):

print(n, "x", i, "=", n * i)

Output:

Enter a number: 5

5x1=5

5 x 2 = 10…

5 x 10 = 50

Pattern Printing (for loop)


Algorithm:
1. Start
2. Read number of rows n
3. For each row i from 1 to n, print * i times
4. Stop
Code:
n = int(input("Enter number of rows: "))
for i in range(1, n + 1):
print("*" * i)
Output:
Enter number of rows: 5
*
**
***
****
*****

2. while loop

• Factorial using while loop

• Reverse a number

• Sum of digits of a number

• Palindrome number

• Print numbers until user stops

Sum of First 10 Numbers (while loop)


Algorithm:
1. Start
2. Initialize i = 1 and sum = 0
3. While i <= 10, add i to sum
4. Increase i by 1
5. Print total sum
6. Stop
Code:
i=1
sum = 0
while i <= 10:
sum = sum + i
i=i+1
print("Sum =", sum)
Output:
Sum = 55

Factorial of a Number (while loop)


Algorithm:
1. Start
2. Read a number n
3. Set fact = 1 and i = 1
4. While i <= n, multiply fact = fact * i
5. Increase i by 1
6. Print fact
7. Stop
Code:

n = int(input("Enter a number: "))


fact = 1
i=1
while i <= n:
fact = fact * i
i=i+1
print("Factorial =", fact)
Output:
Enter a number: 5
Factorial = 120

Reverse of a Number (while loop)


Algorithm:
1. Start
2. Read number n
3. Set rev = 0
4. While n > 0, get last digit → n % 10
5. Add to reverse → rev = rev * 10 + digit
6. Remove last digit → n = n // 10
7. Print reverse
8. Stop
Code:
n = int(input("Enter a number: "))
rev = 0
while n > 0:
digit = n % 10
rev = rev * 10 + digit
n = n // 10
print("Reversed number =", rev)
Output:
Enter a number: 123
Reversed number = 321

Count Digits in a Number (while loop)


Algorithm:
1. Start
2. Read number n
3. Set count = 0
4. While n > 0, remove last digit → n = n // 10
5. Increase count by 1
6. Print count
Code:
n = int(input("Enter a number: "))
count = 0
while n > 0:
count += 1
n = n // 10
print("Total digits =", count)
Output:
Enter a number: 4567
Total digits = 4

Palindrome Number
Algorithm:
Start
Input a number or text and store it in a variable text
Reverse the string using slicing:
→ reversed_text = text[::-1]
Display the reversed text using print(reversed_text)
Stop
Program:
text=input("Enter a number: ",)
reversed_text=text[::-1]
print(reversed_text)
Output:
Enter a number: mom
Mom

num = int(input("Enter a number: "))


temp = num
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num = num // 10
if temp == rev:
print("The number is a Palindrome")
else:
print("The number is not a Palindrome")

Data Collections
• List ,Tuple ,Set, Dictionary, Indexing ,Slicing, All common operations ,Simple
programs.
1. LIST
Definition:
• Ordered
• Changeable (mutable)
• Allows duplicates
Example:
a = [10, 20, 30, 40]
Indexing:
print(a[0]) # 10
print(a[-1]) # 40
Slicing:
print(a[1:3]) # [20, 30]
print(a[:3]) # [10,20,30]
Operations:
[Link](50)
[Link](1, 99)
[Link](20)
[Link]()
[Link]()
[Link]()
Program:
a=[10,20,30,40]
[Link](50)
[Link](20)
print(a)
Algorithm
1. Start
2. Initialize a list with predefined values
3. Loop forever:
o Display menu: Add, Delete, Display, Sort, Reverse, Search, Exit
o Read user choice
o If choice = 1:
▪ Read a value
▪ Append value to list
o Else if choice = 2:
▪ Delete last element using pop()
o Else if choice = 3:
▪ Display the list
o Else if choice = 4:
▪ Sort the list
o Else if choice = 5:
▪ Reverse the list
o Else if choice = 6:
▪ Read search value
▪ If value in list → print found
▪ Else → print not found
o Else if choice = 7:
▪ Print exiting
▪ Break loop
o Else print invalid choice
4. Stop
Code:-
l=[]
n = int(input("How many elements you want to add initially? "))
for i in range(n):
ele = int(input("Enter element: "))
[Link](ele)
print(l)
while True:
print("[Link] [Link] [Link] [Link] [Link] [Link] [Link]")
choice = int(input("ENTER YOUR CHOICE: "))
if choice == 1:
add = int(input("Enter element to add: "))
[Link](add)
print(l)
elif choice==2:
rem=int(input("Enter a element to remove : ",))
if rem in l:
[Link](rem)
print("The element is removed from the list")
else:
print("The element is not found ")
elif choice == 3:
print(l)
elif choice == 4:
[Link]()
print(l)
elif choice == 5:
[Link]()
print(l)
elif choice == 6:
item = int(input("Enter element to search: "))
if item in l:
print("Item is present")
else:
print("Item not found")
elif choice == 7:
print("Exiting...")
break
else:
print("Invalid choice, enter between 1–7")

2. TUPLE
Definition:
• Ordered
• Not changeable (immutable)
• Allows duplicates
Example:
t = (10, 20, 30, 40)
Indexing:
print(t[0]) # 10
Slicing:
print(t[1:3]) # (20, 30)
Tuple can't change:
This is wrong ↓
[Link](20)
Program:
t = (5, 10, 15, 20)
print("Length = ", len(t))
print("Max = ", max(t))
print("Min = ", min(t))
Algorithm: Tuple Operations
1. Start
2. Define a tuple t with some integer values
3. Display the tuple
4. Repeat the following steps
a. Display menu
1. Length
2. Maximum
3. Minimum
4. Display
5. Search
6. Exit
b. Read the user's choice
c. If choice = 1
Print length of tuple using len(t)
d. Else if choice = 2
Print maximum value using max(t)
e. Else if choice = 3
Print minimum value using min(t)
f. Else if choice = 4
Display entire tuple
g. Else if choice = 5
Read item
If item present in tuple
Print “Item found”
Else
Print “Item not found”
h. Else if choice = 6
Print “Exiting” and break the loop
i. Else
Print “Invalid choice”
7. Stop
Code:-
temp = []
n = int(input("Enter how many elements you want: "))
for i in range(n):
ele = int(input(f"Enter element "))
[Link](ele)
t = tuple(temp)
print("Your tuple is:", t)
while True:
print("[Link] [Link] [Link] [Link] [Link] [Link]")
choice = int(input("Enter your choice : "))
if choice == 1:
print("The length of the tuple is :", len(t))
elif choice == 2:
print("The maximum number in the tuple is :", max(t))
elif choice == 3:
print("The minimum number in the tuple is :", min(t))
elif choice == 4:
print(t)
elif choice == 5:
item = int(input("Enter an item to search : "))
if item in t:
print("The item is present")
else:
print("The item is not present")
elif choice == 6:
print("The program is exiting")
break
else:
print("Invalid choice! Enter between 1 to 6")

3. SET
Definition:
• Unordered
• No duplicates
• Mutable
Example:
s = {10, 20, 30, 10}
print(s) # {10,20,30}
Operations:
[Link](40)
[Link](20)
[Link]([50,60])
Set operations (very important):
A = {1,2,3}
B = {3,4,5}
print([Link](B)) # {1,2,3,4,5}
print([Link](B)) # {3}
print([Link](B)) # {1,2}
Algorithm: Start
1. Create an empty set s
2. Repeat the following steps:
a. Display menu:
1. Add
2. Delete
3. Display
4. Search
5. Exit
b. Read the user’s choice
c. If choice = 1
Ask user to enter an element
Add the element to the set using add()
d. Else if choice = 2
If set is not empty
Remove an element using pop()
Else
Display “Set is empty”
e. Else if choice = 3
Display the set
f. Else if choice = 4
Read the search item
If item present in set
Display “Item present”
Else
Display “Item not present”
g. Else if choice = 5
Display “Exiting”
Break the loop
h. Else
Display “Invalid choice”
3. Stop
Code:
s=set()
n = int(input("How many elements you want to add initially? "))
for i in range(n):
ele = int(input("Enter element: "))
[Link](ele)
print(s)
while True:
print("[Link] [Link] [Link] [Link] 5EXIT")
choice=int(input("Enter your choice : "))
if choice==1:
add=int(input("Enter a element which you wat to be added :",))
[Link](add)
print(s)
elif choice==2:
rem = int(input("Enter an element which you want to remove : "))
if rem in s:
[Link](rem)
print("The element removed from the list : ", s)
else:
print("The element not in the list : ", s)
elif choice==3:
print(s)
elif choice==4:
item=int(input("Enter a item which you want to search : ",))
if item in s:
print("THe item is present in the set ")
else:
print("The item is not present in the set ")
elif choice==5:
print("The program is exiting ")
break
else:
print("The choice is invalid choose between 1 to 5 ")

4. DICTIONARY
Definition:
• Key:Value pairs
• Mutable
• No duplicates in keys
Example:
d = {"name": "Sakshi", "age": 20}
Access:
print(d["name"])
Add / Update:
d["city"] = "Bengaluru"
Delete:
[Link]("age")
Loop:
for x,y in [Link]():
print(x, y)
Algorithm: Perform Dictionary Operations Using User Choice
1. Start
2. Create a dictionary d with some initial key-value pairs
3. Repeat the following steps:
a. Display the menu:
1. Add
2. Delete
3. Display Keys
4. Display Values
5. Display Both
6. Sort
7. Exit
b. Read user choice
c. If choice = 1
Read a key and value
Add them to the dictionary
d. Else if choice = 2
If dictionary not empty
Remove last inserted item using popitem()
Else
Display “Dictionary is empty”
e. Else if choice = 3
Display all keys
f. Else if choice = 4
Display all values
g. Else if choice = 5
Display all key–value pairs
h. Else if choice = 6
Sort dictionary items using sorted()
i. Else if choice = 7
Display “Exiting” and break loop
j. Else
Display “Invalid choice”
4. Stop
Code:-
d = {}
n = int(input("How many key-value pairs? "))
for i in range(n):
k = input("Enter key: ")
v = input("Enter value: ")
d[k] = v
while True:
print("[Link] [Link] [Link] KEYS [Link] VALUES [Link] BOTH [Link] BOTH
[Link]")
choice = int(input("Enter your choice: "))
if choice == 1:
key = input("Enter your key: ")
value = int(input("Enter your value: "))
d[key] = value
print(d)
elif choice == 2:
if len(d) > 0:
[Link]()
print(d)
else:
print("Dictionary is empty!")
else:
print("Dictionary is empty, nothing to delete")
elif choice == 3:
print([Link]())
for x in [Link]():
print(x)
elif choice == 4:
print([Link]())
for x in [Link]():
print(x)
elif choice == 5:
print([Link]())
for x in [Link]():
print(x)
elif choice == 6:
print( sorted([Link]()))
elif choice == 7:
print("Exiting the program")
break
else:
print("Invalid choice")

5. INDEXING
Used to access elements:
a = [10,20,30]
print(a[1]) # 20
print(a[-1]) # 30

6. SLICING
Get part of a list/string/tuple:
a = [10,20,30,40,50]
print(a[1:4]) # [20,30,40]
print(a[:3]) # [10,20,30]
print(a[2:]) # [30,40,50]
print(a[::2]) # [10,30,50]

Array and String


Array
Algorithm:-
1. Start
2. Import the array module using
import array
3. Create an empty integer array:
arr = [Link]('i', [])
4. Repeat the following steps continuously (using a while loop):
4.1 Display the menu:
1. Add Element
2. Delete Element
3. Display Elements
4. Reverse Array
5. Exit
4.2 Accept user choice → choice = int(input("Enter your choice: "))
4.3 Use if–elif–else structure to perform operations based on choice:
o If choice == 1 (Add Element):
a. Input the value to add → val = int(input())
b. Append it to the array → [Link](val)
c. Display “Element added successfully.”
o If choice == 2 (Delete Element):
a. Input the value to delete → val = int(input())
b. Check if the value exists in array using if val in arr:
c. If yes → remove using [Link](val)
Display “Element deleted successfully.”
d. Otherwise, display “Element not found.”
o If choice == 3 (Display Elements):
Display all elements of array → print(arr)
o If choice == 4 (Reverse Array):
Reverse array using → [Link]()
Display “Reversed array: ...”
o If choice == 5 (Exit):
Print “Exiting...” and break the loop.
o Else:
Display “Invalid choice! Try again.”
5. End the while loop when the user selects “Exit”.
6. Stop / End

Code:-
import array
arr = [Link]('i', [])
n = int(input("How many numbers? "))
for i in range(n):
num = int(input("Enter number: "))
[Link](num)
print("Array =", arr)
while True:
print("[Link] [Link] [Link] [Link] [Link]")
choice=int(input("Enter the choice : ",))
if choice==1:
val=int(input("Enter the elememt to Add in the array : ",))
[Link](val)
print("The element Add to the array is : ",val )
elif choice==2:
val=int(input("Enter the elememt to Remove from the array : ",))
if val in arr:
[Link](val)
print("The element removed from the array is : ",val)
else:
print("The element not in the array ")
elif choice==3:
print("The array is : ",arr)
elif choice==4:
[Link]()
print("The reverse array is : ",arr)
elif choice==5:
print("exiting")
break
else:
print("The choce is invalid")
op:-
[Link] [Link] [Link] [Link] [Link]
Enter the choice : 1
Enter the elememt to Add in the array : 34
The element Add to the array is : 34
[Link] [Link] [Link] [Link] [Link]
Enter the choice : 2
Enter the elememt to Remove from the array : 22
The element not in the array
[Link] [Link] [Link] [Link] [Link]
Enter the choice : 2
Enter the elememt to Remove from the array : 45
The element removed from the array is : 45
[Link] [Link] [Link] [Link] [Link]
Enter the choice : 3
The array is : array('i', [12, 44, 65, 89, 54, 89, 54, 90, 294, 62, 34])
[Link] [Link] [Link] [Link] [Link]
Enter the choice : 4
The reverse array is : array('i', [34, 62, 294, 90, 54, 89, 54, 89, 65, 44, 12])
[Link] [Link] [Link] [Link] [Link]
Enter the choice : 5
exiting
print(a)
elif choice==4:
[Link]()
print(a)
elif choice==5:
print("exiting")
break
else:
print("Invalid choice")

String
Creating and Assigning Strings
name = "Sakshi"
greet = 'Hello'

Indexing
print(name[0]) # S
print(name[-1]) # i

Traversal (Loop through string)


for ch in name:
print(ch)

Algorithm
1. Start
2. Read the input string
3. Initialize counters: u = l = d = s = 0
4. For each character in the string
o If it’s between ‘A’ and ‘Z’, increment u
o Else if between ‘a’ and ‘z’, increment l
o Else if between ‘0’ and ‘9’, increment d
o Else increment s
5. Display all counts
6. Stop
Code:-
text=input("Enter the text : ")
u=0
l=0
n=0
s=0
for i in range (len(text)):
if text[i]>='A' and text[i]<='Z':
u+=1
elif text[i]>='a' and text[i]<='z':
l+=1
elif text[i]>='0' and text[i]<='9':
n+=1
else:
s+=1
print("The text is : ",text)
print("The number of upper case is :",u)
print("The number of lower case is :",l)
print("The number is :",n)
print("The number of Special character is :",s)

Op:-
print("Number of uppercase letters:", u)
print("Number of lowercase letters:", l)
print("Number of digits:", d)
print("Number of special characters:", s)

Reverse a String
s = input("Enter a string: ")
print("Reversed string:", s[::-1])

Palindrome Check
s = input("Enter a string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

FUNCTIONS IN PYTHON
Algorithm: Function-Based Calculator
Step 1: Start the program
Step 2: Define the following functions:
• sum(a, b) → returns the addition of two numbers
• sub(a, b) → returns the subtraction of two numbers
• mul(a, b) → returns the multiplication of two numbers
• div(a, b) → returns the division of two numbers
• rem(a, b) → returns the remainder of two numbers
Step 3: Display the menu with choices:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Remainder
6. Exit
Step 4: Get the user’s choice
Step 5:
If choice = 1, input two numbers and call sum(a, b)
If choice = 2, input two numbers and call sub(a, b)
If choice = 3, input two numbers and call mul(a, b)
If choice = 4, input two numbers and call div(a, b)
If choice = 5, input two numbers and call rem(a, b)
If choice = 6, display “Exit from program” and stop
Step 6:
If user enters any other number, display “Invalid choice! Try again.”
Step 7: Repeat steps 3–6 until the user chooses Exit.
Step 8: Stop the program
Program:

def sum(a, b):


return a + b
def sub(a, b):
return a - b
def mul(a, b):
return a * b
def div(a, b):
return a / b
def rem(a, b):
return a % b
while True:
print("1. Addition2. Subtraction3. Multiplication4. Division5. Remainder6. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum =", sum(a, b))
elif choice == 2:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Difference =", sub(a, b))
elif choice == 3:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Product =", mul(a, b))
elif choice == 4:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Division =", div(a, b))
elif choice == 5:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Remainder =", rem(a, b))
elif choice == 6:
print("Exit from program")
break
else:
print("Invalid choice! Try again.")
What to 100% practice for practical:
1. Your calculator program using functions (the one you wrote — perfect)
→ covers defining, calling, returning, using loop, and conditional
2. One small recursion program
→ e.g. factorial using recursion (very short, 5–6 lines)
3. One small built-in function example
→ like using len(), max(), min() on a list or string
4. Debug practice
→ be able to find small mistakes like: missing : or wrong indentation
Math Functions
1. [Link]()
2. [Link]()
3. [Link]()
4. math.log2()
5. math.log10()
6. [Link]()
7. [Link]()
8. [Link]()
9. [Link] ()
10. [Link]()
11. [Link]()
12. [Link]()
13. [Link]()
14. [Link]()
15. [Link]()
16. [Link]()
17. max()
18. min()
code
import math
n = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
ad = float(input("Enter angle in degrees: "))
ar = float(input("Enter angle in radians: "))
print("Power =", [Link](n, n2))
print("Exponential (e^num) =", [Link](n))
print("Log (base num2) =", [Link](n, n2))
print("Log2 =", math.log2(n))
print("Log10 =", math.log10(num))
print("Square Root =", [Link](num))
print("Absolute Value =", [Link](num))
print("Floor =", [Link](num))
print("Ceil =", [Link](num))
print("Factorial =", [Link](int(num))) # factorial needs integer
print("GCD =", [Link](int(num), int(num2)))
print("Pi =", [Link])
print("Degrees (radian → degree) =", [Link](angle_rad))
print("Radians (degree → radian) =", [Link](angle_deg))
print("Sine =", [Link](angle_rad))
print("Cosine =", [Link](angle_rad))
print("Max =", max(num, num2))
print("Min =", min(num, num2))

Python Practical Structure (Simple & Clear Overview)

Input and Output Programs


Concept: Taking input from user and displaying output
Examples:
• Arithmetic operations (add, sub, mul, div, etc.)
• Area of triangle and circle
• Simple interest
• Calculator
Concepts Covered:
input(), print(), type casting (int(), float())

Conditional Statements
Concept: Making decisions using conditions
Examples:
• Check vowel or consonant
• Largest of three numbers
• Check even or odd
• Grading system using if–elif–else
Concepts Covered:
if, elif, else, comparison operators (>, <, ==)

Looping Statements
Concept: Repeating actions
Examples:
• Print numbers 1 to 10 using for loop
• Factorial of a number using while loop
• Prime number check
• Multiplication table
Concepts Covered:
for, while, break, continue, range()

Data Collections (Data Structures)


Concept: Storing and managing multiple data items
Examples:
• List operations (add, delete, sort, reverse, search)
• Tuple operations (display, max, min)
• Set operations (union, intersection, difference)
• Dictionary operations (add, delete, display keys/values)
Concepts Covered:
List [], Tuple (), Set {}, Dictionary {key:value}, slicing, indexing

Arrays and Strings


Concept: Handling sequence data
Examples:
• Create and reverse an array
• Add, delete, and display array elements
• Count uppercase, lowercase, digits, and special characters in a string
• String slicing and indexing
Concepts Covered:
array module, string methods (.upper(), .lower(), .isalpha())

Functions
Concept: Reusable code blocks
Examples:
• Define a function to add two numbers
• Function to check even or odd
• Function to calculate factorial
• Function with return value
Concepts Covered:
def, parameters, return statements

File Handling
Concept: Reading and writing files
Examples:
• Write content to a file
• Read content from file
• Read first 10 bytes
• Read first 2 lines
• Read a character
Concepts Covered:
open(), read(), readline(), write(), file modes ('r', 'w', 'a')

Exception Handling
Concept: Handling errors safely in programs
Examples:
• Handle numeric and name errors
• Handle ZeroDivisionError
• Try–except–else–finally structure
Concepts Covered:
try, except, else, finally

You might also like