Assignment1:
• Write a Python Program to print a value using variable.
num=12
print(‘ value of num=’,num)
• Write the correct syntax to print the length of string
X = “Hello, World!
x = "Hello World"
print( len(x))
• Write the correct syntax to convert the value of X to
upper case and lower case.
x = "Hello World"
print( x.lower())
print( x.upper())
• Write a program to replace python with java of string
(txt = "I am a python
developer!
x = " I am a python developer!"
print( x.replace("python","java"))
Assignment2:
• Write a python program to perform arithmetic operation using variable.
num1 = 1.5
num2 = 6.3
sum = num1 + num2
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
• Write a program to print output of 5+9*4 expression. also print which operator has higher
precedence.
exp=5+9*4
print(' Expression evaluated=',exp)
print(' Multiplication, division, remainder operators has highest precedence than addition and
substraction operator ')
Assignment 3:
• Write a python function take name & roll no. as an argument & pass the value for the same.
def stud_detail(name,rollno):
print('Name of Student=',name)
print('RollNo of Student=',rollno)
#call to function
stud_detail('Shreeman',2)
• Write a program to create a variable inside a function, with the same name as the global
variable.
name='Shreeman'
def stud_detail(name):
name='shree'
print('Name of Student=',name)
stud_detail(name)
Assignment 4:
• Write a program to change the value “banana” & cherry with the value “Blackcurrant &
watermelon”
Fruit = [“apple”, ”banana”, “Cherry”, “Mango”, ”Kiwi”, ”dragon”].
Fruit = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
Fruit [1:3] = ["blackcurrant", "watermelon"]
print(Fruit)
• Program to Print values of dictionary. Also print value of any key in dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3}
# Print keys
print("Keys:", list(my_dict.keys()))
# Print values
print("Values:", list(my_dict.values()))
#print value of keys in dictionary
for i in my_dict
print(my_dict[i])
# Print both keys and values
print("Keys and Values:")
print(my_dict)
Assignment 5:
• Display the Addition, Subtraction, Multiplication & Division of 20+50, using two
variables: x and y.
x = 20
y = 50
print("x=",x ,"and y=",y)
result = x + y
print(“Addition of two number=”,result)
result = x - y
print("Substraction of two number=",result)
result = x * y
print("Multiplication of two number=",result)
result = x / y
print("Division of two number=",result)
• Write the correct syntax to perform following task (X = “I am python developer”!)
a)Get the characters from index 2 to index 4
b) To find the length of variable X
x = " I am a python developer!"
print(x[2:4])
print( len(x))
Assignment 6:
• Write a program to read & print file content.
file_path = 'C:\Users\Public\Documents\sample.txt'
file=open(file_path, 'r')
# Read the content of the file
file_content = file.read()
# Print the content
print("File Content:\n", file_content)
• Write a program to print year & short version of month.
from datetime import datetime
month = datetime.now().strftime("%b")
year = datetime.now().year
print("Current month: ", month)
print("Current year: ", year)
Assignment 7:
• Write a program to read & print first 5 character of a file content.
# Open the file in read mode
file= open('file.txt', 'r')
# Read the first 5 characters
content = file.read(5)
# Print the content
print(content)
• Write a program to calculate the Area of circle Take all inputs from users.
PI = 3.14
r = float(input("Enter the radius of a circle:"))
area = PI * r * r
print("Area of a circle = %.2f" %area)
Assignment 8:
• Write a Program to insert and delete from dictionary.
my_dict = {31: 'a', 21: 'b', 14: 'c'}
my_dict[15]='d'
print(my_dict)
del my_dict[31]
print(my_dict)
• Write a program to create a variable inside a function, with the same name as the global
variable.
_my_global = 5
def func1():
_my_global = 42
print(_my_global)
func1()
Assignment 9:
• Write a program to concatenate/join two string using operator. Take input from user.
# Defining strings
var1 = input("enter string 1")
var2 = input("enter string 2")
# + Operator is used to combine strings
var3 = var1 + var2
print(var3)
• Write a program to perform all arithmetic operations. Take input from user.
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
print("Enter which operation would you like to perform?")
ch = input("Enter any of these char for specific operation +,-,*,/: ")
result = 0
if ch == '+':
result = num1 + num2
elif ch == '-':
result = num1 - num2
elif ch == '*':
result = num1 * num2
elif ch == '/':
result = num1 / num2
elif ch == '%':
result = num1 % num2
elif ch == '**':
result = num1 ** num2
else: print("Input character is not recognized!")
print(num1, ch , num2, ":", result)
Assignment 10:
• Write a program to check whether the given number is even or odd
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
• Write a program to print factorial of a given number
# To take input from the user
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Assignment 11:
• Write a program to print all the elements of the list that are less than 10.
list1 = [8, 22, 3, 44, 55]
val = 10
for x in list1:
# compare with all the
# values with value
if x<val:
print(x)
• Write a program to generate random numbers between the range 1 to 100.
import random
print("Random integers between 0 and 9: ")
for i in range(0, 5):
y = random.randrange(1,100)
print(y)
Assignment 12:
• Write a program to print second last item of tuple.
Tuple =( 1, 0, 4, 2, 5, 6, 7, 5)
l=len(Tuple)
print(Tuple[l-2])
• Write a program to return 3, 4 and 5th item of tuple.
Tuple =( 1, 0, 4, 2, 5, 6, 7, 5)
print(Tuple[2])
print(Tuple[3])
print(Tuple[4])
Assignment 13:
• Write a program to print year & short version of month.
from datetime import datetime
month = datetime.now().strftime("%b")
year = datetime.now().year
print("Current month: ", month)
print("Current year: ", year)
• Write a program to print second last item of the list create a module for the same.
list.py
# Python Module list
def lastitem():
list1 = [8, 22, 3, 44, 55]
l=len(list1)
print(list1[l-2])
import list
list.lastitem()
Assignment 14:
• Write a program to print year & short version of month.
from datetime import datetime
month = datetime.now().strftime("%b")
year = datetime.now().year
print("Current month: ", month)
print("Current year: ", year)
• Write a python program to print character from index number 3 to 6 from a
given string (x = “I am a Python developer”)
x = " I am a python developer!"
print(x[3:6])
print( len(x))
Assignment 15:
• Write a program to read & print file content.
file_path = 'C:\Users\Public\Documents\sample.txt'
try:
with open(file_path, 'r') as file:
# Read the content of the file
file_content = file.read()
# Print the content
print("File Content:\n", file_content)
except FileNotFoundError:
print(f"File '{file_path}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
• Write a program Program to Merge Two Lists and Sort it.
list_1 = [20, 18, 9, 51, 48, 31]
list_2 = [28, 33, 3, 22, 15, 20]
print("The first list is :")
print(list_1)
print("The second list is :")
print(list_2)
l1=list_1+ list_2
print("Merged List:", l1)
# sorting list using nested loops
for i in range(0, len(l1)):
for j in range(i+1, len(l1)):
if l1[i] >= l1[j]:
l1[i], l1[j] = l1[j],l1[i]
# sorted list
print("Sorted List", l1)
Write a python program to implement continue statement
for var in "Geeksforgeeks":
if var == "e":
continue
print(var)
Write a python program to implement continue statement
for var in "Geeksforgeeks":
if var == "e":
break;
print(var)
Write a Python Program to print item from index 4 to 8 of a list. Also print second last item of list.
list=['a','b','c','d','e','f','g','h']
l=len(list)
print(list[l-2])
print(list[4:9])
Write a Python program to check if any phrase/word is present in the object/ variable or
not
sentence = "The quick brown fox jumps over the lazy dog."
word = "fox"
if word in sentence:
print("The word {0} is found in the sentence".format(word))
else:
print("The word {0} is not found in the sentence".format(word))
Write a python program to add content/data to the file.
file1 = open("file.txt", "a") # append mode
file1.write("Today \n")
file1.close()
Write a Python program to print current date & time.
import datetime
# Get the current date and time
now = datetime.datetime.now()
# Create a datetime object representing the current date and time
print("Current date and time : ")
# Print the current date and time in a specific format
print(now.strftime("%Y-%m-%d %H:%M:%
S"))
No comments:
Post a Comment