0% found this document useful (0 votes)
11 views5 pages

Pti Lab 2

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

Pti Lab 2

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

Lab Assignment 2

[Bt23ECI024]

Q1:

Write a Python program to print


the following pattern.

0
10
010
1010
01010

n=int(input("Enter n: "))

for i in range(0,n+1):

for j in range(0,i):

if((i+j)%2==0):

print("1",end='')

else:

print("0",end='')

print(" ")
Q2: Write a Python program that takes input from user do the following
1.
Number of Digits

2.
Sum of Digits

3.
Squared sum of digits

n=int(input("Enter the number: "))

num=n

sum=0

sq=0

c=0

while(n>0):

r=n%10

sum+=r

sq+=r*r

c=c+1

n=n//10

print("Number of digits: ",c)

print("Sum of digits: ",sum)

print("Squared sum of digits: ",sq)


Q3: Write a Python Program that will input a list of numbers and return the list with two lists: one
is even and the other is odd.

n=int(input("Enter the length of list: "))

list=[]

for i in range(0,n):

list.append(int(input("Enter number: ")))

print(list)

even=[]

odd=[]

for j in list:

if j%2==0:

even.append(j)

else:

odd.append(j)

print("Even list: ",even)

print("Odd list: ",odd)


Q4: Write a Python Program to take a
list of words as an input and return reversed words of the list.

Original list of words: ['hello', 'world', 'python',


'programming']

Reversed words: ['olleh', 'dlrow', 'nohtyp', 'gnimmargorp']

def reverse_words(words_list):

reversed_list = []

for word in words_list:

reversed_word = word[::-1] #slicing, reversing a string

reversed_list.append(reversed_word)

return reversed_list

input_words = input("Enter a list of words separated by spaces: ")

words_list = input_words.split()

reversed_words = reverse_words(words_list)

print("Original list of words:", words_list)

print("Reversed words:", reversed_words)


Q5: Write a function to rotate a list to the right by a given number of steps. For example, given [1, 2,
3, 4, 5] and k = 2, the output should be [4, 5, 1, 2, 3]

def rotate_list_right(lst, k):

# Handle cases where k is greater than the length of the list

k = k % len(lst)

# Rotate the list

rotated_list = lst[-k:] + lst[:-k]

return rotated_list

# Take user input for the list of numbers

input_list = input("Enter the list of numbers separated by spaces: ")

# Convert the input string into a list of integers

lst = [int(x) for x in input_list.split()]

# Take user input for the number of steps

k = int(input("Enter the number of steps to rotate the list to the right: "))

# Rotate the list

rotated_list = rotate_list_right(lst, k)

# Display the results

print("Original list:", lst)

print("Rotated list:", rotated_list)

You might also like