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

Python Programming Basics and Examples

The document contains a series of practical exercises focused on Python programming, covering topics such as variables, data types, and basic arithmetic operations. Each practical includes a question, programming code, and example outputs demonstrating the results. Key concepts include defining variables, calculating areas, and determining whether numbers are positive, negative, or even/odd.

Uploaded by

sumanraisr669757
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)
6 views16 pages

Python Programming Basics and Examples

The document contains a series of practical exercises focused on Python programming, covering topics such as variables, data types, and basic arithmetic operations. Each practical includes a question, programming code, and example outputs demonstrating the results. Key concepts include defining variables, calculating areas, and determining whether numbers are positive, negative, or even/odd.

Uploaded by

sumanraisr669757
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

(PRACTICAL 1)

QUESTION: What is python? Write some important features of .


. python?
= Python is a powerful and immensely popular high level programming
language. Python is being extensively used to create web software,
internet based applications, games and mobile apps. Some important
features of python are
1. Easy to learn and use: Being easy to use, it is developer friendly
high level programming language.
2. Easy to read: the python language code closely resembles the
English language .
3. Interpreted language: Python is an interpreter based language, the
python interpreter executes the code one line at a time.
4. Cross platform language: The python code can run equally well
on different platforms such as windows, Linux, Unix, and
Macintosh.
5. Object oriented language: Python supports object oriented
programming. It supports the of classes and objects.

Page 1 of 16
(PRACTICAL 2)
QUESTION: Define variables and data types in python. .
. How do you gain a value to a variable?

=In a computer program a variable is a quantity, whose value


can change as many times as required during the execution of a
program .In other words we can say that a variable is a quantity
that can store varying data values during the execution of a
program. The type of data a variable can store is called its data
type . For examples when a variable is declared of an integer
type as in ‘int x’, it can store only integer values in a program.
Variable names are case sensitive and can include
alphanumerical letters as well as the underscore character.
Variable names can not contain reserved terms. The equal to
sign “=” , followed by the variables value , is used to assign
variables in python.

Page 2 of 16
(PRACTICAL 3)
QUESTION: Write a program to input two number and
display their sum.
PROGRAMING
>>> num1 = float(input("Enter the first number: "))
>>>num2 = float(input("Enter the second number: "))
>>> sum_of_numbers = num1 + num2
>>> print(f"The sum of {num1} and {num2} is:
{sum_of_numbers}")
The sum of 20 and 100 is:120

OUTPUT
Enter the first number: 20
Enter the second number: 100
The sum of 20 and 100 is: 120

Page 3 of 16
(PRACTICAL 4)
QUESTION: Write a program to interchange the value of two .
. variables.
PROGRAMMING
>>> x = input('Enter value of x:')
>>> y = input('Enter value of y:')
>>> temp = x
>>> x = y
>>> y = temp
>>> print('The value of x after swapping: {}'.format(x))
The value of x after swapping: 9
>>> print('The value of y after swapping: {}' .format(y))

OUTPUT
Enter value of x: 9
Enter value of y:3
The value of x after swapping: 3
The value of y after swapping: 9

Page 4 of 16
(PRACTICAL 5)

QUESTION: Write a program to calculate the volume of a .


. cylinder.

PROGRAMING
>>> Pi = 22/7
>>> r = input('Radius of cylinder:')
>>> h = input('Height of cylinder:')
>>> volume = Pi*r*r*h
>>> print ("volume is: ", volume)

OUTPUT

radius = 50
height = 100
volume is: 78500.0

Page 5 of 16
(PRACTICAL 6)

QUESTION: Write a program to calculate simple interest.

PROGRAMING
>>> principal = input("Enter the principal amount: ")
>>> rate = input("Enter the interest rate: ")
>>> time = input("Enter the time period: ")
>>> interest = (principal*rate*time)/100
>>> print("Simple Interest: " , interest)

OUTPUT
principal = 80000
rate = 12
time = 6
Simple Interest: 57600.0

Page 6 of 16
(PRACTICAL 7)

QUESTION: Find the area of a rectangle whose length and


breadth are 20cm and 25cm .

PROGRAMING
L = float(input(‘Enter Length of the Rectangle: ‘))
. B = float(input(‘Enter Breadth of the Rectangle: ‘))
Area = l * b
Print(f”Area of rectangle is {area:.2f}”)

OUTPUT
length = 20
breadth = 25
Area of rectangle: 500

Page 7 of 16
(PRACTICAL 8)

QUESTION: Write a program to find area of a circle.

PROGRAMING
>>> radius = input("Enter value of radius: ")
>>>Pi = input("Enter value of Pi: ")
>>> area = Pi*radius*radius
>>> print("Area of circle: " , area)

OUTPUT
Enter value of radius:14
Enter value of Pi: 22/7
Area of circle: 616.0

Page 8 of 16
PRACTICAL 9

QUESTION: Write a program to compare two numbers


and . print the bigger one.

PROGRAMING
num1= float(input(“Enter the first number:”))
num2= float(input(“Enter the second number:”))

Compare and print the bigger number


bigger = max(num1,num2)
print(“The bigger number is:”,bigger)

OUTPUT

Enter the first number: 4

Page 9 of 16
Enter the second number:6
The bigger number is:6.0

(PRACTICAL 10)

QUESTION: Write a program to display one of the two . .


. alternate message.
PROGRAMING
>>>print("Choose a message to display:")
>>> print("1. Hello, world!")
>>> print("2. Welcome to Python programming!")
>>> choice = input("Enter 1 or 2: ")
>>>if choice == "1": print("Hello, world!")
elif
choice == "2": print("Welcome to Python programming!")
else:
print("Invalid choice. Please enter 1 or 2.")

OUTPUT
Choose a message to display
Enter 1 or 2: 1
Hello, world!
Page 10 of 16
(PRACTICAL 11)

QUESTION: Write a program to find whether a given


number is even or odd.

PROGRAMING
>>> num = int(input("Enter a number: "))
>>>if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))

OUTPUT
Enter a number: 99
99 is Odd

Page 11 of 16
(PRACTICAL 12)

QUESTION: Write a program to find given number is


positive or negative.
PROGRAMING
>>>num = float(input("Enter a number: "))
>>>if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
>>>print("Negative number")

OUTPUT
Enter a number: -1

Page 12 of 16
Negative number

(PRACTICAL 13)

QUESTION: Write a program to compare three numbers and print


the largest of them.

PROGRAMING
>>>num1 = float(input("Enter first number: "))
>>>num2 = float(input("Enter second number: "))
>>>num3 = float(input("Enter third number: "))

>>>if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

Page 13 of 16
>>>print("The largest number is", largest)

The largest number is 14

OUTPUT
num1 = 8
num2 = 26
num3 = 7
The largest number is 26

(PRACTICAL 14)
QUESTION: Accept the value from user and calculate the
perimeter and area of rectangle.

PROGRAMING
>>> length = float(input(“Enter length of the rectangle”))
>>> breadth = float(input(“Enter breadth of the rectangle”))
>>> area = length*breadth
>>> perimeter = 2*(length+breadth)
>>> print(“Area of rectangle = “, area)
>>> print(“Perimeter of rectangle = “ , perimeter)

OUTPUT
Enter length of the rectangle: 10
Enter breadth of the rectangle = 5
Area of rectangle = 50.0

Page 14 of 16
Perimeter of rectangle = 30.0

(PRACTICAL 15)

QUESTION: Write a program to check given number is positive,


zero or negative.

PROGRAMING
>>>num = float(input("Enter a number: "))
>>>if num > 0:
print("Positive number")
>>>elif num == 0:
print("Zero")
>>>else:
print("Negative number")

Page 15 of 16
OUTPUT

Enter a number: 20
Positive number

Page 16 of 16

You might also like