0% found this document useful (0 votes)
9 views7 pages

Python Programming Basics and Exercises

The document contains various Python programming exercises covering basic data types, variables, type conversion, user input, real-world scenarios, and challenges. Each section includes specific tasks such as creating variables, lists, tuples, dictionaries, and performing calculations like temperature conversion and area of a circle. Additionally, it includes user interaction examples and checks for even/odd numbers, palindromes, and factorial calculations.

Uploaded by

lordjdbh1
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)
9 views7 pages

Python Programming Basics and Exercises

The document contains various Python programming exercises covering basic data types, variables, type conversion, user input, real-world scenarios, and challenges. Each section includes specific tasks such as creating variables, lists, tuples, dictionaries, and performing calculations like temperature conversion and area of a circle. Additionally, it includes user interaction examples and checks for even/odd numbers, palindromes, and factorial calculations.

Uploaded by

lordjdbh1
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

**Name:Trishit Santra, roll:MTUG/161/24**

Name:Trishit Santra, roll:MTUG/161/24

[Link] Datatypes

[Link] a python program

Assign the integer 25 to a variable a and the float 3.14 to a variable [Link] their sum.

a = 25
b = 3.14
Sum = a + b
print("Sum of a and b:",Sum)

Sum of a and b: 28.14

Assign the string "Python Programming" to a variable and print it.

str="Python Programming"
print(str)

Python Programming

2. Create a list containing the names of your three favourite fruits. Print the second fruit in the
list.

fruits = ["apple", "banana", "mango"]⁹


print("Second fruit:", fruits[1])

Second fruit: banana

3. Create a tuple containing three numbers 5, 10, and 15. Print the sum of all the numbers in
the tuple.

numbers = (5, 10, 15)


sum= sum(numbers)
print("Sum of numbers:",sum)

Sum of numbers: 30

4. Define a dictionary with the following key-value pairs:

'name': 'Alice'

'age': 30

'city': 'Kolkata'
Print the value of the 'city' key.

d = {'name': 'Alice','age': 30,'city': 'Kolkata'}


print(d['city'])

Kolkata

[Link]

Write a Python program to:

○Assign the value 50 to a variable x.


○Assign the value 100 to a variable y.
○Swap the values of x and y without using a third variable.

x = 50
y = 100
x = x + y
y = x - y
x = x - y
print("After swapping:")
print("x =", x)
print("y =", y)

After swapping:
x = 100
y = 50

6. Assign the following values to variables a, b, and c:

○a=10,b=20,c=30
○Calculate and print:
Average = a+b+c/3

a = 10
b = 20
c = 30
average = (a+b+c)/3
print("Average:", average)

Average: 20.0

[Link] Conversion

7. Write a Python program to:

Take a string input from the user representing an integer (e.g., "25") and convert it into an
integer.
Add 10 to the converted value and print the result.

input_string = input("Enter an integer: ")

integer_value = int(input_string)

result = integer_value + 10

print("The result is:", result)

Enter an integer: 4
The result is: 14

8. Convert the following values to their respective types and print:

5.5 to an integer.

"123" to a float.

[1, 2, 3] to a tuple.

int_value = int(5.5)
print("5.5 as an integer:", int_value)

float_value = float("123")
print('"123" as a float:', float_value)

5.5 as an integer: 5
"123" as a float: 123.0

Numbers=[1,2,3]
c=tuple(Numbers)
print("Tuple:",c)

Next steps: Explain error

[Link] Input

9. Write a program to:


Ask the user to input their name and age.

Print a message like: "Hello [name], you are [age] years old."

name = input("Enter your name: ")


age = input("Enter your age: ")
print(f"Hello {name}, you are {age} years old.")

Enter your name: Dj


Enter your age: 45
Hello Dj, you are 45 years old.

10. Write a program to:

Ask the user to enter two numbers.

Calculate and print their sum, difference, product, and division.

num1 = float(input("Enter the first number: "))


num2 = float(input("Enter the second number: "))
sum_result = num1 + num2
difference_result = num1 - num2
product_result = num1 * num2
division_result = num1 / num2
print(f"Sum: {sum_result}")
print(f"Difference: {difference_result}")
print(f"Product: {product_result}")
print(f"Division: {division_result}")

Enter the first number: 1


Enter the second number: 2
Sum: 3.0
Difference: -1.0
Product: 2.0
Division: 0.5

[Link] World Scenarios

11. Temperature Conversion

Write a Python program to:

○Take a temperature in Celsius from the user.


○Convert it to Fahrenheit using the formula:
○Print the result.
Fahrenheit (Celsius×9/5)+32

celsius = float(input("Enter temperature in Celsius: "))


fahrenheit = (celsius * 9/5) + 32
print(f"{celsius} degrees Celsius is equal to {fahrenheit} degrees Fahrenheit.")
Enter temperature in Celsius: 34
34.0 degrees Celsius is equal to 93.2 degrees Fahrenheit.

12. Area of a Circle

Write a program to:

Take the radius of a circle as input from the user.

Calculate the area using the formula:

Area=(pi) x radius² (use [Link]).

Print the area.

import math
radius = float(input("Enter the radius of the circle: "))
area = [Link] * radius**2
print(f"The area of the circle is {area:.2f}")

Enter the radius of the circle: 4


The area of the circle is 50.27

[Link] concepts

Shopping Cart

Write a program to:

○Create a dictionary representing a shopping cart with items and their prices: Example: apple':
30, 'banana': 10, 'milk': 25).

○Ask the user for the quantity of each item.


Calculate and print the total bill.

d={'apple':30, 'banana': 10, 'milk':25}

a=float(input("Enter the number of apples:"))

b=float(input("Enter the number of bananas:"))

m=float(input("Enter the number of packet of milk:"))

Shopping_Bill=(a*d['apple'])+(b*d['banana']) +(m*d['milk'])

print("Total bill is", Shopping_Bill)

Enter the number of apples:4


Enter the number of bananas:5
Enter the number of packet of milk:6
Total bill is 320.0
[Link]

Find Even or Odd

Write a Python program to:

Ask the user to input a number.

Check if the number is even or odd and print the result.

number = int(input("Enter a number: "))


if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")

Enter a number: 150


150 is even.

Palindrome Check

Write a program to:

Take a string as input from the user.

Check if the string is a palindrome (reads the same forward and backward) and print the result.

string = input("Enter a string: ")


if is_palindrome(string):
print(f"{string} is a palindrome.")
else:
print(f"{string} is not a palindrome.")

Enter a string: 33
33 is a palindrome.

Factorial Calculation

Write a program to:

Take an integer input from the user.

Calculate the factorial of the number using a loop.

Print the result.

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


result = factorial(number)
print(f"The factorial of {number} is {result}")

Enter an integer: 69
The factorial of 69 is 1711224524281413113724683388812728390922705448935203693

You might also like