0% found this document useful (0 votes)
8 views24 pages

Record Python

The document outlines a series of Python programming exercises demonstrating various concepts such as branching statements, loops, functions, string operations, lists, tuples, dictionaries, file handling, directories, exception handling, inheritance, and classes. Each exercise includes an aim, algorithm, program code, output, and a result indicating successful execution. The exercises serve as practical examples for learning fundamental Python programming techniques.

Uploaded by

R S Jeysiva
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)
8 views24 pages

Record Python

The document outlines a series of Python programming exercises demonstrating various concepts such as branching statements, loops, functions, string operations, lists, tuples, dictionaries, file handling, directories, exception handling, inheritance, and classes. Each exercise includes an aim, algorithm, program code, output, and a result indicating successful execution. The exercises serve as practical examples for learning fundamental Python programming techniques.

Uploaded by

R S Jeysiva
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/ 24

EX NO 1 PYTHON PROGRAM USING BRANCHING

STATEMENTS

AIM

To write a Python program that checks whether a number is positive, negative, or


zero using if-elif-else.

ALGORITHM

STEP 1: Start the program.

STEP 2: Take an integer input from the user.

STEP 3: Use if-elif-else to check:

1) If the number is greater than zero, print "Positive Number".


2) If the number is less than zero, print "Negative Number".
3) Else, print "Zero".

STEP 4: End the program.


PROGRAM

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

if num > 0:

print("Positive Number")

elif num < 0:

print("Negative Number")

else:

print("Zero")

OUTPUT

Enter a number: -5

Negative Number

RESULT

Thus, the above program is executed successfully.


EX NO 2 PYTHON PROGRAM USING LOOPING
STATEMENTS

AIM

To write a Python program that prints numbers from 1 to 10 using a for loop.

ALGORITHM

STEP 1: Start the program.

STEP 2: Use a for loop to iterate from 1 to 10.

STEP 3: Print each number.

STEP 4: End the program.


PROGRAM

for i in range(1, 11):

print(i)

OUTPUT

10

RESULT

Thus, the above program is executed successfully.


EX NO 3 PYTHON PROGRAM USING FUNCTIONS

AIM

To write a Python program that calculates the square of a number using a


function.

ALGORITHM

STEP 1: Start the program.

STEP 2: Define a function square(num).

STEP 3: Take user input for a number.

STEP 4: Call the function and print the result.

STEP 5: End the program.


PROGRAM

def square(num):

return num * num

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

print("Square:", square(n))

OUTPUT

Enter a number: 4

Square: 16

RESULT

Thus, the above program is executed successfully.


EX NO 4 PYTHON PROGRAM USING STRING
OPERATIONS

AIM

To perform basic string operations like concatenation, length, and slicing.

ALGORITHM

STEP 1: Start the program.

STEP 2: Take a string input from the user.

STEP 3: Perform string concatenation, length calculation, and slicing.

STEP 4: Print the results.

STEP 5: End the program.


PROGRAM

s = input("Enter a string: ")

print("Uppercase:", s.upper())

print("Concatenation:", s + " World")

print("Substring:", s[0:3]) # First 3 characters

OUTPUT

Enter a string: Hello

Uppercase: HELLO

Concatenation: Hello World

Substring: Hel

RESULT

Thus, the above program is executed successfully.


EX NO 5 PYTHON PROGRAM USING LIST

AIM

To demonstrate list operations like append, remove, and sorting.

ALGORITHM

STEP 1: Start the program.

STEP 2: Define a list.

STEP 3: Perform append, remove, and sorting operations.

STEP 4: Print the results.

STEP 5: End the program.


PROGRAM

numbers = [3, 1, 4, 2]

numbers.append(5)

numbers.remove(3)

numbers.sort()

print("Updated List:", numbers)

OUTPUT

Updated List: [1, 2, 4, 5]

RESULT

Thus, the above program is executed successfully.


EX NO 6 PYTHON PROGRAM USING TUPLES

AIM

To demonstrate tuple operations like accessing elements and slicing.

ALGORITHM

STEP 1: Start the program.

STEP 2: Define a tuple.

STEP 3: Access elements and perform slicing.

STEP 4: Print the results.

STEP 5: End the program.


PROGRAM

tup = (10, 20, 30, 40)

print("First Element:", tup[0])

print("Last Two Elements:", tup[-2:])

OUTPUT

First Element: 10

Last Two Elements: (30, 40)

RESULT

Thus, the above program is executed successfully.


EX NO 7 PYTHON PROGRAM USING DICTIONARIES

AIM

To demonstrate dictionary operations like adding, deleting, and retrieving


values.

ALGORITHM

STEP 1: Start the program.

STEP 2: Define a dictionary.

STEP 3: Perform key-value operations.

STEP 4: Print the results.

STEP 5: End the program.


PROGRAM

student = {"name": "John", "age": 20}

student["course"] = "CS"

del student["age"]

print("Updated Dictionary:", student)

OUTPUT

Updated Dictionary: {'name': 'John', 'course': 'CS'}

RESULT

Thus, the above program is executed successfully.


EX NO 8 PYTHON PROGRAM USING TEXT FILES

AIM

To write and read a text file.

ALGORITHM

STEP 1: Start the program.

STEP 2: Open a file in write mode and write data.

STEP 3: Open the file in read mode and read the content.

STEP 4: Print the content.

STEP 5: End the program.


PROGRAM

with open("sample.txt", "w") as file:

file.write("Hello, Python!")

with open("sample.txt", "r") as file:

print("File Content:", file.read())

OUTPUT

File Content: Hello, Python!

RESULT

Thus, the above program is executed successfully.


EX NO 9 PYTHON PROGRAM USING DIRECTORIES

AIM

To create and list directories.

ALGORITHM

STEP 1: Start the program.

STEP 2: Create a new directory using os module.

STEP 3: List all files in the directory.

STEP 4: End the program.


PROGRAM

import os

os.mkdir("NewFolder")

print("Directories:", os.listdir("."))

OUTPUT

Directories: ['NewFolder', ...]

RESULT

Thus, the above program is executed successfully.


EX NO 10 PYTHON PROGRAM USING EXCEPTION
HANDLING

AIM

To handle division by zero using try-except.

ALGORITHM

STEP 1: Start the program.

STEP 2: Use try-except to handle division errors.

STEP 3: Print the result or an error message.

STEP 4: End the program.


PROGRAM

try:

a, b = 5, 0

print(a / b)

except ZeroDivisionError:

print("Cannot divide by zero!")

OUTPUT

Cannot divide by zero!

RESULT

Thus, the above program is executed successfully.


EX NO 11 PYTHON PROGRAM USING INHERITANCE

AIM

To demonstrate single inheritance in Python.

ALGORITHM

STEP 1: Start the program.

STEP 2: Create a base class and a derived class.

STEP 3: Instantiate the derived class and call methods.

STEP 4: End the program.


PROGRAM

class Animal:

def sound(self):

print("Animal makes a sound")

class Dog(Animal):

def bark(self):

print("Dog barks")

d = Dog()

d.sound()

d.bark()

OUTPUT

Animal makes a sound

Dog barks

RESULT

Thus, the above program is executed successfully.


EX NO 12 PYTHON PROGRAM USING CLASSES AND
OBJECTS

AIM

To create a class Person with attributes and methods.

ALGORITHM

STEP 1: Start the program.

STEP 2: Define a class with attributes and methods.

STEP 3: Create an object and call methods.

STEP 4: End the program.


PROGRAM

class Person:

def __init__(self, name):

self.name = name

def display(self):

print("Name:", self.name)

p = Person("Alice")

p.display()

OUTPUT

Name: Alice

RESULT

Thus, the above program is executed successfully.

You might also like