Doubts Solution
Doubts Solution
Doubts Solution
● 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.
list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 3, 4, 5]
are_same_object = list1 is list2
● 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.
# Use the identity operator `is` to check if both variables refer to the same object in
memory
are_same_object = x is y
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.
● 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.
# Use the identity operator `is` to check if both lists refer to the same object in
memory
are_same_object = list1 is list2
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.
● 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
# Use the identity operator `is` to check if list1 and list2 refer to the same
object in memory
are_same_object = list1 is list2
● 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.
# Use the identity operator `is` to check if both variables refer to the same
object in memory
are_same_object = a is b
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.
# Use the identity operator `is` to check if both dictionaries refer to the same
object in memory
are_same_object = dict1 is dict2
# Use the identity operator `is` to check if the returned lists are the same objects in
memory
are_same_object = list1 is list2
● 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.
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
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
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.")
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.")
● 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.
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.")
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.")
● 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.
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!")
● 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.