0% found this document useful (0 votes)
21 views

Python Practicals Grade Xii

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Python Practicals Grade Xii

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PYTHON

PRACTICALS
Kushagra Parijat Lahir
Grade XII
Science
Program to find a Factorial of A
Number

def factorial(n): # single line


to find factorial return 1 if
(n==1 or n==0) else n * factorial(n
- 1) # Driver Code num = 5
("Factorial
of",num,"is",factorial(num))
Program to find Nth Multiple of
Fabonacci Series

def findPosition(k, n): f1 = 0 f2 = 1 i =2;


while i!=0: f3 = f1 + f2; f1 = f2; f2 = f3;
if f2%k == 0:
return n*i i+=1
return # Multiple no. n = 5; # Number of
whose multiple we are finding k = 4;

("Position of n\'th multiple of k in"


"Fibonacci Series is", findPosition(k,n));
Program to Interchange the First
and Last Elements
# Swap function
def swapList(newList):
size = len(newList)

# Swapping
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp

return newList

# Driver code
newList = [12, 35, 9, 56, 24]

print(swapList(newList))
Check if element exists in list in
Python
lst=[ 1, 6, 3, 5, 3, 4 ]
#checking if element 7 is present
# in the given list or not

i=7

# if element present then return


# exist otherwise not exist

if i in lst:

("exist") else:
("not exist")
Python Cloning or Copying
a List
# Python program to copy or
clone a list
# Using the Slice Operator
def Cloning(li1):
li_copy = li1[:]
return li_copy

# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)
Program for removing i-th
character from a string
# Python3 program for removing i-th
# indexed character from a string

# Removes character at index i

def remove(string, i):

# Characters before the i-th indexed


# is stored in a variable a
a = string[: i]

# Characters after the nth indexed


# is stored in a variable b
b = string[i + 1:]

# Returning string after removing


# nth indexed character.
return a + b

# Driver Code
if __name__ == '__main__':

string = "geeksFORgeeks"

# Remove nth index element


i = 5

# Print the new string


print(remove(string, i))
Sort python Dictionaries by key or
value
myDict = {'ravi': 10, 'rajnish': 9,
'sanjeev': 15, 'yash': 2,
'suraj': 32}

myKeys = list(myDict.keys())
myKeys.sort()
sorted_dict = {i: myDict[i] for i in
myKeys}

print(sorted_dict)
Ways to remove a key from
Dictionary
# Initializing dictionary
test_dict = {"Arushi": 22, "Mani": 21,
"Haritha": 21}

# Printing dictionary before removal


print("The dictionary before
performing remove is : ", test_dict)

# Using del to remove a dict


# removes Mani
del test_dict['Mani']

# Printing dictionary after removal


print("The dictionary after remove is
: ", test_dict)

# Using del to remove a dict


# raises exception
del test_dict['Mani']
Remove all duplicate words
from a given Sentence
from collections import Counter

def remov_duplicates(input):

# split input string separated by space


input = input.split(" ")

# now create dictionary using counter method


# which will have strings as key and their
# frequencies as value
UniqW = Counter(input)

# joins two adjacent elements in iterable way


s = " ".join(UniqW.keys())
print (s)

# Driver program
if __name__ == "__main__":
input = 'Python is great and Java is also great'
remov_duplicates(input)

You might also like