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

Programs

The document contains various Python programming examples covering topics such as Fibonacci series, mean and standard deviation calculations, palindrome checks, file handling, exception handling, class and object creation, operator overloading, and more. Each example includes code snippets and sample outputs demonstrating the functionality. The document serves as a comprehensive guide for basic Python programming concepts and operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Programs

The document contains various Python programming examples covering topics such as Fibonacci series, mean and standard deviation calculations, palindrome checks, file handling, exception handling, class and object creation, operator overloading, and more. Each example includes code snippets and sample outputs demonstrating the functionality. The document serves as a comprehensive guide for basic Python programming concepts and operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Fibbonacci Series

n = int(input("Enter number of terms: "))

a=0

b=1

print("Fibonacci Series:")

for i in range(n):

print(a, end=" ")

c=a+b

a=b

b=c

output

Enter number of terms: 6

Fibonacci Series:

011235

1. Numbers divisible by 3 or 5 but not both


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

if (n % 3 == 0 or n % 5 == 0) and not (n % 3 == 0 and n % 5 == 0):

print("Divisible by 3 or 5 but not both")

else:

print("Condition not satisfied")

output

Enter a number: 9

Divisible by 3 or 5 but not both


2. Mean, Variance & Standard Deviation

import math

nums = list(map(int, input("Enter numbers separated by space: ").split()))


n = len(nums)

mean = sum(nums) / n
variance = sum((x - mean) ** 2 for x in nums) / n
std_dev = [Link](variance)

print("Mean =", mean)


print("Variance =", variance)
print("Standard Deviation =", std_dev)

output:
Enter numbers separated by space: 2 4 6 8
Mean = 5.0
Variance = 5.0
Standard Deviation = 2.23606797749979

3. Dictionary Example

student = {"Name": "Vani", "Age": 21, "Branch": "CSE"}

print("Keys:", [Link]())
print("Values:", [Link]())
print("Items:", [Link]())

output:
Keys: dict_keys(['Name', 'Age', 'Branch'])
Values: dict_values(['Vani', 21, 'CSE'])
Items: dict_items([('Name', 'Vani'), ('Age', 21), ('Branch', 'CSE')])

4. Palindrome Check

s = input("Enter a string: ")

if s == s[::-1]:
print("Palindrome")

else:

print("Not Palindrome")

output: Enter a string: madam

Palindrome

5. Count Character Frequency


s = input("Enter a string: ")
freq = {}

for char in s:
freq[char] = [Link](char, 0) + 1

print("Character Frequency:", freq)

output:
Enter a string: hello
Character Frequency: {'h': 1, 'e': 1, 'l': 2, 'o': 1}

6. Write and Read File


file = open("[Link]", "w")
[Link]("Hello VTU Students")
[Link]()

file = open("[Link]", "r")


content = [Link]()
print("File Content:", content)
[Link]()

output:
File Content: Hello VTU Students

7. Exception Handling

try:

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

b = int(input("Enter number: "))


print("Result =", a/b)

except ZeroDivisionError:

print("Cannot divide by zero")

output:

Enter number: 10

Enter number: 0

Cannot divide by zero

8. Class & Object with init and str


class Student:
def __init__(self, name, marks):
[Link] = name
[Link] = marks

def __str__(self):
return f"Name: {[Link]}, Marks: {[Link]}"

s1 = Student("Vani", 95)
print(s1)

output:
Name: Vani, Marks: 95

9. Operator Overloading

class Add:

def __init__(self, value):

[Link] = value

def __add__(self, other):

return [Link] + [Link]

a = Add(10)

b = Add(20)
print("Sum =", a + b)

output:

Sum = 30

10. Factorial Using Loop


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

for i in range(1, n+1):


fact *= i

print("Factorial =", fact)

output:
Enter a number: 5
Factorial = 120

11. Prime Number Check


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

if n > 1:
for i in range(2, n):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime Number")
else:
print("Not Prime")

output:
Enter a number: 7
Prime Number

12. Armstrong Number


n = int(input("Enter number: "))
temp = n
sum = 0
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

if sum == n:
print("Armstrong Number")
else:
print("Not Armstrong")

output:
Enter number: 153
Armstrong Number

13. Largest Element in List


nums = list(map(int, input("Enter numbers: ").split()))
print("Largest =", max(nums))

output:
Enter numbers: 4 9 2 7
Largest = 9

14. Remove Duplicates from List

nums = list(map(int, input("Enter numbers: ").split()))

unique = list(set(nums))

print("After removing duplicates:", unique)

output:

Enter numbers: 1 2 2 3 4 4

After removing duplicates: [1, 2, 3, 4]

15. Count Vowels and Consonants

s = input("Enter string: ")

vowels = "aeiouAEIOU"

v=c=0

for ch in s:
if [Link]():

if ch in vowels:

v += 1

else:

c += 1

print("Vowels =", v)

print("Consonants =", c)

output:

Enter string: Hello

Vowels = 2

Consonants = 3

16. . Reverse Each Word in Sentence


s = input("Enter sentence: ")

words = [Link]()

for word in words:

print(word[::-1], end=" ")


output:
Enter sentence: Python Programming

nohtyP gnimmargorP

17. Count Words in File

file = open("[Link]", "r")

content = [Link]()
words = [Link]()

print("Total words:", len(words))

[Link]()

output:

Total words: 3

18. Copy File Content

with open("[Link]", "r") as f1:

with open("[Link]", "w") as f2:

[Link]([Link]())

print("File copied successfully")

output:

File copied successfully

19. Inheritance Example

class Person:

def __init__(self, name):

[Link] = name

class Student(Person):

def __init__(self, name, marks):

super().__init__(name)
[Link] = marks

def display(self):

print("Name:", [Link])

print("Marks:", [Link])

s = Student("Vani", 90)

[Link]()

output:

Name: Vani

Marks: 90

20. Simple Bank Account Class

class Bank:

def __init__(self, balance):

[Link] = balance

def deposit(self, amount):

[Link] += amount

def withdraw(self, amount):

[Link] -= amount

def display(self):

print("Balance:", [Link])
b = Bank(1000)

[Link](500)

[Link](200)

[Link]()

output:

Balance: 1300

You might also like