Assignment1_solution
Assignment1_solution
# Question 10
# The type() function is mostly used for
debugging purposes. Two different types of
arguments can be
# passed to type() function, single and
three arguments. If a single argument
type(obj) is passed, it returns
# the type of the given object. If three
argument types (object, bases, dict) are
passed, it returns a new
# type object.
# Determine the type of the variable x
where x = 5.5.
# x=5.5
# print(type(x))
# float
# Question 11
# Find the type of y where y = [1, 2, 3].
y=[1,2,3]
print(type(y))
# list
# Question 12
# Determine the type of m where m = 3 + 4j.
m=3+4j
print(type(m))
# complex
# Question 13
# What is the type of a variable data =
{'key': 'value'}.
data={'key':'value'}
print(type(data))
# dict
# Question 14
# Determine the type of name where name =
"John".
name="john"
print(type(name))
# str
# Question 15
# Check the type of a variable b where b =
True.
b=True
print(type(b))
# bool
# Question 16
# Determine the type of the variable status
where status = None.
status= None
print(type(status))
# NoneType
# format():
# Question 17
# The format() method is a powerful tool
that allows developers to create formatted
strings by
# embedding variables and values into
placeholders within a template string. This
method offers a flexible
# and versatile way to construct textual
output for a wide range of applications.
Python string format()
# function has been introduced for handling
complex string formatting more efficiently.
Sometimes we
# want to make generalized print statements
in that case instead of writing print
statements every time we
# use the concept of formatting.
# Format the string "Hello, {}" to include
the name "Alice".
# Question 18
# Format the number 3.14159 to display only
two decimal places.
x="{}"
print(x.format(3.14))
# 3.14
# Question 19
# Format the string "Welcome to {}" with
the name of a city "New York".
a="Welcome to {}"
print(a.format("New York"))
# Welcome to New York
# Question 20
# Format the string "You have {} new
messages" with the number 7.
a="You have {} new messages"
print(a.format(7))
# You have 7 new messages
# Question 21
# Format the string "The price is {}
dollars" to include price = 50.
x="The price is {} dollars"
print(x.format(50))
# The price is 50 dollars
# Question 22
# Format the string "Coordinates: ({}, {})"
to include x = 10 and y = 20.
a="Coordinates: ({}, {})"
print(a.format(10, 20))
# Question 23
# Format the string “Warm welcome,{}” to
include the name “Ben”
welc="Warm welcome,{}"
print(welc.format("Ben"))
# Warm welcome,Ben
# Question 24
# Format the number 8.7654 to display only
two decimal places.
x=8.7654
print(format(x,".2f"))
print(f"{x:.2f}")
# 8.77
# Question 25
# Format the string “Greetings, {}!” to
include the name “Sarah”.
m="Greetings, {}!"
print(m.format("Sarah"))
# Greetings, Sarah!
# Question 26
# Format the number 9.8765 to display only
one decimal place.
n=9.8765
print(format(n,".1f"))
print(f"{n:.1f}")
# 9.9
# Id():
# Question 29
# In Python, id() function is a built-in
function that returns the unique identifier
of an object. The identifier
# is an integer, which represents the
memory address of the object. The id()
function is commonly used
# to check if two variables or objects
refer to the same memory location.
# Find the id of the variable z = 100.
z=100
print(id(z))
# 4330778712
# Question 30
# Check if the ids of two variables, a = 50
and b = 50, are the same or not.
a=50
b=50
print(id(a))
print(id(b))
print(id(a)==id(b))
# 4322421272
# 4322421272
# True
# Question 31
# Get the id of an integer num = 5.
num=5
print(id(num))
# 4334642296
# Question 32
# Check if the ids of two lists a = [1, 2,
3] and b = [1, 2, 3] are the same.
a=[1,2,3]
b=[1,2,3]
print(id(a))
print(id(b))
print(id(a)==id(b))
# 4297298048
# 4297601920
# False
# Question 33
# Find the id of the string s = "hello".
s="hello"
print(id(s))
# 4373131216
# Question 34
# Check if the ids of two variables, a =
256 and b = 256, are same or not.
a=256
b=256
print(id(a))
print(id(b))
print(id(a)==id(b))
# 4398089176
# 4398089176
# True
# Question 35
# Find the id of the variable z = “foo”.
z="foo"
print(id(z))
# 4377244272
# Question 36
# Find the id of the variable a = "bar".
a="bar"
print(id(a))