Electronics Practical : Python Programming
Q.1. Write a python program to find Sum and difference of a=20, b=11.
a=20
b=11
sum_result=a+b
difference_result=a-b
print("Sum:",sum_result)
Output:
Sum: 31
print("Difference:",difference_result)
Output:
Difference: 9
Q.2. write a python program to find area and perimeter of a triangle.
import math
def triangle_perimeter(a,b,c):
return a+b+c
def herons_area(a,b,c):
s=(a+b+c)/2
return math.sqrt(s*(s-a)*(s-b)*(s-c))
print("Enter the sides of the triangle:")
Output:
Enter the sides of the triangle:
a=float(input("Side 1:"))
Output:
Side 1: 3
b=float(input("Side 2:"))
Output:
Side 2: 4
c=float(input("Side 3:"))
Output:
Side 3: 6
if a+b>c and a+c>b and b+c>a:
perimeter=triangle_perimeter(a,b,c)
area = herons_area(a, b, c)
print(f"Perimeter of the triangle:{perimeter}")
Output:
Perimeter of the triangle:13.0
print(f"Area of the triangle:{area:2f}")
Output:
Area of the triangle:5.33
Q.3. Write a python program to append name in the given below list ?
Names = ["Joseph", "Peter", "Cook", "Tim"]
Names=["Joseph","Peter","Cook","Tim"]
def append_name(name):
Name.append(name)
print("Updated List:",Names)
Output:
Updated List: ['Joseph', 'Peter', 'Cook', 'Tim']
new_name=input("Enter a name to append:Nooh")
Output:
Enter a name to append:Nooh
Q.4. Write a python program to sort the given dictionary.
names = {1:’Alice’, 2:’John’, 4:’Peter’, 3:’Andrew’, 6:’Ruffalo’, 5:’Chris’}
names={1:'Alice',2:'John',4:'Peter',3:'Andrew',6:'Ruffalo',5:'Chris'}
sorted_names=dict(sorted(names.items()))
print("Sorted Dictionary:",sorted_names)
Output:
Sorted Dictionary: {1: 'Alice', 2: 'John', 3: 'Andrew', 4: 'Peter', 5: 'Chris', 6: 'Ruffalo'}
Q.5. Write syntax of all data types of python language.
1. Numeric Types
a. Integer (int)
num_int = 10
b. Floating Point (float)
num_float = 10.5
c. Complex Number (complex)
num_complex = 3 + 4j
2. Sequence Types
a. String (str)
text = "Hello, Python!"
b. List (list)
my_list = [1, 2, 3, "apple", 4.5]
c. Tuple (tuple)
my_tuple = (1, 2, 3, "banana", 5.6)
d. Range (range)
my_range = range(5)
3. Set Types
a. Set (set)
my_set = {1, 2, 3, 4, 5} # Example of a set
b. Frozen Set (frozenset)
my_frozenset = frozenset([1, 2, 3, 4, 5])
4. Mapping Type
a. Dictionary (dict)
my_dict = {"name": "Alice", "age": 25, "city": "New York"}
5. Boolean Type
a. Boolean (bool)
is_valid = True
6. Binary Types
a. Bytes (bytes)
my_bytes = b"Hello"
b. Bytearray (bytearray)
my_bytearray = bytearray(5)
c. Memoryview (memoryview)
my_memoryview = memoryview(bytes(5))
7. None Type
a. NoneType (None)
my_var = None