Professional Documents
Culture Documents
D20dce172 Journal
D20dce172 Journal
D20dce172 Journal
PRACTICAL -1
AIM: Create a program that asks the user to enter their name and their
age. Printout a message addressed to them that tells them the year that
they will turn 100 years old.
CODE:
name = input("Enter your Name:")
age = int(input("Enter your Age:"))
age=100-age
yearOf100 = 2021+age
A =name+ "you will be aged 100 years in
"+str(yearOf100)
print(A)
OUTPUT:
1
DEPSTAR-CE
Programming in python D20DCE172
PRACTICAL – 2.1
AIM: Ask the user for a number. Depending on whether the number is
even or 2 odd, print out an appropriate message to theuser.
CODE:
number = int(input("enter the Number:"))
if number%2==0:
print("number is even")
else:
print("number is odd")
OUTPUT:
2
DEPSTAR-CE
Programming in python D20DCE172
PRACTICAL-2.2
AIM: Take a list, say for example this one:a = [1, 1, 2, 3, 5, 8, 13, 21, 34,
55, 89], and write a program that prints out all the elements of the list that
are less than 5.
CODE:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
for i in a:
if i < 5 :
print(i)
output:
3
DEPSTAR-CE
Programming in python D20DCE172
PRACTICAL-3.1
AIM: Create a program that asks the user for a number and then prints out
a list of all the divisors of that number
CODE:
number = int(input("Enter the number :"))
i=1
while i < number:
if number%i==0 :
print(i)
i+=1
OUTPUT:
4
DEPSTAR-CE
Programming in python D20DCE172
PRACTICAL-3.2
AIM: Take two lists, say for example these two:
and write a program that returns a list that contains only the elements thatare
common between the lists (without duplicates).
CODE:
a =[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b =[1, 2, 3, 4,5, 6, 7, 8, 9, 10, 11, 12, 13]
c = []
print(c)
output:
5
DEPSTAR-CE
Programming in python D20DCE172
Practical-3.3
AIM: Ask the user for a string and print out whether this string is a
palindrome or not. (A palindrome is a string that reads the same forwards
and backwards.
Code:
x = input("enter the string:")
w = ""
for i in x:
w = i + w
if (x == w):
print("Yes")
else:
print("No")
output:
PRACTICAL-4.1
6
DEPSTAR-CE
Programming in python D20DCE172
AIM: a = [1, 4, 9, 16, 25, 36, 49, 64,81, 100]. Write one line of
Python that takes this list and makes a new listthat has only the
even elements of this list in it.
CODE:
a = [1, 4, 9, 16, 25, 36, 49, 64,81, 100]
b= [ i for i in a if i%2==0]
print("Even elements are :",b)
OUTPUT:
PRACTICAL-4.2
7
DEPSTAR-CE
Programming in python D20DCE172
AIM: Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays
(using input), compare them, print out a message of congratulations to
thewinner, and ask if the players want to start a new game)
CODE:
import random
print("Winning Rules of the Rock paper scissor game
as follows: \n"
+ "Rock vs paper=paper wins \n"
+ "Rock vs scissor=Rock wins \n"
+ "paper vs scissor=scissor wins \n")
while True:
print("Enter choice \n 1. Rock \n 2. paper \n 3.
scissor \n")
choice = int(input("User turn: "))
if choice == 1:
choice_name = 'Rock'
elif choice == 2:
choice_name = 'paper'
else:
choice_name = 'scissor'
if comp_choice == 1:
comp_choice_name = 'Rock'
elif comp_choice == 2:
comp_choice_name = 'paper'
8
DEPSTAR-CE
Programming in python D20DCE172
else:
comp_choice_name = 'scissor'
if result == choice_name:
print("User won")
else:
print("Computer won")
OUTPUT:
9
DEPSTAR-CE
Programming in python D20DCE172
PRACTICAL -4.3
10
DEPSTAR-CE
Programming in python D20DCE172
AIM: Generate a random number between 1 and 9 (including 1 and 9). Ask
theuser to guess the number, then tell them whether they guessed too low,
too high, or exactly right.
CODE:
import random
number = random.randint(1,9)
user_number = int(input("Guess the number:"))
if number > user_number:
print("Guessed too low")
elif number < user_number:
print("Guessed too high")
else:
print("Exactly right")
print("The real number: ",number)
print("Your Guessed number:",user_number)
OUTPUT
PRACTICAL-5.1
11
DEPSTAR-CE
Programming in python D20DCE172
AIM: Take two lists, say for example these two:a = [1, 1, 2, 3, 5, 8, 13, 21,
34, 55, 89]b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] and write a program that
returns a list that contains only the elements that are common betweenthe
lists (without duplicates). Make sure your program works on two lists of
different sizes. Write this in one line of Python using at least one list
comprehension
CODE:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
list1_as_set = set(a)
intersection = list1_as_set.intersection(b)
c_list = list(intersection)
print("Common elements are :",c_list)
OUTPUT:
PRACTICAL-5.2
12
DEPSTAR-CE
Programming in python D20DCE172
AIM: Ask the user for a number and determine whether the number is
primeor not. (For those who have forgotten, a prime number is a number that
has no divisors.).
CODE:
def checkprime(num):
if num >1:
for i in range(2,num):
if (num%2)==0:
print(num,"number is not a prime
number")
break
else:
print(num,"number is a prime number")
else:
print("number is not a prime number")
num = int(input("Enter the number to find if its
prime or not:"))
checkprime(num)
output:
Practical-5.3
13
DEPSTAR-CE
Programming in python D20DCE172
AIM: Write a program that takes a list of numbers (for example, a = [5, 10,
15, 20,25]) and makes a new list of only the first and last elements of the
given list.For practice, write this code inside a function.
CODE:
def flFunction(a):
x=len(a)
print("all elements",a)
print("first element:",a[0])
print("last element:",a[x-1])
c=[a[0],a[x-1]]
print("new created array:",c)
OUTPUT:
PRACTICAL-6.1
14
DEPSTAR-CE
Programming in python D20DCE172
AIM: Write a program that asks the user how many Fibonacci numbers to
generate and then generates them. Take this opportunity to think about how
you can use functions. Make sure to ask the user to enter the number of
numbers in the sequence to generate
CODE:
def febo(n):
n1= 0
n2 = 1
count = 0
if n <= 0:
print("enter positive number")
elif n == 1 :
print("end of sequence.")
else:
while count < n:
nth = n1+n2
print(n1)
n1=n2
n2=nth
count += 1
OUTPUT:
15
DEPSTAR-CE
Programming in python D20DCE172
Practical-6.2
16
DEPSTAR-CE
Programming in python D20DCE172
AIM: Write a program (function!) that takes a list and returns a new list that
contains all the elements of the first list minus all the duplicates
CODE:
workout_1 = ["Push Ups", "Pull Ups", "Military Press",
"Chin Ups", "Flys", "Lat Pull Down", "One Arm Pushups",
"V Pull Ups", "Burnout"]
workout_2 = ["Push Ups", "Pull Ups", "Military Press",
"Chin Ups", "Flys", "Wide Pushups", "Heavy Pants",
"Plyometric Pushups", "Lawnmower Pull"]
def remove_duplicates():
todays_workout = []
for i in workout_1:
if i not in workout_2:
workout_2.append(i)
return workout_2
remove_duplicates()
common_exercises = [exercise for exercise in
set(workout_1) if exercise in workout_2]
common_listed = [i for i in common_exercises if
common_exercises.count(i) == 1]
print("Today's workout will be: ", common_listed)
OUTPUT:
17
DEPSTAR-CE
Programming in python D20DCE172
PRACTICAL-6.3
18
DEPSTAR-CE
Programming in python D20DCE172
AIM: - Write a program (using functions!) that asks the user for
a long string function. Containing multiple words. Print back to
the user the same string, except with the words in backwards
order. For example, say I type the string: My name is Michele
Then I would see the string: Michele is name My shown back to
me.
Code:
def phraseReverse(string):
list1 = string.split(' ')
list1.reverse()
list1 = ' '.join(list1)
return list1
question = input('Please enter a phrase: ')
answer = phraseReverse(question)
print(answer)
OUTPUT:
PRACTICAL-7.1
19
DEPSTAR-CE
Programming in python D20DCE172
CODE:
import random
import array
import string
MAX_LEN = 12
dig = string.digits
lcase = string.ascii_lowercase
ucase = string.ascii_uppercase
symbols = string.punctuation
pwd = dig + ucase + lcase + symbols
rand_digit = random.choice(dig)
rand_upper = random.choice(ucase)
rand_lower = random.choice(lcase)
rand_symbol = random.choice(symbols)
temp_pass = rand_digit + rand_upper + rand_lower +
rand_symbol
for x in range(MAX_LEN - 4):
temp_pass = temp_pass + random.choice(pwd)
temp_pass_list = array.array('u', temp_pass)
random.shuffle(temp_pass_list)
password = ""
for x in temp_pass_list:
password = password + x
OUTPUT:
20
DEPSTAR-CE
Programming in python D20DCE172
PRACTICAL-7.2
21
DEPSTAR-CE
Programming in python D20DCE172
AIM: Write a Python class named Circle constructed by a radius and two
methods which will compute the area and the perimeter of a circle.
CODE:
class Circle():
def __init__(self, r):
self.radius = r
def area(self):
return self.radius*self.radius*3.14
def perimeter(self):
return 2*self.radius*3.14
mycircle = Circle(12)
print(mycircle.area())
print(mycircle.perimeter())
OUTPUT:
PRACTICAL-8.1
22
DEPSTAR-CE
Programming in python D20DCE172
AIM: Python supports classes inheriting from other classes. The class being
inherited is called the Parent or Superclass, while the class that inherits is
called the Child or Subclass. How can we definethe order in which the base
classes are searched when executing a method?
CODE:
class f:
def show_f(self):
print("List Of My Friends : ")
class friend1(f):
friend1Name = " "
def show_friend1(self):
print(self.friend1Name)
class friend2(f):
friend2Name = " "
def show_friend2(self):
print(self.friend2Name)
class c1(friend1,friend2):
def show_fs(self):
print("My First Friend Name is:
",self.friend1Name)
print("My Second Friend Name is:
",self.friend2Name)
s1 = c1()
s1.friend1Name = "Tejash"
s1.friend2Name = "Harsh"
s1.show_f()
s1.show_fs()
OUTPUT:
23
DEPSTAR-CE
Programming in python D20DCE172
PRACTICAL-8.2
24
DEPSTAR-CE
Programming in python D20DCE172
AIM: Write a function that takes an ordered list of numbers (a list where the
elements are in order from smallest to largest) and another number. The
function decides whether or not the given number is inside the list and returns
(then prints) an appropriate boolean.
CODE:
OUTPUT:
Practical-8.3
25
DEPSTAR-CE
Programming in python D20DCE172
AIM: Given a .txt file that has a list ofa bunch of names, count how many of
each name thereare in the file, and print out the results to the screen.
CODE:
file = open("u.txt","r")
count = 0
for line in file:
names = line.split(" ")
count += len(names)
file.close()
print("Number of Names in Text File is:",count)
u.txt:
OUTPUT:
PRACTICAL-9.1
26
DEPSTAR-CE
Programming in python D20DCE172
OUTPUT:
Practical-9.2
27
DEPSTAR-CE
Programming in python D20DCE172
def bubbleSort(arr):
n = len(arr)
for i in range(n-1):
for j in range(0, n-i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
arr = [11,54,2,6,80,2,1]
bubbleSort(arr)
print ("Array Sorted Using Bubble Sort:")
for i in range(len(arr)):
print(arr[i])
OUTPUR:
def insertionSort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >= 0 and key < arr[j] :
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
arr = [11,54,2,6,80,2,1]
insertionSort(arr)
print ("Array Sorted Using Insertion Sort: ")
for i in range(len(arr)):
print (arr[i])
Output:
Practical-9.3
29
DEPSTAR-CE
Programming in python D20DCE172
CODE:
def devByzero():
num= 10
try:
a=num/0
except:
print("devide By zoro")
def arrayOutOFBound():
arry =[1,3,44,6567,7,77,4]
num1=10
try:
arry[11]=num1
except:
print("array out of bound")
def FileNotFound():
try:
file = open('tejash.txt','r');
except:
print("File not Found")
devByzero()
arrayOutOFBound()
FileNotFound()
OUTPUT:
Assertion:
30
DEPSTAR-CE
Programming in python D20DCE172
Code:
def checkAge(age):
assert (age >= 18),"You can't vote!"
return "Yeah! you can vote"
print(checkAge(20))
print(checkAge(14))
Output:
31
DEPSTAR-CE
Programming in python D20DCE172
Practical-10
AIM: Introduction to Django- Python based free and open-
source web framework and Flask- Python based micro web
framework.
• Django- Python based free and open-source web
framework: -
Django is a high-level Python Web framework that encourages
rapid development and clean pragmatic design. A Web
framework is a set of components that provide a standard way
to develop websites fast and easily. Django’s primary goal is to
ease the creation of complex databasedriven websites. Some
well-known sites that use Django include PBS, Instagram,
Disqus, Washington Times, Bitbucket and Mozilla.
Advantages of Django: -
• Object-Relational Mapping (ORM) Support − Django provides
a bridge between the data model and the database engine, and
supports a large set of database systems including MySQL,
Oracle, Postgres, etc. Django also supports NoSQL database
32
DEPSTAR-CE
Programming in python D20DCE172
33
DEPSTAR-CE
Programming in python D20DCE172
Advantages of Flask: -
• Jinja2 templating.
• Unicode based.
34
DEPSTAR-CE