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

python_programs_collection

The document contains various Python code snippets demonstrating different programming concepts. It includes examples for displaying radio frequency bands, creating a multiplication table, performing list operations, using built-in functions, defining a student class, creating a calculator module, performing NumPy array operations, implementing search techniques, managing stack operations, and demonstrating tuple operations. Each section provides a clear and concise implementation of the respective concept.
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)
6 views3 pages

python_programs_collection

The document contains various Python code snippets demonstrating different programming concepts. It includes examples for displaying radio frequency bands, creating a multiplication table, performing list operations, using built-in functions, defining a student class, creating a calculator module, performing NumPy array operations, implementing search techniques, managing stack operations, and demonstrating tuple operations. Each section provides a clear and concise implementation of the respective concept.
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
You are on page 1/ 3

1) Display various radio frequency bands using if..

elif ladder
frequency = float(input("Enter frequency in MHz: "))
if frequency < 3:
print("Very Low Frequency (VLF)")
elif frequency < 30:
print("Low Frequency (LF)")
elif frequency < 300:
print("Medium Frequency (MF)")
elif frequency < 3000:
print("High Frequency (HF)")
elif frequency < 30000:
print("Very High Frequency (VHF)")
elif frequency < 300000:
print("Ultra High Frequency (UHF)")
else:
print("Super High Frequency (SHF) or above")

2) Multiplication table using for loop


num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")

3) Operations on List
# a) Create
my_list = [1, 2, 3, 4]
# b) Access
print("Element at index 2:", my_list[2])
# c) Update
my_list[1] = 20
# d) Delete
del my_list[0]
print("Updated List:", my_list)

4) Use of math and string built-in functions


import math
import string

print("Square root of 16 is:", math.sqrt(16))


text = "hello world"
print("Uppercase:", text.upper())
print("Is digit:", "123".isdigit())

5) Class Student
class Student:
def __init__(self, roll_no, name, course, percentage):
self.roll_no = roll_no
self.name = name
self.course = course
self.percentage = percentage
def display(self):
print("Roll No:", self.roll_no)
print("Name:", self.name)
print("Course:", self.course)
print("Percentage:", self.percentage)

s1 = Student(101, "John", "Computer", 85.5)


s1.display()

6) User-defined module (Calculator)


# calculator_module.py
def add(x, y):
return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


return x / y

# main.py
import calculator_module as calc

print("Addition:", calc.add(5, 3))


print("Multiplication:", calc.multiply(4, 2))

7) NumPy Array Operations


import numpy as np

arr = np.array([1, 2, 3])


print("Array:", arr)
print("Shape:", arr.shape)
print("Max:", np.max(arr))
print("Sum:", np.sum(arr))

8) Searching techniques
# a) Linear Search
def linear_search(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1

# b) Binary Search
def binary_search(arr, x):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1

arr = [1, 3, 5, 7, 9]
print("Linear Search:", linear_search(arr, 7))
print("Binary Search:", binary_search(arr, 7))

9) Stack operations using Array


stack = []

def push(val):
stack.append(val)

def pop():
if stack:
return stack.pop()
else:
return "Stack is empty"

push(10)
push(20)
print("Popped:", pop())
print("Stack:", stack)

10) Tuple Operations


# a) Create
my_tuple = (1, 2, 3)
# b) Access
print("Second element:", my_tuple[1])
# c) Update (tuples are immutable, so we recreate)
my_tuple = my_tuple[:1] + (99,) + my_tuple[2:]
# d) Delete
del my_tuple
print("Tuple deleted")

You might also like