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

Python Programs With Output

The document contains multiple Python programming examples covering string manipulation, finding the largest of three numbers, printing prime numbers, calculating factorial using recursion, and demonstrating list, tuple, dictionary operations, Fibonacci series, class and object creation, and inheritance. Each example includes code snippets and expected outputs. The document serves as a practical guide for basic Python programming concepts.

Uploaded by

mohammedarshad
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 views4 pages

Python Programs With Output

The document contains multiple Python programming examples covering string manipulation, finding the largest of three numbers, printing prime numbers, calculating factorial using recursion, and demonstrating list, tuple, dictionary operations, Fibonacci series, class and object creation, and inheritance. Each example includes code snippets and expected outputs. The document serves as a practical guide for basic Python programming concepts.

Uploaded by

mohammedarshad
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/ 4

1.

Create, Concatenate, Print String, and Access Sub-string

# Creating strings
str1 = "Hello"
str2 = "World"

# Concatenating strings
str3 = str1 + " " + str2
print("Concatenated String:", str3)

# Accessing sub-string
print("Sub-string (0 to 4):", str3[0:5])

Expected Output:
Concatenated String: Hello World
Sub-string (0 to 4): Hello

2. Find Largest of Three Numbers

# Taking three numbers as input from user


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

# Comparing numbers using if-elif-else


if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c

# Displaying the largest number


print("The largest number is:", largest)

Expected Output:
Sample Input:
Enter first number: 25
Enter second number: 17
Enter third number: 40
Output:
The largest number is: 40

3. Print Prime Numbers Less Than 20

# Printing prime numbers below 20


print("Prime numbers less than 20:")
for num in range(2, 20):
for i in range(2, num):
if num % i == 0:
break
else:
print(num, end=" ")

Expected Output:
Prime numbers less than 20:
2 3 5 7 11 13 17 19

4. Find Factorial Using Recursion

# Recursive function to calculate factorial


def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

# Taking input and calling factorial


num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))

Expected Output:
Enter a number: 5
Factorial of 5 is 120

5. List Operations - Create, Append, Remove

# Creating a list
my_list = [1, 2, 3]
print("Initial List:", my_list)

# Appending an element
my_list.append(4)
print("After Appending:", my_list)

# Removing an element
my_list.remove(2)
print("After Removing 2:", my_list)

Expected Output:
Initial List: [1, 2, 3]
After Appending: [1, 2, 3, 4]
After Removing 2: [1, 3, 4]

6. Tuple Demonstration

# Creating a tuple
my_tuple = (10, 20, 30, 40)

# Accessing elements and slicing


print("First element:", my_tuple[0])
print("Sliced tuple:", my_tuple[1:3])

Expected Output:
First element: 10
Sliced tuple: (20, 30)

7. Dictionary Demonstration

# Creating a dictionary
student = {'name': 'Alice', 'age': 22, 'course': 'Python'}
# Accessing and modifying dictionary
print("Name:", student['name'])
student['grade'] = 'A'
print("Updated Dictionary:", student)
del student['age']
print("After Removing Age:", student)

Expected Output:
Name: Alice
Updated Dictionary: {'name': 'Alice', 'age': 22, 'course': 'Python', 'grade': 'A'}
After Removing Age: {'name': 'Alice', 'course': 'Python', 'grade': 'A'}

8. Fibonacci Module and Import

# fib_module.py
def fibonacci(n):
fib_series = []
a, b = 0, 1
while len(fib_series) < n:
fib_series.append(a)
a, b = b, a + b
return fib_series

# main.py
import fib_module
n = int(input("Enter the number of Fibonacci terms: "))
print("Fibonacci Series:", fib_module.fibonacci(n))

Expected Output:
Enter the number of Fibonacci terms: 6
Fibonacci Series: [0, 1, 1, 2, 3, 5]

9. Class and Object

# Creating a class with constructor and method


class Student:
def __init__(self, name, age):
self.name = name
self.age = age

def display(self):
print("Name:", self.name)
print("Age:", self.age)

# Creating object and calling method


s1 = Student("John", 21)
s1.display()

Expected Output:
Name: John
Age: 21

10. Inheritance

# Base class
class Animal:
def __init__(self, name):
self.name = name

def speak(self):
print(self.name, "makes a sound")

# Derived class
class Dog(Animal):
def speak(self):
print(self.name, "barks")

# Creating object of subclass


d = Dog("Buddy")
d.speak()

Expected Output:
Buddy barks

You might also like