Doubts Solution

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Object Comparison:

● Write a Python program that creates two lists with the same elements. Use
the identity operator is to check if both lists refer to the same object in
memory. Print the result.

# Create two lists with the same elements

list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 3, 4, 5]
are_same_object = list1 is list2

# Print the result


print("Do list1 and list2 refer to the same object in memory?", are_same_object)
Variable Comparison:

● Create two variables x and y and assign the same integer value to both. Use
the identity operator is to check if both variables refer to the same object in
memory. Print the result.

# Assign the same integer value to two variables


x = 10
y = 10

# Use the identity operator `is` to check if both variables refer to the same object in
memory
are_same_object = x is y

# Print the result


print("Do x and y refer to the same object in memory?", are_same_object)

String Comparison:

● Declare two string variables str1 and str2 with the same content. Use the
identity operator is to check if both strings refer to the same object in
memory. Print the result.

# Assign the same string value to two variables


str1 = "hello"
str2 = "hello"
# Use the identity operator `is` to check if both variables refer to the same object in
memory
are_same_object = str1 is str2

# Print the result


print("Do str1 and str2 refer to the same object in memory?", are_same_object)

List Object Creation:

● Write a Python program that creates two empty lists using different methods
(e.g., list() constructor and []). Use the identity operator is to check if both
lists refer to the same object in memory. Print the result.

# Create two empty lists using different methods


list1 = list()
list2 = []

# Use the identity operator `is` to check if both lists refer to the same object in
memory
are_same_object = list1 is list2

# Print the result


print("Do list1 and list2 refer to the same object in memory?",
are_same_object)

None Comparison:

● Create a variable x and assign None to it. Use the identity operator is to check
if x is None. Print the result.

# Assign None to a variable


x = None

# Use the identity operator `is` to check if x is None


is_none = x is None

# Print the result


print("Is x None?", is_none)
Object Reassignment:

● Write a Python program that creates a list list1 and assigns it to another
variable list2. Then, modify list1 by appending an element. Use the identity
operator is to check if list1 and list2 still refer to the same object in
memory. Print the result.
# Create a list and assign it to another variable
list1 = [1, 2, 3]
list2 = list1

# Modify list1 by appending an element


list1.append(4)

# Use the identity operator `is` to check if list1 and list2 refer to the same
object in memory
are_same_object = list1 is list2

# Print the result


print("Do list1 and list2 refer to the same object in memory?",
are_same_object)
print("list1:", list1)
print("list2:", list2)

Comparison with Immutable Types:

● Declare two variables a and b and assign the same integer value to both. Use
the identity operator is to check if a and b refer to the same object in memory.
Print the result.

# Assign the same larger integer value to two variables


a = 1000
b = 1000

# Use the identity operator `is` to check if both variables refer to the same
object in memory
are_same_object = a is b

# Print the result


print("Do a and b refer to the same object in memory?", are_same_object)

Dictionary Comparison:

● Write a Python program that creates two dictionaries with the same key-value
pairs. Use the identity operator is to check if both dictionaries refer to the
same object in memory. Print the result.

# Create two dictionaries with the same key-value pairs


dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'a': 1, 'b': 2, 'c': 3}

# Use the identity operator `is` to check if both dictionaries refer to the same
object in memory
are_same_object = dict1 is dict2

# Print the result


print("Do dict1 and dict2 refer to the same object in memory?",
are_same_object)

Function Return Values:


Define a function create_list() that returns a list object. Call this function twice
and use the identity operator is to check if the returned lists are the same objects in
memory. Print the result.

# Define the function that returns a list object


def create_list():
return [1, 2, 3]

# Call the function twice


list1 = create_list()
list2 = create_list()

# Use the identity operator `is` to check if the returned lists are the same objects in
memory
are_same_object = list1 is list2

# Print the result


print("Do list1 and list2 refer to the same object in memory?", are_same_object)

Comparison with Different Types:

● Create two variables x and y and assign an integer value to x and a string
value to y. Use the identity operator is to check if x and y refer to the same
object in memory. Print the result.

# Assign an integer value to x and a string value to y


x = 42
y = "hello"
# Use the identity operator `is` to check if x and y refer to the same object in
memory
are_same_object = x is y

# Print the result


print("Do x and y refer to the same object in memory?", are_same_object)

List Membership:

● Write a Python program that prompts the user to enter a number. Check if the
number is present in a predefined list of numbers using the in operator. Print
a message indicating whether the number is found in the list or not.# List
Membership

numbers = [1, 2, 3, 4, 5]
num = int(input("Enter a number: "))
if num in numbers:
print("Number is found in the list.")
else:
print("Number is not found in the list.")

String Membership:

● Create a Python program that asks the user to input a character. Check if the
character is present in a given string using the in operator. Print a message
indicating whether the character is found in the string or not.# String
Membership

my_string = "Hello, world!"


char = input("Enter a character: ")
if char in my_string:
print("Character is found in the string.")
else:
print("Character is not found in the string.")

Tuple Membership:

● Define a tuple containing the names of some fruits. Prompt the user to enter
the name of a fruit. Check if the entered fruit name is present in the tuple
using the in operator. # Tuple Membership

fruits = ('apple', 'banana', 'orange', 'grape')


fruit = input("Enter a fruit name: ")
if fruit in fruits:
print("Fruit is found in the tuple.")
else:
print("Fruit is not found in the tuple.")Print a message indicating whether the fruit is
found in the tuple or not.

Dictionary Membership:

Write a Python program that defines a dictionary containing some words and their
corresponding meanings. Prompt the user to input a word. Check if the entered word
is present in the dictionary keys using the in operator. Print a message indicating
whether the word is found in the dictionary or not.
# Dictionary Membership
words = {'apple': 'a fruit', 'book': 'a collection of pages'}
word = input("Enter a word: ")
if word in words:
print("Word is found in the dictionary.")
else:
print("Word is not found in the dictionary.")

List Comprehension with Membership:

● Define a list of numbers. Use a list comprehension to create a new list


containing only the even numbers from the original list. Print the result.

# List Comprehension with Membership


original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [num for num in original_list if num % 2 == 0]
print("Even numbers:", even_numbers)

String Concatenation with Membership:

Create two strings containing words or phrases. Use the in operator to check if the
second string is a substring of the first string. If it is, concatenate the two strings and
print the result; otherwise, print a message indicating that no concatenation is
performed.
# String Concatenation with Membership
string1 = "Hello, how are you?"
string2 = "how are"
if string2 in string1:
print(string1 + string2)
else:
print("No concatenation performed.")

Nested List Membership:

● Define a nested list containing several lists of integers. Prompt the user to
enter an integer. Use a nested loop and the in operator to check if the entered
integer is present in any of the inner lists. Print a message indicating whether
the integer is found or not.

# Nested List Membership


nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
num = int(input("Enter an integer: "))
found = False
for sublist in nested_list:
if num in sublist:
found = True
break
if found:
print("Integer is found in the nested list.")
else:
print("Integer is not found in the nested list.")

Set Membership:

● Define a set containing some unique values. Prompt the user to input a value.
Check if the entered value is present in the set using the in operator. Print a
message indicating whether the value is found in the set or not.# Set
Membership

my_set = {1, 2, 3, 4, 5}
value = int(input("Enter a value: "))
if value in my_set:
print("Value is found in the set.")
else:
print("Value is not found in the set.")

List of Strings Membership:

Create a list containing several strings. Prompt the user to input a string. Use the in
operator to check if the entered string is present in the list. Print a message
indicating whether the string is found in the list or not
# List of Strings Membership
strings_list = ["apple", "banana", "orange", "grape"]
user_input = input("Enter a string: ")
if user_input in strings_list:
print("String is found in the list.")
else:
print("String is not found in the list.")

String Slicing with Membership:

● Define a string containing a sentence. Prompt the user to input a word. Use
the in operator to check if the entered word is present in the sentence
(ignoring case). If it is, slice the sentence to include only the portion before
the word and print the result; otherwise, print a message indicating that the
word is not found.

# String Slicing with Membership


sentence = "Python is a powerful programming language"
word = input("Enter a word: ")
if word.lower() in sentence.lower():
index = sentence.lower().index(word.lower())
print("Substring found:", sentence[:index])
else:
print("Word is not found in the sentence.")

Password Strength Checker:

● Create a Python function that takes a password as input and checks if it


meets the criteria for a strong password. Criteria: minimum length of 8
characters, contains at least one uppercase letter, one lowercase letter, and
one digit. Return True if the password is strong, otherwise return False.

# Password Strength Checker


def is_strong_password(password):
return len(password) >= 8 and any(c.isupper() for c in password) and
any(c.islower() for c in password) and any(c.isdigit() for c in password)

Time Comparison:

● Write a Python program that prompts the user to input two times in the format
"HH:MM" (24-hour format). Compare the two times and print "Time 1 is
earlier" if the first time is earlier than the second, "Time 2 is earlier" if the
second time is earlier, and "Both times are the same" if they are equal.

# Time Comparison
time1 = input("Enter time 1 (HH:MM): ")
time2 = input("Enter time 2 (HH:MM): ")
if time1 < time2:
print("Time 1 is earlier")
elif time1 > time2:
print("Time 2 is earlier")
else:
print("Both times are the same")

Guessing Game:
Write a Python program that generates a random number between 1 and 100.
Prompt the user to guess the number. Compare the user's guess with the generated
number and print "Too high" if the guess is greater, "Too low" if it's smaller, and
"Congratulations! You guessed it!" if they guess correctly.
# Guessing Game
import random
random_number = random.randint(1, 100)
guess = int(input("Guess the number (between 1 and 100): "))
if guess < random_number:
print("Too low")
elif guess > random_number:
print("Too high")
else:
print("Congratulations! You guessed it!")

# Toggle Bit at Specific Position


num = int(input("Enter an integer: "))
position = int(input("Enter the position of the bit to toggle: "))
result = num ^ (1 << position)
print("Result after toggling bit at position {}: {}".format(position, result))

Toggle Bit at Specific Position:

● Develop a Python program that toggles (flips) the bit at a specific position in
an integer provided by the user. Prompt the user to input an integer and the
position of the bit to toggle. Print the result after toggling the bit.

def toggle_bit(num, position):


mask = 1 << position
return num ^ mask

num = int(input("Enter an integer: "))


position = int(input("Enter the position of the bit to toggle: "))

result = toggle_bit(num, position)


print("Result after toggling bit at position {}: {}".format(position, result))

You might also like