Chapter 3 Lists, functions, tuples and dictionaries, Sets - BCS Guruji

Ad

Saturday, December 9, 2023

Chapter 3 Lists, functions, tuples and dictionaries, Sets

Q. List out main differences between lists & tuple.

Mutability: Lists are mutable, meaning their elements can be modified after creation. Tuples are immutable, and once created, their elements cannot be changed.

Syntax: Lists are defined using square brackets [], while tuples use parentheses ().

Performance: Tuples generally have better performance than lists, especially in terms of iteration and memory usage.

Methods: Lists have more built-in methods compared to tuples because of their mutability.


Q.  Demonstrate set with example.

# Set Example

fruits = {"apple", "banana", "orange"}

print(fruits)


# Adding an element to the set

fruits.add("grape")

print(fruits)


# Removing an element from the set

fruits.remove("banana")

print(fruits)


- Python is case sensitive language. Comment.
Yes, Python is case-sensitive. Variable names variable and Variable are considered different.


- Write a python program to calculate XY.

# Python Program to Calculate XY

x = 2

y = 3

result = x ** y

print(result)


- Write a python program to accept a number and check whether it is

 perfect number or not.

# Python Program to Check Perfect Number

def is_perfect_number(num):

    divisors_sum = sum(i for i in range(1, num) if num % i == 0)

    return divisors_sum == num


number = int(input("Enter a number: "))

if is_perfect_number(number):

    print(f"{number} is a perfect number.")

else:

    print(f"{number} is not a perfect number.")



-Demonstrate list slicing.

# List Slicing Demonstration

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

subset = numbers[2:7]  # Elements from index 2 to 6

print(subset)


 -A tuple is ordered collection of items. Comment.
Yes, a tuple is an ordered collection of items. The order of elements in a tuple is preserved.


 -Write a recursive function in Python to display addition of digits in

 single digit.

def sum_digits(n):

    # Base case: single-digit number

    if n < 10:

        return n

    else:

        return sum_digits(sum(map(int, str(n))))


result = sum_digits(12345)

print(f"Sum of digits in single digit: {result}")


 -Write a program in python to accept 'n' integers in a list, compute &

 display addition of all squares of these integers.

n = int(input("Enter the value of n: "))

numbers = [int(input(f"Enter integer {i + 1}: ")) for i in range(n)]

sum_squares = sum(x**2 for x in numbers)

print(f"Addition of squares: {sum_squares}")


- Explain the function enumerate( ).
The enumerate() function in Python is used to iterate over a sequence (such as a list) while keeping track of the index of the current item. It returns pairs of the form (index, value).


-- Explain the extend method of list.

- What are required arguments in function?

- Explain any 2 built-in list functions.

  • Write a Python program to print even length words in a string.
  • Write a Python program to check if a given key already exists in a dictionary.

Write a Python program to find GCD of a number using recursion.


No comments:

Post a Comment