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

Computer Science With Python

This document contains 6 programming questions and their solutions in Python. Each question is about a different Python concept: 1) Loops - To find the sum of squares of digits in a number 2) Loops - To print the Fibonacci series up to a given number of terms 3) Lists - To separate elements of a list into even and odd indexes 4) Lists - To swap elements at even and odd indexes of a list 5) Tuples - To create a list of tuples from a given list with number and its cube 6) Tuples - To find max, min, and sort a tuple For each question, the code and sample output is given.

Uploaded by

adsaditya24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Computer Science With Python

This document contains 6 programming questions and their solutions in Python. Each question is about a different Python concept: 1) Loops - To find the sum of squares of digits in a number 2) Loops - To print the Fibonacci series up to a given number of terms 3) Lists - To separate elements of a list into even and odd indexes 4) Lists - To swap elements at even and odd indexes of a list 5) Tuples - To create a list of tuples from a given list with number and its cube 6) Tuples - To find max, min, and sort a tuple For each question, the code and sample output is given.

Uploaded by

adsaditya24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

COMPUTER SCIENCE WITH PYTHON

PROGRAM FILE

Name : Priyanshu Malik


Class : XIth E
Roll no. : 26
INDEX

Sr. QUESTIONS signature


1 WAP accept any number from user and display sum of the square of each
digit (LOOP)
2 WAP to print Fibonacci Series (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...) till n terms.
(Accept n from the user) (LOOP)
3 WAP ALL[ ] to two list Odd[ ]and Even[].The Even should contain values
from places (0,2,4,………) of ALL[] and Odd[] should contain values from
places ( 1,3,5,……….). (LIST)
4 WAP in python and swap the elements of every even location with its
following odd location. Example : If an list of nine elements initially contains
the elements as 2,4,1,6,5,7,9,23,10 rearranges the list as as
4,2,6,1,7,5,23,9,10 (LIST)
5 WAP to create a list of tuples from given list having number and
its cube in each tuple. (TUPLE)
6 WAP to create a tuple and display its maximum ,minimum value
and display it in ascending order. (TUPLE)
Q1. WAP accept any number from user and display sum of the square
of each digit (LOOP)
Code:

c=0 OUTPUT:

n=input('enter the number:') enter the number:21

5
for i in n:

d=int(i)

d=d**2

c=c+d

print(c)

1.
Q2. WAP to print Fibonacci Series (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...) till n
terms. (Accept n from the user)
CODE:
OUTPUT:
n1=0
enter the no of terms u want:10
n2=1
fibonacci series
c=0
0
n=int(input('enter the no of terms u want:'))
1
if n<=0:
1
print('error')

elif n==1: 2

print(n1) 3

else: 5

print('fibonacii series') 8

while c<n: 13

print(n1) 21
d=n1+n2 34
n1=n2

n2=d

c=c+1

2.
Q3. WAP ALL[ ] to two list Odd[ ]and Even[].The Even should contain
values from places (0,2,4,………) of ALL[] and Odd[] should contain
values from places ( 1,3,5,……….).
CODE:
OUTPUT:
ALL=[]
enter the no of terms:5
Even=[]
enter the elements of the list:1
Odd=[]
enter the elements of the list:2
c=int(input('enter the no of terms:'))
enter the elements of the list:3
for i in range(c):
enter the elements of the list:4
l=int(input('enter the elements of the list:'))
enter the elements of the list:5
ALL.append(l)
ALL: [1, 2, 3, 4, 5]
for i in ALL:
Even: [2, 4]
if i%2==0:
Odd: [1, 3, 5]
Even.append(i)

else:

Odd.append(i)

print('ALL:',ALL)

print('Even:',Even)

print('Odd:',Odd)

3.
Q4. Write a code in python and swap the elements of every even
location with its following odd location. Example : If an list of nine
elements initially contains the elements as 2,4,1,6,5,7,9,23,10
rearranges the list as as 4,2,6,1,7,5,23,9,10
CODE:

l=[]
OUTPUT:
c=int(input('enter the no of terms in the list:')) enter the no of terms in the list:6
for i in range(c): enter the elements of the list:1

n=input('enter the elements of the list:') enter the elements of the list:hood

l.append(n) enter the elements of the list:2

print('original list:',l) enter the elements of the list:any

for i in range (len(l)): enter the elements of the list:3

if i%2==0: enter the elements of the list:house

t=l[i] original list: ['1', 'hood', '2', 'any', '3', 'house']

new list: ['hood', '1', 'any', '2', 'house', '3']


l[i]=l[i+1]

l[i+1]=t

print('new list:',l)

4.
Q5. WAP program to create a list of tuples from given list having
number and its cube in each tuple

CODE:

l=[] OUTPUT:

c=[] enter any number:1

enter any number:2


for i in range(3):
enter any number:3
n=int(input('enter any number:'))
[1, 2, 3]
l.append(n)
((1, 1), (2, 8), (3, 27))
print(l)

for i in l:

d=i**3

c.append((i,d))

e=tuple(c)

print(e)

5.
Q6.WAP to create a tuple and display its maximum ,minimum value
and display it in ascending order.
CODE:
l=[]
OUTPUT:
c=[]
enter any number:210
for i in range(3): enter any number:12
n=int(input('enter any number:')) enter any number:68

l.append(n) original tuple: (210, 12, 68)

print('original tuple:',tuple(l)) maximum value in tuple: 201

print( 'maximum value in tuple:',max(l)) minimum value in tuple: 12

print('minimum value in tuple:',min(l)) tuple in ascedning order: [12, 68, 210]

print('tuple in ascedning order:',sorted(l))

6.

You might also like