efile
efile
efile
PROGRAM-2
#To print the squares of numbers from 1 to 10 using loop control.
CODE:- OUTPUT:-
num = 1
while num <= 10:
square = num ** 2
print(f"The square of {num} is
{square}")
num += 1
PROGRAM-3
#Display numbers from -10 to-1 using for loop.
CODE:- OUTPUT:-
for num in range(-10,0):
print(num)
PROGRAM-4
# To count the number of even and odd numbers from a series of numbers.
CODE:- OUTPUT:-
even_count = 0
odd_count = 0
for num in range(1,11):
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
print("The number of Odd nos and even nos
from 1 to 10 is :")
print(f"Even numbers: {even_count}")
print(f"Odd numbers: {odd_count}")
PROGRAM-5
# Program that prints all the numbers from 0 to 6 except 3 and 6
CODE:- OUTPUT:-
for i in range (0,6):
if i==3:
continue
print(i)
PROGRAM:6
#Program to generate the Fibonacci series.
CODE:- OUTPUT:-
n = int(input("enter a number:"))
a=0
b=1
for i in range(1,n):
c=a+b
a=b
b=c
print(c)
PROGRAM-7
# Program to compute the GCD of two numbers. (with and with recursion)
CODE:- OUTPUT:-
#without recursion:
num1=int(input("enter a number: "))
num2=int(input("enter second
number"))
while num2!=0:
num1,num2=num2,num1%num2
print("the gcd is of the two numbers
is
",num1)
#with recursion
def gcd (a,b):
if b == 0:
return a
else :
return gcd(b,a%b)
num1=int(input("enter first number:"))
num2=int(input("enter second
number: "))
a=gcd(num1,num2)
print("the gcd of thr two numbers are :
",a)
PROGRAM-8
#Check if a number is prime or not.
CODE:- OUTPUT:-
n=int(input("enter a number to
check if number is prime or not: "))
for i in range (2,n):
if n%i==0:
print("not prime number")
break;
else:
print("prime number")
PROGRAM-9
# Generate the first prime numbers.
CODE:- OUTPUT:-
lower=int(input("enter the starting
number:"))
higher=int(input("enter the end
number:"))
print(f"the prime number for
{lower} to {higher} are")
for num in range(lower,higher+1):
if num>1:
prime=True
for i in
range(2,int(num**0.5)+1):
if num % i==0:
prime=False
break
if prime:
print(num)
PROGRAM-10
# Find the sum of squares of n natural numbers.
CODE:- OUTPUT:-
n=int(input("Enter the value of n:
"))
i=1
sum=0
while i<=n:
sum=sum+i*i
i=i+1
print("sum=",sum)
PROGRAM-11
#Check if a number is a palindrome.
CODE:- OUTPUT:-
list1 =["apple",
"banana","pineapple","kiwi"]
print(list1)
PROGRAM-12
# Store strings in a list and print them
CODE:- OUTPUT:-
list1 =["apple",
"banana","pineapple","kiwi"]
print(list1)
PROGRAM-13
# Calculate the length of a list, reverse it, copy it, and then clear it.
CODE:- OUTPUT:-
list1 =["apple",
"banana","pineapple","kiwi"]
print("length of this list is
",len(list1))
list1.reverse()
print("the reverse list is
:",list1)
a=list1.copy()
print("the copy list is:", a)
list1.clear()
print("clar list is ",list1)
PROGRAM-14
#Calculate the length of a string
CODE:- OUTPUT:-
list1 ="apple is red"
print("length of this list is ",len(list1))
PROGRAM-15
# Check if the word 'open' is present in the "This is open-source software".
CODE:- OUTPUT:-
a= "This is open source software"
if "open "in a:
print("the word open is there in
sentence")
else:
print("the word open is not there
in sentence")
PROGRAM-16
# Check if the letter 'e' is present in the word 'Welcome'
CODE:- OUTPUT:-
a= 'welcome'
if "e"in a:
print("it is there ")
else:
print("it is not there")
PROGRAM-17
#Program to reverse a string
CODE:- OUTPUT:-
def rev (str1):
re_str=""
for i in str1:
re_str =i+re_str
return re_str
s =input("enter a string:")
print("before reversing: ",s)
b=rev(s)
print("after reversing: ",b)
PROGRAM-18
#Delete first and last elements from a list
CODE:- OUTPUT:-
list1=["apple","banana","kiwi","pineapple"]
list2 =list1[1:-1]
print("before deleting:",list1)
print("after deleting:",list2)
PROGRAM-19
# Check a list is empty or not
CODE:- OUTPUT:-
list1=[]
if len(list1)==0:
print("list1 is empty")
else:
print("list1 is not
empty")
PROGRAM-20
# Print the even numbers from a given list.
CODE:- OUTPUT:-
def even_numbers(num):
for i in numbers:
if i % 2 == 0:
print("even number is
;",i)
numbers = [1, 2, 3, 4, 5, 6, 7, 8,
9, 10]
even_numbers(numbers)
PROGRAM-21
# Let list=[’a’,’b’,’c’,’d’,’e’,’f’]. Find a)list[1:3] b)list[:4] c)list[3:]
CODE:- OUTPUT:-
list1=["a","b","c","d","e","f"]
print(list1[1:3])
print(list1[:4])
print(list1[3:])
PROGRAM-22
# Function to sum all the numbers in a list: Sample List: (8, 2, 3, 0, 7)
CODE:- OUTPUT:-
def sum_of_number(num):
total=0
for i in num:
total+=i
print("the total is ",total)
sample_list=[8, 2, 3, 0, 7]
sum_of_number(sample_list)
PROGRAM-23
# Find common items from two lists
CODE:- OUTPUT
list1=[1,2,3,4,5,6,7,8,9,10]
list2=[2,5,4,11,64,45,18,7]
common_items=set(list1) &
set(list2)
print(common_items)
PROGRAM-24
# Create a tuple with different data types.
CODE:- OUTPUT:-
my_tuple=[1,"mustang",6.66,True]
integervalue= my_tuple[0]
stringvalue= my_tuple[1]
floatvalue= my_tuple[2]
booleanvalue= my_tuple[3]
print("integer is:",integervalue)
print("string is:",stringvalue)
print("float value is:",floatvalue)
print("boolean value
is:",booleanvalue)
PROGRAM-25
# Unpack a tuple in several variables
CODE:- OUTPUT
my_tuple=(1,2,3,4,6)
a,b,c,d,e= my_tuple
print(a)
print(b)
print(c)
print(d)
print(e)
PROGRAM-26
# Print a pattern:
1
12
123
1234
12345
CODE:- OUTPUT
n =int(input("enter a value:\t"))
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end="")
print()
PROGRAM-27
# Print a pattern:
*
**
***
****
CODE:- OUTPUT
n = int(input("enter a value:\t"))
for i in range(n):
print(" " * (n - i - 1), end="")
print("* " * (i + 1))
PROGRAM-28
# Function to calculate the factorial of a number
CODE:- OUTPUT:-
def fact(n):
if n < 0:
return "Factorial is not defined for
negative numbers."
result = 1
for i in range(1, n + 1):
result *= i
return result
# Example usage
number = int(input("enter a number:"))
print(f"Factorial of {number}:",
fact(number))
PROGRAM-29
# Change a given string to a new string where first and last chars have been
changed.
CODE:- OUTPUT
def swap(s):
if len(s) <= 1:
return s
new_string = s[-1] + s[1:-1] + s[0]
return new_string
word_count[word] +=
1
else:
word_count[word] = 1
return word_count
sentence =
input("enter a
sentence:")
result =
word_count(sentence)
print("Word
occurrences:", result)
PROGRAM-31
#Find the first and last occurrence of the letter ‘E’ and character ‘ , ’, in “NEP
IMPLEMENTATION, FOR BCA”
CODE:- OUTPUT
first_comma, last_comma =
find(string, ',')
print(f"First occurrence of ',':
{first_comma}, Last occurrence of ',':
{last_comma}")
PROGRAM-32
# Remove duplicates from a list
CODE:- OUTPUT
list1 = [1, 2, 2, 3, 4, 5, 5, 6]
list2 = list(set(list1))
print(list2)
PROGRAM-33
# Find second smallest number in a list
CODE:- OUTPUT
list1 = [7, 2, 5, 3, 2, 8, 1, 4, 5]
list2 = list(set(list1))
list2.sort()
if len(list2) >= 2:
second_small =list2[1]
print("second smallest
number:",second_small)
PROGRAM-34
#Read an entire text file.
CODE:- OUTPUT:-
f=open("file1.txt","r")
data=f.read()
print(data)
PROGRAM-35
# Append a text file and display the text.
CODE:- OUTPUT:-
f=open("file1.txt","a") Before:-
data=f.write("\ni like to study python ")
After:-
PROGRAM-36
# Count number of lines in a text file.
CODE:- OUTPUT:-
file=open("file1.txt","r")
count=0
content=file.read()
conlist=content.split('\n')
print(conlist)
for i in conlist:
if i:
count+=1
print("number of lines in the file
is",count)
PROGRAM-37
# Write a list to a file.
CODE:- OUTPUT:-
my_list = ['apple', 'banana', 'cherry']
with open('output.txt', 'w') as file:
for item in my_list:
file.write(item + '\n')
PROGRAM-38
# Extract characters from various text files and put them into a list.
CODE:- OUTPUT:-
word=[]
with open("file1.txt","r")as f:
for i in f:
word.append(i)
with open("file2.txt","r")as f:
for i in f:
word.append(i)
print(word)
PROGRAM-39
# Function that reads a file file1 and copies only alternative lines to another file
file2.
CODE:- OUTPUT:-
f1=open("1.txt","r")
f2=open("even.txt","w")
f3=open("odd.txt","w")
data=f1.readlines()
for i in range (len(data)):
if (i%2!=0):
f2.write(data[i])
else:
f3.write(data[i])
f1.close()
f2.close()
f3.close()
f4=open("even.txt","r")
f5=open("odd.txt","r")
print("even file:\n",f4.read())
print("odd file:\n",f5.read())
PROGRAM-40
# Function that reads a file file 1 and displays the number of words and the
number of vowels in the file2.
CODE:- OUTPUT:-
# to count vowels in the file
f=open("file1.txt","r")
data=f.read()
word=data.split()
v=0
for i in data:
if i in ['a','e','i','o','u']:
v=v+1
print("vowels:",v)
print("words: ",len(word))
PROGRAM-41
# Function to calculate total sales of a salesman in a month, commission and
remarks for the salesman.
CODE:- OUTPUT:-
def cal_salary (t_sales):
monthlysale=t_sales
## to mark a remark
if (monthlysale>=80000):
print("excellent")
elif(monthlysale>=60000 and
monthlysale<=80000):
print("good sales ")
elif(monthlysale>=40000 and
monthlysale<=60000):
print("average")
else:
print("work hardd ")
## to calcutale comission
if (monthlysale>=50000):
comission=monthlysale*0.05
print("the comission
is:",comission)
else :
print("no comission")
week1=int(input("enter 1st week
sales: "))
week2=int(input("enter 2nd week
sales: "))
week3=int(input("enter 3rd week
sales: "))
week4=int(input("enter 4th week
sales: "))
t_sales=week1+week2+week3+week4
print("your monthly sales is: ",t_sales)
cal_salary(t_sales)