0% found this document useful (0 votes)
7 views6 pages

Python Code

Uploaded by

Taha Khan
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)
7 views6 pages

Python Code

Uploaded by

Taha Khan
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/ 6

Q.

1 :
# Get user input

quantity = int(input("Enter the quantity of items: "))

price_per_item = float(input("Enter the price per item: "))

# Calculate total cost

total_cost = quantity * price_per_item

# Apply 10% discount if quantity > 1000


if quantity > 1000:

discount = 0.10 * total_cost

total_expense = total_cost - discount

else:
total_expense = total_cost

# Display the result

print(f"Total expense: ₹{total_expense:.2f}")

OUTPUT :

Enter the quantity of items: 1200

Enter the price per item: 15

Total expense: ₹16200.00

Q.2 :

# Print numbers from 1 to 10

print("Numbers from 1 to 10:")

for i in range(1, 11):

print(i, end=' ')


print() # for newline

# Print alphabets from A to Z

print("Alphabets from A to Z:")

for ch in range(ord('A'), ord('Z') + 1):

print(chr(ch), end=' ')

print()

OUTPUT :
Numbers from 1 to 10:
1 2 3 4 5 6 7 8 9 10

Alphabets from A to Z:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Q.3 :

# Get input from user


n = int(input("Enter a positive integer (n): "))

# Initialize variables

sum_natural = 0
i=1

# Use while loop to calculate sum

while i <= n:

sum_natural += i

i += 1

# Display the result

print(f"Sum of first {n} natural numbers is: {sum_natural}")

OUTPUT :
Enter a positive integer (n): 5

Sum of first 5 natural numbers is: 15

Q.4 :

# Get number from user


num = int(input("Enter a number to print its table: "))

# Initialize counter

i=1

# Simulate do-while loop

while True:

print(f"{num} x {i} = {num * i}")

i += 1
if i > 10:
break

OUTPUT:

5x1=5

5 x 2 = 10

5 x 3 = 15

5 x 10 = 50

Q.5:

# Function to format full name

def format_name(first, last):


return f"Hello, {first.capitalize()} {last.capitalize()}!"

# Infinite loop to take user input

while True:

print("\nType 'exit' anytime to stop.")

first_name = input("Enter your first name: ")

if first_name.lower() == 'exit':
break

last_name = input("Enter your last name: ")

if last_name.lower() == 'exit':

break

# Call the function and print result

formatted = format_name(first_name, last_name)

print(formatted)

OUTPUT:

Enter your first name: Zaid


Enter your last name: Khan
Hello, Zaid Khan!

Enter your first name: exit

Q.7 :

# Ask the user to enter their age


age = int(input("Enter your age: "))

# Determine membership cost

if age < 12:


cost = 0

elif 12 <= age <= 18:

cost = 2500

elif 18 < age <= 60:

cost = 4000

else:

cost = 1500

# Print membership cost

if cost == 0:

print("Your membership is FREE!")

else:

print(f"Your membership cost is ₹{cost}")


OUTPUT :

Enter your age: 10

Your membership is FREE!

Q.7 :

# Step 1: Create the first dictionary with basic user info

user_info = {
"Name": "Zaid",
"Email-ID": "[email protected]",

"Phone": "9876543210",

"Address": "Mumbai, India"

# Step 2: Create second dictionary with additional info


additional_info = {

"Height": "5'8\"",

"Weight": "68 kg",

"Blood Group": "O+"


}

# Step 3: Print elements of first dictionary separately

print("User Info:")

for key, value in user_info.items():

print(f"{key}: {value}")

# Step 4: Update first dictionary with second dictionary's data


user_info.update(additional_info)

# Step 5: Remove last element from both dictionaries

user_info.popitem()

additional_info.popitem()

# Step 6: Print both dictionaries

print("\nUpdated User Info (after removing last element):")

print(user_info)

print("\nUpdated Additional Info (after removing last”)

OUTPUT:

User Info:
Name: Zaid
Email-ID: [email protected]

Phone: 9876543210

Address: Mumbai, India

Updated User Info (after removing last element):

{'Name': 'Zaid', 'Email-ID': '[email protected]', 'Phone': '9876543210', 'Address': 'Mumbai, India',


'Height': '5\'8"', 'Weight': '68 kg'}

Updated Additional Info (after removing last element):

{'Height': '5\'8"', 'Weight': '68 kg'}

You might also like