0% found this document useful (0 votes)
5 views14 pages

Python Practical 1

The document contains a series of Python programming examples demonstrating various concepts such as variables, operators, conditional statements, loops, jump statements, functions, recursion, arrays, strings, modules, lists, tuples, dictionaries, and file handling. Each section includes coding examples along with corresponding outputs for user inputs. The examples are designed to illustrate fundamental programming techniques and data structures in Python.

Uploaded by

j90292151
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)
5 views14 pages

Python Practical 1

The document contains a series of Python programming examples demonstrating various concepts such as variables, operators, conditional statements, loops, jump statements, functions, recursion, arrays, strings, modules, lists, tuples, dictionaries, and file handling. Each section includes coding examples along with corresponding outputs for user inputs. The examples are designed to illustrate fundamental programming techniques and data structures in Python.

Uploaded by

j90292151
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

1.

PROGRAM USING VARIABLES, CONSTANTS, I/O


STATEMENTS IN PYTHON
CODING:

PI = 3.14159
radius = float(input("Enter the radius of the circle: "))
area = PI * radius * radius
print("The area of the circle is:", area)

OUTPUT:

Enter the radius of the circle: 5


The area of the circle is: 78.5397
[Link] USING OPERATORS IN PYTHON

CODING:

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


num2 = float(input("Enter the second number: "))
result = num1 + num2
print("Addition:", result)
result = num1 - num2
print("Subtraction:", result)
result = num1 * num2
print("Multiplication:", result)
result = num1 / num2
print("Division:", result)
result = num1 % num2
print("Modulo:", result)
result = num1 ** num2
print("Exponentiation:", result)

OUTPUT:
Enter the first number: 10
Enter the second number: 3
Addition: 13.0
Subtraction: 7.0
Multiplication: 30.0
Division: 3.3333333333333335
Modulo: 1.0
Exponentiation: 1000.0
[Link] USING CONDITIONAL STATEMENTS

CODING:

num = float(input("Enter a number: "))


if num> 0:
print("Positive number")
elif num< 0:
print("Negative number")
else:
print("Zero")

OUTPUT:

Enter a number: 5
Positive number
Enter a number: -2
Negative number
Enter a number: 0
Zero
4. PROGRAM USING LOOPS

CODING:

num = 1
whilenum<= 10:
print(num)
num += 1

OUTPUT:

1
2
3
4
5
6
7
8
9
10
[Link] USING JUMP STATEMENTS

CODING:

for num in range(1, 11):


if num % 3 == 0:
continue
print(num)
if num == 7:
break

OUTPUT:

1
2
4
5
6
7
6. PROGRAM USING FUNCTIONS
CODING:

def add_numbers(num1, num2):


sum = num1 + num2
return sum
n1 = float(input("Enter the first number: "))
n2 = float(input("Enter the second number: "))
result = add_numbers(n1, n2)
print("Sum:", result)

OUTPUT:

Enter the first number: 5


Enter the second number: 3
Sum: 8.0
[Link] USING RECURSION

CODING:

def recur_factorial(n):
if n==0:
return 1
else:
return n*recur_factorial(n-1)
n=7
if n<0:
print(''sorry ,there is no factorial for negative values'')
elif n==0:
print(''factorial of 0 is 1 '')
else:
print(''factorial of '',n,''is'',recur_factorial(n))

OUTPUT:

factorial of 7 is 5040
[Link] USING ARRAYS
CODING:

size = int(input("Enter the size of the array:"))


numbers = []
for i in range(size):
num = float(input("Enter a number: "))
[Link](num)
sum = 0
for num in numbers:
sum += num
average = sum / size
print("Array:", numbers)
print("Sum:", sum)
print("Average:", average)

OUTPUT:
Enter the size of the array:4
Enter a number: 5
Enter a number: 2
Enter a number: 8
Enter a number: 3
Array: [5.0, 2.0, 8.0, 3.0]
Sum: 18.0
Average: 4.5
9. PROGRAM USING STRINGS
CODING:

sentence = input("Enter a sentence: ")


length = len(sentence)
words = [Link]()
word_count = len(words)
word_to_replace = input("Enter the word to replace: ")
new_word = input("Enter the new word: ")
modified_sentence = [Link](word_to_replace, new_word)
print("Length of the sentence:", length)
print("Number of words:", word_count)
print("Modified sentence:", modified_sentence)

OUTPUT:
Enter a sentence: Hello, how are you?
Enter the word to replace: how
Enter the new word: is
Length of the sentence: 19
Number of words: 4
Modified sentence: Hello, is are you?
10. PROGRAM USING MODULES
CODING:

import math
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
sqrt = [Link](num1)
power = [Link](num1, num2)
print("Square root of", num1, "is", sqrt)
print(num1, "raised to the power of", num2, "is", power)

OUTPUT:

Enter the first number: 9


Enter the second number: 2
Square root of 9.0 is 3.0
9.0 raised to the power of 2.0 is 81.0
11. PROGRAM USING LIST
CODING:

size = int(input("Enter the size of the list: "))


numbers = []
for i in range(size):
num = float(input("Enter a number: "))
[Link](num)
sum = sum(numbers)
average = sum / size
maximum = max(numbers)
minimum = min(numbers)
print("List:", numbers)
print("Sum:", sum)
print("Average:", average)
print("Maximum:", maximum)
print("Minimum:", minimum)

OUTPUT:

Enter the size of the list: 5


Enter a number: 7
Enter a number: 4
Enter a number: 9
Enter a number: 2
Enter a number: 5
List: [7.0, 4.0, 9.0, 2.0, 5.0]
Sum: 27.0
Average: 5.4
Maximum: 9.0
Minimum: 2.0
12. PROGRAM USING TUPLES
CODING:

value1 = input("Enter the first value: ")


value2 = input("Enter the second value: ")
value3 = input("Enter the third value: ")
my_tuple = (value1, value2, value3)
length = len(my_tuple)
element1 = my_tuple[0]
sliced_tuple = my_tuple[1:3]
concatenated_tuple = my_tuple + ('extra',)
print("Length of the tuple:", length)
print("First element:", element1)
print("Sliced tuple:", sliced_tuple)
print("Concatenated tuple:", concatenated_tuple)

OUTPUT:

Enter the first value: Apple


Enter the second value: Banana
Enter the third value: Orange
Length of the tuple: 3
First element: Apple
Sliced tuple: ('Banana', 'Orange')
Concatenated tuple: ('Apple', 'Banana', 'Orange', 'extra')
[Link] USING DICTIONARIES
CODING:

person = {}
person['name'] = input("Enter the name: ")
person['age'] = input("Enter the age: ")
person['city'] = input("Enter the city: ")
print("Name:", person['name'])
print("Age:", person['age'])
print("City:", person['city'])

OUTPUT:

Enter the name: John


Enter the age: 25
Enter the city: London
Name: John
Age: 25
City: London
14. PROGRAM USING FOR FILE HANDLING
CODING:

filename = input("Enter the file name: ")


file = open(filename, 'w')
data = input("Enter data to be written to the file: ")
[Link](data)
[Link]()
file = open(filename, 'r')
contents = [Link]()
[Link]()
print("Contents of the file:")
print(contents)

OUTPUT:

Enter the file name: my_file.txt


Enter data to be written to the file: Hello, this is a file handling program.
Contents of the file:Hello, this is a file handling program

You might also like