0% found this document useful (0 votes)
23 views81 pages

Python Practical File (1)

interview question python
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
23 views81 pages

Python Practical File (1)

interview question python
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 81

Delhi Skill and Entrepreneurship University

(DSEU)
Department of BSc (Data Analytics)
Bhai Parmanand DSEU Shakarpur Campus -II

SEMESTER : - 2 SUBJECT :- PYTHON


SESSION : - 2021-22 SUB CODE :- DTA- DC201

Submitted To:- Dr. Mamta Mittal and priyanka


mam

Submitted By:- Nikita kumari


Roll no. :- 40821075
Section :- A
INDEX
Experiment number Experiment name
1 Write a program to build a calculator
2 write a program to input the radius of circle
from the user and calculate it's area and
perimeter
3 write a program to input the coefficients a, b
and c for a quadratic equation and calculate
the determinant and roots of the same
4 write a program to input the temperature in
Fahrenheit and convert it into Celcius
5 write a program to check if a number is even
or odd
6 calculate simple interest by using input from
user of principle, rate if interest and time
period
7 Write a program to swap two number with
and without taking the third variable.
8 write a program to find the greatest of 3
numbers inputted by the user
9 write a program to find whether a year
entered by user is a leap year or not
10 write a program to input the sides of triangle
from user and decide whether the triangle is
equilateral, isosceles or scalene
11 write a program to input the sides of triangle
from user and decide whether the triangle is
equilateral, isosceles or scalene

iv) %age>=60 (grade A)


%age b/w 51 to 60 (grade B)
%age b/w 41 to 50 (grade C)
%age b/w 31 to 40 (grade D)
otherwise FAIL.
Write a program to input the marks of five
subjects by the user and calculate total marks
of five subjects, and assign grades
accordingly
12
>=20,000 (20% of total bill)
>=15,000 (10% of total bill)
>=10,000 (5% of total bill)
Otherwise NO DISCOUNT
Write a program to input the total bill value
from the user and then calculate the discount
amount, total bill after discount and the
discounted %.
13 write a program to print the table of a
number inputted by the user using while loop
14 write a program to print all the even
numbers in a given range using while loop.
15 write a program to find sum of 'n' natural
numbers using while loop
16 write a program to print all the numbers in a
range given by user except those which are
divisible by 5 (using while loop)
17 write a program to input range from user
and skip any number divisible by 5 and print
only even numbers (except 10,20,30, etc..)
18 factorial of a number
19 print table, value inputted by user (using for
loop)
20 *
**
*
**
***
input number of rows from user and print
this pattern
21 ***
***
***
***
***
input number of rows from user and print
this pattern
22 1
12
123
1234
12345
input number of rows from user and print
this pattern
23 Write a program to print the prime number
in a given range.
24 Write a program to check if a number is
positive or negative.
25 Write a program to check if a number is
palindrome or not.
26 Write a program to find the lowest Common
Multiple (LCM).

25 Greatest Common Divisor (GCD) of two


numbers.
26 Write a program to print prime numbers
between 1 to 50.
27 Write a program to calculate the number of
days between two dates.

28 Write a program to calculate the amount of


simple interest.

29 Write a program to find the factorial of a


number using user defined functions.

30 Write a program to find area of circle using


user defined function.
31 Write a program to find area of triangle
using user defined function.
32 Write a program to find the factorial of a
number using recursive functions.
33 Write a program to convert a number from
decimal to binary.

34 Write a program to find the per kilometer


fare of a cab service including GST.

35 Write a program to build a calculator using


nested user defined functions.

36 Write a program to find area of a circle using


anonymous functions.

37 Write a program to print Fibonacci series


using recursive function.

38 Write a program to print the calendar of a


given year using calendar function.

39 Write a program to find the sum of two


numbers lengths of which should not exceed
80.

40 Tabulate the built-in functions of strings with


examples.

41 Write a program to print all the elements of a


list.

42 Write a program to print all the elements in a


list which are divisible by 5.

43 Write a program to input a list and print it


using a user defined function.

44 Write a program to concatenate two lists.


Write a program to repeat a list.

45 Write a program to print index of the input


element in a list.

46 Tabulate some of the important list functions.

47 Write a program to add all the elements in a


list.
48 Write a program to return prime number in
a tuple.

49 Write a program to return even indexed


number in a tuple.

50 Write a program to sort a tuple in increasing


or decreasing order.
51 Write a program to remove duplicates from a
tuple.
52 Write a program to add and remove items
from set.
53 Write a program to remove an item from set
if it is present in set.
54 Write a program to create intersection,
union, difference of two sets.
55 Write a program to create intersection,
union, difference of two frozen sets.
56 Write a program to study built in methods
related to set.
57 Write a program to find length of a set using
loops.
58 Write a program to remove intersection of a
2nd set from 1st.
59 Write a program to find the element in a
given set that are not in another set using
loops, conditional statement and functions.
60 Write a python program to get the path and
name of the file that is currently executing

61 WAP to check if a particular file exists and


print its file size

62 WAP to search a particular string in text file.

63 sum of square and cube of first 100 natural


number

64 find all odd numbers and evens number and


amstrom in the range 1-500

65 Fibonnaci Series

66 Write a python program to determine if a


python shell is executing in 32 bit or 64 bit
mode.

67 WAP to count no. of lines in file


68 WAP to read specific lines in a file
69 WAP to make a tic taco game
Write a program to Build a calculator using python.
num = int(input("enter the number"))
num1 = int(input("enter the number"))
oper = input("enter the oprator")
if oper == "+" :
print(num+num1)
elif oper == "-":
print(num-num1)
elif oper == "*":
print(num*num1)
elif oper == "/":
print(num/num1)
DAY:- 2
Objective: write a program to input the radius of the
circle from the user and calculate its area,
circumference, diameter.
Prodecure:
Code:-
#calculate the area , circumference, and diameter
r=int(input('enter the value of radius'))
a=3.14*r**2
print("area",a)
c=2*3.14*r
print("circumference",c)
d=2*r
print("diameter",d)

Output:-
Objective: write a program to input the coefficients a,
b and c for a quadratic equation and calculate the
determinant and roots of the same
Prodecure:
Code:-
#quadratic equation
a=int(input('enter the value of a'))
b=int(input('enter the value of b'))
c=int(input('enter the value of c'))
d=b**2-4*a*c
print(d)
if d>0:
r=(-b+d**1/2)/2*a
f=(-b-d**1/2)/2*a
print('roots are real and uneqaul with roots',r,f)
elif d==0:
h=-b/2*a
print("roots are real and equal with having roots",h,h)
elif d<0:
print("roots are not real”)

Output:
Objective: write a program to input the temperature
in Fahrenheit and convert it into Celcius
Prodecure:
Code:-
#temperature conversion
tem=int(input("enter the value of temperature in
Fahrenhei: "))
t=(tem-32)*5/9
print("temperature in Celsius: ",t)
print("-----------------")

Output:-
Objective: calculate simple interest by using input
from user of principle, rate if interest and time period
Prodecure:
Code:-
#simple interest
p=int(input("enter the value of p: "))
r=int(input("enter the value of r: "))
t=int(input("enter the value of t(in months): "))
si=(p*r*t)/100
print("simple interest: ",si)

Output
Write a program to swap two number with and
without taking a third variable

a,b = 1,2
print(a,b)
b,a = 2,1
print(b,a)
DAY 3
1:- write a program to find the greatest of 3 numbers
inputted by the user
a=int(input("enter the number: "))
b=int(input('enter the second number: '))
c=int(input('enter the third: '))

if a>b and a>c:


print(a, "is greater than all two")
elif b>a and b>c:
print(b ,"is greater than all two")
else:
print(c, "is greater than all two")
2:- write a program to find whether a year entered by
user is a leap year or not
year=int(input("enter year"))
if year%4==0:
if year%100==0:
if year%400==0:
print("yes it is a leap year")
else:
print("it is not a leap year")
else:
print("yes it is a leap year")
else:
print("no, it is not a leap year")
iii) write a program to input the sides of triangle from
user and decide whether the triangle is equilateral,
isosceles or scalene
a=float(input("enter the length of side1: "))
b=float(input("enter the length of side2: "))
c=float(input("enter the length of side3: "))
if a==b and b==c and c==a:
print("triangle is equilateral")
elif a==b or b==c or c==a:
print("triangle is isosceles")
else:
print("triangle is scalene")
iv) %age>=60 (grade A)
%age b/w 51 to 60 (grade B)
%age b/w 41 to 50 (grade C)
%age b/w 31 to 40 (grade D)
otherwise FAIL.
Write a program to input the marks of five subjects by the user and
calculate total marks of five subjects, and assign grades accordingly
eng=input("enter english marks: ")
math=input("enter math marks: ")
history=input("enter history marks: ")
hindi=input("enter hindi marks: ")
total=float(eng)+float(math)+float(history)+float(hindi)
per=((total/400)*100)
print("-------student result------")
print("total marks",total)
print("percentage: ",per,"%")
if per<0 or per>100:
print("error")
elif per>=60:
print("grade A")
elif per>51 and per<60:
print("GRADE:- B")
elif per>41 and per<50:
print("GRADE:- C")
elif per>31 and per<40:
print("GRADE:- D")
else:
print("fail!!!!")
v) >=20,000 (20% of total bill)
>=15,000 (10% of total bill)
>=10,000 (5% of total bill)
Otherwise NO DISCOUNT
Write a program to input the total bill value from the user and then
calculate the discount amount, total bill after discount and the
discounted %.
bill=float(input("enter the total amount"))
if bill>=20000:
dis=(20/100)*bill
print("discount amount", dis)
print("total bill",bill-dis)
print("discounted % :- 20%")
elif bill>=15000:
dis=(10/100)*bill
print("discount amount", dis)
print("total bill",bill-dis)
print("discounted % :- 10%")
elif bill>=10000:
dis=(5/100)*bill
print("discount amount", dis)
print("total bill",bill-dis)
print("discounted % :- 5%")
else:
print("no discount")
print("total bill",bill)
DAY 4
write a program to print the table of a number
inputted by the user using while loop
table = int(input("enter the number :- "))
i=1
while i<=10:
t=i*table
print(table,"x",i,"=",t)
i=i+1
write a program to print all the even numbers in a
given range using while loop.
le=0
while le<=20:
le=le+1
if le%2==0:
print(le)
else:
continue
write a program to find sum of 'n' natural numbers
using while loop

t=int(input("enter the number of terms:- "))


i=0
e=0
while e<=t:
i=i+e
e=e+1
print("sum of",t, "terms","is equal to",i)
write a program to print all the numbers in a range
given by user except those which are divisible by 5
(using while loop)
a = int(input("enter "))
i = int(input("enter "))
while i>=a:
a=a+1
if a%5 == 0:
continue
print(a)
write a program to input range from user and skip any
number divisible by 5 and print only even numbers
(except 10,20,30, etc..)

a = int(input("enter "))
i = int(input("enter "))
while i>=a:
a=a+1
if a%5 == 0:
continue
elif a%2 == 0:
print(a)
factorial of a number

a= int(input("enter the number"))


fac=1
if a<0:
print("enter a postive number")
elif a==0:
print("factorial of zero if 1")
else:
while a>0:
fac=fac*a
a=a-1
print(fac)
print table, value inputted by user (using for loop)

a= int(input("enter the table you want to enter"))


for i in range(1,11):
print(a ,"x",i,"=",a*i)
*
**
***
****
*****
input number of rows from user and print this pattern
n=int(input("enter the range"))
for i in range(n):
for j in range(i):
print("*",end=" ")
print()
***
***
***
***
***
input number of rows from user and print this pattern
n=int(input("enter the range"))
for i in range(n):
print("*"*5)
1
12
123
1234
12345
input number of rows from user and print this pattern

n=int(input("enter the range"))


for i in range(n+1):
for j in range(1,i+1):
print(j,end=" ")
print()
Write a program to print the prime numbers in a
given range
i = int(input("enter the number"))
s= 0
while i>=s:
s =s+1
li = list()
for x in range(1,s+1):
if s%x == 0:
li.append(x)
if 1 in li and s in li and len(li) == 2 :
print(s,"is prime number")
Write a program to check if a number is positive or
negative.

i = int(input("enter the number"))


if i<0:
print(i ,"is a negative number")
else:
print(i,"is a positive number")
Write a program to check if a number is palindrome
or not.
w = input("enter the number")
lis = ''
for x in w:
lis = x+lis
if w == lis:
print("yes",w,"this is a palindrome number")
else :
print("no",w,"is not a palindrome number")
Write a program to find the lowest Common Multiple
(LCM).
i = int(input("enter the number"))
s= 1
li = list()
for x in range(1,i):
if i%x == 0:
li.append(x)
print(li)
for i in li:
s = s*i
print(s)
Greatest Common Divisor (GCD) of two numbers.
num1 = 36
num2 = 60
hcf = 1

for i in range(1, min(num1, num2)):


if num1 % i == 0 and num2 % i == 0:
hcf = i
print("Hcf of", num1, "and", num2, "is", hcf)
Write a program to print prime numbers between 1 to
50.
i = 50
s= 0
while i>=s:
s =s+1
li = list()
for x in range(1,s+1):
if s%x == 0:
li.append(x)
if 1 in li and s in li and len(li) == 2 :
print(s,"is prime number")
Write a program to calculate the number of days
between two dates.

from datetime import date


def numOfDays(date1, date2):
return (date2-date1).days

# Driver program
date1 = date(2020, 12, 13)
date2 = date(2021, 2, 25)
print(numOfDays(date1, date2), "days")
Calculate total salary of the person and tax is 15% of
the basic which is to be deducted.

a=int(input("Enter the basic salary:"))


tax=(15/100)*a
print('Amount to be paid as tax:',tax)
print("Salary after deducting tax amount:",a-tax)
Write a program to find the factorial of a number
using user defined functions.
def fact(n):
fact=1
a=1
while a<=n:
fact*=a
a+=1
print("Factorial of",n,"is:",fact)
fact(20)
Write a program to find area of circle using user
defined function.
def AOC():
r=int(input("Enter Radius of Circle:"))
area=3.14*r**2
print("Area of Circle is:",area)
AOC()
Write a program to find area of triangle using user
defined function.
def AOT():
b=int(input("Enter Base:"))
h=int(input("Enter height:"))
area=int(1/2*b*h)
print("Area of triangle :",area)
AOT()
Write a program to find the factorial of a number
using recursive functions.
def fact(n):
if n == 1:
return n
else:
return n*fact(n-1)
num = int(input("Enter Number:"))
if num < 0:
print("factorial does not exist for negative numbers.")
elif num == 0:
print("The factorial of 0 is 1.")
else:
print("The factorial of",num, "is:", fact(num))
Write a program to convert a number from decimal to
binary.
dec=int(input("Enter Decimal Number:"))
print('Binary representation of',dec,"is:",bin(dec)[2:])
Write a program to find the per kilometer fare of a
cab service including GST.
print("Fare per kilometer is 750rs and GST is 2.5%:")
dis=int(input("Enter distance traveled:"))
fare=dis*750
final_fare=fare+fare*(25/1000)
print("Final Fare including GST is:",int(final_fare),"₹")
Write a program to build a calculator using nested
user defined functions.
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b
a,b=map(int,input("Enter two numbers separated by space:").split())
print("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division")
op=int(input("Choose Operation:"))
if op==1:
print('Addition of',a,"&",b,":",add(a,b))
elif op==2:
print('Subtraction of',a,"&",b,":",sub(a,b))
elif op==3:
print('Multiplication of',a,"&",b,":",mul(a,b))
elif op==4:
print('Division of',a,"&",b,":",div(a,b))
else:
print("Invail Choice.")
Write a program to find area of a circle using
anonymous functions.
area_of_circle=lambda r=int(input("Enter
Radius:")):3.14*(r**2)
print('Area of circle:',area_of_circle())
Write a program to print Fibonacci series using
recursive function.
def fibo(n):
if n<=1:
return n
else:
return (fibo(n-1)+fibo(n-2))
t=int(input("Enter No. of terms:"))
if t<=0:
print("Enter Vaild Number.")
else:
for u in range(t):
print(fibo(u),end=" ")
Write a program to print the calendar of a given year
using calendar function.
from calendar import *
y=int(input("Enter Year:"))
print ("The calendar of year",y," is : ")
print (calendar(y))
Write a program to find the sum of two numbers
lengths of which should not exceed 80.
Input:

a=int(input("Enter 1st no. :"))


b=int(input("Enter 2nd no. :"))
x=a+b
if x>=80 or a>=80 or b>=80:
print("Overflow")
else:
print('Sum of two numbers is:',x)
Tabulate the built-in functions of strings with examples.

Extract N number of characters from


mystring[:N]
start of string.

Extract N number of characters from


mystring[-N:]
end of string

Extract characters from middle of


mystring[X:Y] string, starting from X position and
ends with Y

str.split(sep=' ') Split Strings

str.replace(old_substring, Replace a part of text with different


new_substring) sub-string

str.lower() Convert characters to lowercase

str.upper() Convert characters to uppercase

str.contains('pattern', Check if pattern matches (Pandas


case=False) Function)

Return matched values (Pandas


str.extract(regular_expression)
Function)

str.count('sub_string') Count occurrence of pattern in string

str.find( ) Return position of sub-string or pattern

Check whether string consists of only


str.isalnum()
alphanumeric characters

Check whether characters are all lower


str.islower()
case
Check whether characters are all upper
str.isupper()
case

Check whether string consists of only


str.isnumeric()
numeric characters

Check whether string consists of only


str.isspace()
whitespace characters

len( ) Calculate length of string


Write a program to print all the elements of a list.

L=[2,4,5,"Hi",74,"Hello","Good Night"]
for u in L:
print(u)
Write a program to print all the elements in a list
which are divisible by 5.

Input:
L1=[5,4,9,10,45,89,45445,789,895,963,67,70]
print("Elements Divisible by 5:")
for i in L1:
if i%5==0:
print(i)
Write a program to input a list and print it using a user defined function.

Input:
def prn(n):
print(n)
#main
l=list(input("Enter Elements of the list:"))
prn(l)
Write a program to concatenate two lists and to
repeat a list.
print("1.Concatenation\n2.Repeatition\n3.Slicing\n4.Member
ship")
L3=[1,5,6,88,445,454,"Hello"]
L4=[51,848,51,151,515,151,55,51,212121]
print("Your lists:\n1.",L3,"\n2.",L4)
#main
ans="y"
while ans=="y":
q=int(input("Enter Your Choice:"))
if q==1:
print("Concatenation of both list is:",L3+L4)
elif q==2:
po=int(input("Choose list:"))
og=int(input("How many times you want to repeat:"))
if po==1:
print(L3*og)
elif po==2:
print(L4*og)
else:
print("wrong input.")
elif q==3:
lc=int(input("Choose list:"))
fi=int(input("enter starting point:"))
la=int(input("enter ending point:"))
if lc==1:
print(L3[fi:la])
elif lc==2:
print(L4[fi:la])
else:
print("wrong input.")
elif q==4:
lc1=int(input("Choose list:"))
print("Search type\n1.string\n2.numeric")
sea_type=int(input("what you want to search:"))
if sea_type==1 and lc1==1:
st=input("Enter String:")
if st in L3:
print(st,"is present in the list.")
else:
print(st,"is not present in the list.")
elif sea_type==1 and lc1==2:
st1=input("Enter String:")
if st1 in L3:
print(st1,"is present in the list.")
else:
print(st1,"is not present in the list.")
elif sea_type==2 and lc1==1:
int1=int(input("Enter number:"))
if int1 in L3:
print(int1,"is present in the list.")
else:
print(int1,"is not present in the list.")
elif sea_type==2 and lc1==2:
int2=int(input("Enter number:"))
if int2 in L3:
print(int2,"is present in the list.")
else:
print(int2,"is not present in the list.")
else:
print("wrong input.")
else:
print("wrong input.")
ans=input("Do you want to continue:\ny/n:")
if ans=="n" or ans=="N":
print("tata bye bye :)")
Write a program to print index of the input element in
a list.

L5=[45,545,6,848,12,55,484]
Choose=int(input("Search number:"))
if Choose in L5:
for z in range(len(L5)):
if L5[z]==Choose:
print("Entered number is present and index
is:",z)
else:
print("Entered number is not in the list")
Tabulate some of the important list functions.

Method Description
append() Adds an element at the end of
the list
clear() Removes all the elements from
the list
copy() Returns a copy of the list
count() Returns the number of elements
with the specified value
extend() Add the elements of a list to the
end of the current list
index() Returns the index of the first
element with the specified value
insert() Adds an element at the specified
position
pop() Removes the element at the
specified position
remove() Removes the first item with the
specified value
reverse() Reverses the order of the list
sort() Sorts the list
Write a program to add all the elements in a list.

L2=[4,8,6,245,9,44,25,54,32,56,78,45,4514,956]
def add_list():
sum=0
for k in L2:
sum+=k
print("sum of all element is:",sum)
print("Given list:",L2)
add_list()
Write a program to return prime number in a tuple.
t1=(2,4,6,5,48,89,81,77,11,52)
print("All prime number present in tuple are:")
for ele in t1:
condi=True
for u in range (2,ele):
if ele%u==0:
condi=False
break
if condi:
print(ele)
Write a program to return even indexed number in a
tuple.

Input:

t2=(2,4,6,5,48,89,81,77,11,52,5454,74,545)
print("All even indexed elements in the tuple are:")
for index in range(0,len(t2)):
if index%2==0:
print(t2[index])
Write a program to sort a tuple in increasing or
decreasing order.

Input:

t3=(-1,2,5,9,55,15,-10,0,9,18)
print("Given Tuple:",t3)
print("Sorted Tuple in Ascending Order:",sorted(t3))
print("Sorted Tuple in Descending
Order:",sorted(t3,reverse=True))
Write a program to remove duplicates from a tuple.

Input:

t4=(12,8,45,-15,12,84,-15,8,56)
print("Original Tuple:",t4)
print("Tuple after removing all
duplicates:",tuple(set(t4)))
Write a program to add and remove items from set.

Input:

s1={7,8,0,2,"hello"}
print(s1)
s1.discard(7)
s1.add("bye")
print(s1)
Write a program to remove an item from set if it is
present in set.

Input:
s1={7,8,0,2,"hello"}
a=60
if a in s1:
s1.discard(a)
print(s1)
else:
s1.add(a)
print(s1)
Write a program to create intersection, union,
difference of two sets.

Input:
s1={7,8,0,2,"hello"}
s2={84,18,73,7,0}
s3=s1.union(s2)
s4=s1.intersection(s2)
s5=s1.difference(s2)
print("union",s3)
print("intersection",s4)
print("difference",s5)
Write a program to create intersection, union,
difference of two frozen sets.

Input:

set1=frozenset(s3)
set2=frozenset(s4)
uni=set1.union(set2)
diff=set1.difference(set2)
inter=set1.intersection(set2)
print("union",uni)
print("intersection",diff)
print("difference",inter)
Write a program to study built in methods related to
set.

Input:

print(dir(set))
Write a program to find length of a set using loops.

Input:

from itertools import count


s1={7,8,0,2,"hello"}
for i in s1:
count=+1
print('Q6 lenght of set is:',count)
Write a program to remove intersection of a 2nd set
from 1st.
Input:

set3={1,2,35,56,6,90}
set4={99,218,56,3,2,6}
set5=set3.copy()
for ele5 in set3:
if ele5 in set4:
set5.discard(ele5)
print('Q7 new set:',set5)
Write a program to find the element in a given set
that are not in another set using loops, conditional
statement and functions.

Input:

set3={1,2,35,56,6,90}
set4={99,218,56,3,2,6}
set6=set()
for ele3 in set3:
if ele3 not in set4:
set6.add(ele3)
print(‘Old set’,set3)
print(“New Set”,set6)
Write a python program to get the path and name of
the file that is currently executing
import os

print('getcwd: ', os.getcwd())


print('__file__: ', __file__)
WAP to check if a particular file exists and print its file
size
import os.path as pa
e = input("enter the name of file")
try:
fhandle = open(e,"r")
print("yes we found it!!")
#get size
size = pa.getsize(e)
print(size)
except:
print("file not found")
WAP to search a particular string in text file.
i = input("enter the file name ")
lines = open(i,"r")
cou = 0
for line in lines:
cou = cou + 1
if "From" in line:
print("we found it 'From' in line",cou)
sum of square and cube of first 100 natural number
t=int(input("enter the number of terms:- "))
i=0
e=0
j=0
while e<=t:
i=i+e*e
j=j+e*e*e
e=e+1
print("sum of square of ",t, "terms","is equal to",i)
print("sum of cube of ",t, "terms","is equal to",j)
print("----------------")
print(" ")
find all odd numbers and evens number and amstrom
in the range 1-500

i = int(input("enter "))
for t in range(i):
if t%2 == 0:
print( t , "is even")
else :
print(t , "is odd")
su = 0
te = t
while te>0:
digit = te%10
su += digit**3
te //= 10
if t == su:
print(t,"is armstrong")
Fibonnaci Series
n=int(input('Enter the value of n:'))
a=0
b=1
sum=0
count=1
print('Fibonnaci Series:',end='')
while(count<=n):
print(sum,end='')
count+=1
a=b
b=sum
sum=a+b
Output:
Write a python program to determine if a python
shell is executing in 32 bit or 64 bit mode.

import platform
print(platform.architecture()[0])
WAP to count no. of lines in file WAP to count no. of
lines in file
i = input("enter the file name ")
lines = open(i,"r")
cou = 0
for line in lines:
cou = cou + 1
print("total no.of lines is", cou)
WAP to read specific lines in a file.
i = input("enter the file name ")
lines = open(i,"r")
cou = str()
for line in lines:
if line.startswith("From"):
cou = line

print(" lines is", cou)


WAP to make a tic taco game
import random
import sys
board=[i for i in range(0,9)]
player, computer = '',''
moves=((1,7,3,9),(5,),(2,4,6,8))
winners=((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))
# Table
tab=range(1,10)
def print_board():
x=1
for i in board:
end = ' | '
if x%3 == 0:
end = ' \n'
if i != 1: end+='---------\n';
char=' '
if i in ('X','O'): char=i;
x+=1
print(char,end=end)
def select_char():
chars=('X','O')
if random.randint(0,1) == 0:
return chars[::-1]
return chars
def can_move(brd, player, move):
if move in tab and brd[move-1] == move-1:
return True
return False
def can_win(brd, player, move):
places=[]
x=0
for i in brd:
if i == player: places.append(x);
x+=1
win=True
for tup in winners:
win=True
for ix in tup:
if brd[ix] != player:
win=False
break
if win == True:
break
return win
def make_move(brd, player, move, undo=False):
if can_move(brd, player, move):
brd[move-1] = player
win=can_win(brd, player, move)
if undo:
brd[move-1] = move-1
return (True, win)
return (False, False)
# AI goes here
def computer_move():
move=-1
# If I can win, others don't matter.
for i in range(1,10):
if make_move(board, computer, i, True)[1]:
move=i
break
if move == -1:
# If player can win, block him.
for i in range(1,10):
if make_move(board, player, i, True)[1]:
move=i
break
if move == -1:
# Otherwise, try to take one of desired places.
for tup in moves:
for mv in tup:
if move == -1 and can_move(board, computer, mv):
move=mv
break
return make_move(board, computer, move)
def space_exist():
return board.count('X') + board.count('O') != 9
player, computer = select_char()
print('Player is [%s] and computer is [%s]' % (player, computer))
result='%%% draw !! %%%'
while space_exist():
print_board()
print('# Make your move ! [1-9] : ', end='')
move = int(input())
moved, won = make_move(board, player, move)
if not moved:
print(' >> Invalid number ! Try again !')
continue
if won:
result='* Congratulations ! You won ! *'
break
elif computer_move()[1]:
result='=== You lose ! =='
break;
print_board()
print(result)

You might also like