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

Python Lab

The document contains a series of programming exercises for B.Tech students, focusing on basic Python programming concepts such as arithmetic operations, area calculations, and data input/output. It includes examples of using print() and input() functions, along with various formatting options. Additionally, it provides hints for unit conversions and mathematical computations.

Uploaded by

jinayshah780
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)
2 views6 pages

Python Lab

The document contains a series of programming exercises for B.Tech students, focusing on basic Python programming concepts such as arithmetic operations, area calculations, and data input/output. It includes examples of using print() and input() functions, along with various formatting options. Additionally, it provides hints for unit conversions and mathematical computations.

Uploaded by

jinayshah780
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

PP LAB BATCH B DIV 3

[Link](I), DIV III

1. Write a program that performs as calculator (addition, multiplication, division,


subtraction).
2. Write a program to find area of triangle (a=h*b*.5) where a = area, h = height, b = base.
3. Write a program to calculate simple interest (i = (p*r*n)/100) where i = Simple interest, p
=
Principal amount, r = Rate of interest, n = Number of years.
4. Write a program to interchange two numbers.
5. Write a program to enter a distance into kilometer and convert it in to meter, feet, inches
and centimeter.
Hint: 1 Kilometer = 3280.8399 Feet, 1 kilometer = in x 0.0000254, centimeters =
kilometers × 100000
6. Write a program to compute Fahrenheit from centigrade (f=1.8*c +32)

Python input() and print() Exercises (20 Examples)

Example 1: Print a Welcome Message


print("Welcome to Python Programming")

Example 2: Print Multiple Values


name = "Alice"
age = 20

print("Name:", name)
print("Age:", age)

Example 3: Use sep


print("Python", "Java", "C++", sep=" | ")

Output
Python | Java | C++

Example 4: Use end


print("Hello", end=" ")
print("Students")

Output

Hello Students

Example 5: Print Escape Characters


print("Line1\nLine2")
print("Name\tMarks")

Example 6: Read a Name


name = input("Enter your name: ")
print("Welcome", name)

Example 7: Read an Integer


age = int(input("Enter your age: "))
print("Age =", age)

Example 8: Read a Float


salary = float(input("Enter your salary: "))
print("Salary =", salary)

Example 9: Read Two Numbers and Add Them


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum =", a + b)

Example 10: Read Three Numbers


a, b, c = map(int, input("Enter three numbers: ").split())

print("Numbers are:", a, b, c)
print("Total =", a + b + c)

Example 11: Calculate Average


m1 = float(input("Enter first mark: "))
m2 = float(input("Enter second mark: "))
m3 = float(input("Enter third mark: "))

average = (m1 + m2 + m3) / 3

print("Average =", average)

Example 12: Use f-Strings


name = input("Enter your name: ")
city = input("Enter your city: ")

print(f"{name} lives in {city}.")

Example 13: Use format() Method


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

print("Student Name: {} Age: {}".format(name, age))

Example 14: Print Decimal Values


price = float(input("Enter price: "))
print(f"Price = {price:.2f}")

Example 15: Print Percentage


marks = float(input("Enter marks: "))

print(f"Percentage = {marks:.1f}%")

Example 16: Print a Formatted Table


print("{:<15}{:<10}".format("Name", "Marks"))
print("-"*25)
print("{:<15}{:<10}".format("Amit", 85))
print("{:<15}{:<10}".format("Riya", 92))

Example 17: Rectangle Area


length = float(input("Enter length: "))
width = float(input("Enter width: "))

area = length * width

print("Area =", area)

Example 18: Circle Area


radius = float(input("Enter radius: "))

area = 3.14 * radius * radius

print(f"Area of Circle = {area:.2f}")

Example 19: Student Information


name = input("Enter student name: ")
roll = input("Enter roll number: ")
branch = input("Enter branch: ")

print("\nStudent Details")
print("-"*30)
print("Name :", name)
print("Roll :", roll)
print("Branch :", branch)

Example 20: Complete Formatted Program


name = input("Enter your name: ")
age = int(input("Enter your age: "))
city = input("Enter your city: ")
percentage = float(input("Enter percentage: "))

print("\n" + "="*40)
print(" STUDENT PROFILE")
print("="*40)
print(f"Name : {name}")
print(f"Age : {age}")
print(f"City : {city}")
print(f"Percentage : {percentage:.2f}%")
print("="*40)

Formatting Options Used

Formatting Option Example

print() print("Hello")

input() name = input()

int() int(input())

float() float(input())

sep print(a, b, sep=",")

end print(a, end=" ")

\n New line
\t Tab space

+ String concatenation

* Repeat characters ("-"*30)

f-string f"{name}"

.format() "{}".format(x)

:.2f Two decimal places

< Left alignment

> Right alignment

^ Center alignment

split() input().split()

map() map(int, input().split())

You might also like