0% found this document useful (0 votes)
4 views16 pages

Assignment1_solution

Uploaded by

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

Assignment1_solution

Uploaded by

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

# Question 1

# Write a Python code that prints the


sentence: "Python is fun!".
print("Python is fun!")
# Python is fun!
# Question 2
# Print the message "Learning Python is
easy!".
print("Learning Python is easy!")
# Learning Python is easy!
# Question 3
# Print a variable c = 100 using the print
function.
c=100
print(c)
# 100
# Question 4
# Print the values of two variables, a = 10
and b = 20, in the same line.
a=10
b=20
print(a,b)
# 10 20
# Question 5
# Print the sum of x = 15 and y = 25.
x=15
y=25
sum=x+y
print(sum)
# 40
# Question 6
# Print the following list [1, 2, 3, 4, 5]
as a string.
l="[1,2,3,4,5]"
print(l)
print(type(l))
# [1,2,3,4,5]
# class 'str'
# Question 7
# Write a Python code that prints the
sentence: Data science is exciting!.
print("Data science is exciting!")
# Data science is exciting!
# Question 8
# Print the values of two variables, x =
15.75 and y = -90.00, on the same line.
x=15.75
y=-90.00
print(x,",",y)
# 15.75 , -90.0
# Question 9
# Joey has been tasked with solving some
math problems to unlock the door to the
hidden library. He
# needs to:
# ● Add 10 and 15
# ● Multiply 7 and 8
# ● Divide 56 by 7
# ● Subtract 300 from 450
# Task:
# Help Joey to write a Python program that
performs these operations and prints the
results.
print("Add:",10+15)
print("Multiply:",7*8)
print("Divide:",56/7)
print("Subtract:",450-300)
# Add: 25
# Multiply: 56
# Divide: 8.0
# Subtract: 150

# 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

# Question 27: Personalized Invitation


Generator
# Scenario: You are creating a personalized
invitation generator for an event. Write a
Python program
# that takes the guest's name, the event
name, and the date as input. The program
should then generate a
# personalized invitation message using the
format() function, such as "Dear [name],
you are invited to
# [event] on [date]."

# a=input("Enter Your Name:")


# b=input("Enter Event Name:")
# c=input("Enter Date:")
# x="Dear {}, you are invited to {} on {}"
# print(x.format(a,b,c))

# Question 28: Travel Expense Calculator


# Scenario: You are planning a trip and
want to calculate your travel expenses.
Write a Python program
# that takes the distance of the trip in
kilometers and the cost per kilometer as
input. The program
# should then calculate and print the total
travel expense using the input() and
format() functions.

# a=int(input("Enter distance of the trip:


"))
# b=int(input("Enter cost per Km: "))
# x= "the total travel expense is {}"
# print(x.format(a*b),"Rs")

# Enter distance of the trip: 15


# Enter cost per Km: 15
# the total travel expense is 225 Rs

# 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))

You might also like