Aim: Write a program to demonstrate basic data type in python.
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
c = a + b
print("Sum=", c)
# Output:
# python add.py 4 5
# Sum= 9Code:
a = 5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
# Creating a String
String1 = "Welcome to the Geeks World"
print("String with the use of Single Quotes: ")
print(String1)
# Creating a List with
List = ["Geeks", "For", "Geeks"]
print("\nList containing multiple values: ")
print(List[0])
print(List[2])
# Creating a Tuple with the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
# Creating a Set
set1 = set()
set1.add(8)
set1.add(9)
set1.add((6, 7))
print("\nSet after Addition of Three elements: ")
print(set1)
# Output:
# Type of a: <class 'int'>
# Type of b: <class 'float'>
# Type of c: <class 'complex'>
# String with the use of Single Quotes:
# Welcome to the Geeks World
# 17
# List containing multiple values:
# Geeks
# Geeks
# Tuple using List:
# (1, 2, 4, 5, 6)
# Set after Addition of Three elements:
# {8, 9, (6, 7)}
Aim: (a) Write a program to compute distance between two points taking input from the user.
# Code:
import math
a = int(input("Enter first value"))
b = int(input("Enter second value"))
c = math.sqrt(a**2 + b**2)
print("Distance=", c)
# Output:
# Enter first value :- 5
#Enter second value :- 6
#Distance= 7.810249675906654
Aim: (b) Write a program add.py that takes 2 numbers as command line arguments and prints its sum.
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
c = a + b
print("Sum=", c)
# Output:
# python add.py 4 5
# Sum= 9
Aim: (a) Write a Program for checking whether the given number is an even number or not.
# Code:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("This is an even number.")
else:
print("This is an odd number.")
# Output:
# Enter a number: 4
# This is an even number.
Aim: (b) Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . . , 1/10.
# Code:
for i in range(1, 11):
print("Decimal equivalent value for 1/", i, " is", 1 / float(i))
# Output:
# Decimal equivalent value for 1/ 1 is 1.0
# Decimal equivalent value for 1/ 2 is 0.5
# Decimal equivalent value for 1/ 3 is 0.3333333333333333
# Decimal equivalent value for 1/ 4 is 0.25
# Decimal equivalent value for 1/ 5 is 0.2
# Decimal equivalent value for 1/ 6 is 0.16666666666666666
# Decimal equivalent value for 1/ 7 is 0.14285714285714285
# Decimal equivalent value for 1/ 8 is 0.125
# Decimal equivalent value for 1/ 9 is 0.1111111111111111
# Decimal equivalent value for 1/ 10 is 0.1
Aim: (a) Write a Program to demonstrate list in python (We are given an array of n distinct numbers, the task is to sort all even-placed numbers in increasing and odd-place numbers in decreasing order. The modified array should contain all sorted even-placed numbers followed by reverse sorted odd-placed numbers.)
# Code:
def evenOddSort(input):
# separate even odd indexed elements list
evens = [input[i] for i in range(0, len(input)) if i % 2 == 0]
odds = [input[i] for i in range(0, len(input)) if i % 2 != 0]
# sort evens in ascending and odds in descending using sorted() method
print(sorted(evens) + sorted(odds, reverse=True))
input = [0, 1, 2, 3, 4, 5, 6, 7]
evenOddSort(input)
# Output:
# [0, 2, 4, 6, 7, 5, 3, 1]
Aim: (b) Write a Program to demonstrate tuple in python (Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list).
def removeDuplicates(lst):
return [t for t in (set(tuple(i) for i in lst))]
# Driver code
lst = [(1, 2), (5, 7), (3, 6), (1, 2)]
print(removeDuplicates(lst))
# Output:
# [(1, 2), (5, 7), (3, 6)]
Aim: (c) Write a program using a for loop that loops over a sequence.
players = ["kohli", "dhoni", "sachin", "sehwag", "Dravid"]
for i in players:
print(i)
# Output:
# kohli
# dhoni
# sachin
# sehwag
# Dravid
Aim: (d) Write a program using a while loop that asks the user for a number, and prints a countdown from that number to zero.
# Code:
n = int(input("Enter the number for countdown: "))
while 0 <= n:
print(n, end=" ")
n = n - 1
# Output:
# Enter the number for countdown: 15
# 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Aim: (a) Find the sum of all the primes below two million.
# Code:
n = 2000000
prime = [True for i in range(n + 1)]
p = 2
while p * p <= n:
if prime[p] == True:
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
sum = 0
for p in range(2, n):
if prime[p]:
sum = sum + p
print("sum=", sum)
# Output:
# sum= 142913828922
Aim: (b) By considering the terms in the Fibonacci sequence whose values do not exceed four million, WAP to find the sum of the even-valued terms.
# Code:
limit = 4000000
if limit < 2:
print("Sum=0")
else:
ef1 = 0
ef2 = 2
sm = ef1 + ef2
while ef2 <= limit:
ef3 = 4 * ef2 + ef1
if ef3 > limit:
break
ef1 = ef2
ef2 = ef3
sm = sm + ef2
print("Sum=", sm)
# Output: Sum= 4613732
Aim: (a) Write a program to count the numbers of characters in the string and store them in a dictionary data structure.
def char_frequency(str1):
dict = {}
for n in str1:
keys = dict.keys()
if n in keys:
dict[n] += 1
else:
dict[n] = 1
return dict
print(char_frequency("google.com"))
print(char_frequency("https://aayushkimehnat.vercel.app"))
#Output:
#{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
#{'a': 4, 'y': 1, 'u': 1, 's': 1, 'h': 2, 'k': 1, 'i': 1, 'm': 1, 'e': 3, 'n': 1, 't': 1, '.': 2, 'v': 1, 'r': 1, 'c': 1, 'l': 1, 'p': 2}
Aim: (b) Write a program to use split and join methods in the string and trace a birthday of a person with a dictionary data structure.
dob = {"mothi": "12-11-1990", "sudheer": "17-08-1991", "vinay": "31-08-1988"}
str1 = input("which person dob you want:")
l = str1.split()
birth = ""
for i in l:
if i in dob.keys():
name = i
print(" ".join([name, "Birthday is", dob[name]]))
Output:-
#which person dob you want:- mothi
#mothi Birthday is 12-11-1990
Aim: Write a program to count frequency of characters in a given file. Can you use character frequency to tell whether the given file is a Python program file, C program file or a text file?
# Code:
import os
f = open("deepa.py")
count = dict()
for line in f:
for ch in line:
if ch in count:
count[ch] = count[ch] + 1
else:
count[ch] = 1
print(count)
filename, file_extension = os.path.splitext("deepa.py")
print("file_extension==", file_extension)
if file_extension == ".py":
print("its python program file")
elif file_extension == ".txt":
print("its a txt file")
elif file_extension == ".c":
print("its a c program file")
f.close()
# deepa.py:
# my name is deepa modi
# Output:
# {'m': 3, 'y': 1, ' ': 4, 'n': 1, 'a': 2, 'e': 3, 'i': 2, 's': 1, 'd': 2, 'p': 1, 'o': 1}
# file_extension== .py
# its python program file
def student_management():
students = {}
student_marks = {}
courses = set()
def add_student(roll_no, student_name):
if roll_no not in students:
students[roll_no] = student_name
else:
print(f"Roll number {roll_no} already exists.")
def add_marks(roll_no, subject, marks):
if roll_no in students:
if roll_no in student_marks:
student_marks[roll_no][subject] = marks
else:
student_marks[roll_no] = {subject: marks}
courses.add(subject)
else:
print(f"Student with roll number {roll_no} not found.")
def get_student_details(roll_no):
if roll_no in students:
student_name = students[roll_no]
print(f"Roll Number: {roll_no}")
print(f"Student: {student_name}")
if roll_no in student_marks:
print("Marks:")
for subject, marks in student_marks[roll_no].items():
print(f" {subject}: {marks}")
else:
print("No marks recorded for this student.")
else:
print(f"Student with roll number {roll_no} not found.")
def get_unique_courses():
return courses
add_student(101, "Aman")
add_student(102, "Shivam")
add_student(103, "Pravesh")
add_student(101, "Aman")
add_marks(101, "Mad_lab", 90)
add_marks(101, "Python_Lab", 85)
add_marks(102, "Mad_lab", 75)
add_marks(102, "Python_Lab", 80)
add_marks(103, "Mad_lab", 92)
add_marks(103, "Python_Lab", 88)
add_marks(104, "Mad_lab", 90)
get_student_details(102)
print("\nUnique Courses:", get_unique_courses())
student_management()