0% found this document useful (0 votes)
7 views9 pages

Riya Python Lab File

This document contains a collection of basic Python programs organized into categories such as basic programs, logic and loops, data structures, functions, strings, list-based programs, tuple-based programs, and dictionary-based programs. Each section includes sample code snippets for tasks like checking even or odd numbers, finding the largest number among three, calculating factorials, and manipulating lists and dictionaries. The document serves as a practical reference for common programming tasks in Python.
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)
7 views9 pages

Riya Python Lab File

This document contains a collection of basic Python programs organized into categories such as basic programs, logic and loops, data structures, functions, strings, list-based programs, tuple-based programs, and dictionary-based programs. Each section includes sample code snippets for tasks like checking even or odd numbers, finding the largest number among three, calculating factorials, and manipulating lists and dictionaries. The document serves as a practical reference for common programming tasks in Python.
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
You are on page 1/ 9

PYTHON LAB FILE

NAME : RIYA MAHTO


BTECH CSE 2A
2K24CUSN01058
BASIC PROGRAMS

1. Check Even or Odd


num = int(input("Enter a number: "))
print("Even" if num % 2 == 0 else "Odd")

2. Largest Among Three Numbers


a, b, c = map(int, input("Enter three numbers: ").split())
print("Largest:", max(a, b, c))

3. Factorial using Recursion


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

4. Reverse a String
s = input("Enter a string: ")
print(s[::-1])
LOGIC AND LOOPS
1. Fibonacci Sequence
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b

2. Prime Check
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

3. Count Vowels
s = input("Enter a string: ").lower()
print("Vowel Count:", sum(1 for ch in s if ch in 'aeiou'))
DATA STRUCTURES

1. Remove Duplicates from List


lst = list(map(int, input("Enter list elements: ").split()))
lst = list(set(lst))
print(lst)

2. Frequency of Elements in List


freq = {}
for item in lst:
freq[item] = freq.get(item, 0) + 1
print(freq)

FUNCTIONS
1. Check Palindrome Number
def is_palindrome(n):
return str(n) == str(n)[::-1]

2. Recursive Power Function


def power(base, exp):
return 1 if exp == 0 else base * power(base, exp - 1)
STRINGS
1. String Palindrome
s = input("Enter a string: ")
print("Palindrome" if s == s[::-1] else "Not Palindrome")

2. Frequency of Characters
from collections import Counter
s = input()
print(dict(Counter(s)))

LIST BASED PROGRAMS

1. Largest in List
print(max(lst))

2. Remove All Duplicates


lst = list(set(lst))
print(lst)

3. Count Even and Odd


even = sum(1 for x in lst if x % 2 == 0)
odd = len(lst) – even
print(f”Even: {even}, Odd: {odd}”)

4. Sort List
lst.sort()
print(lst)

5. Remove Negative Numbers


lst = [x for x in lst if x >= 0]
print(lst)

6. Second Largest Number


unique = list(set(lst))
unique.sort()
print(“Second Largest:”, unique[-2])

7. Sum and Average


s = sum(lst)
avg = s / len(lst)
print(f”Sum: {s}, Average: {avg}”)
TUPLE-BASED PROGRAMS
1. Create and Print Tuple
t = (1, 2, 3)
print(t)

2. Index of Element
print(t.index(2))

3. Count Occurrences
print(t.count(2))

4. Convert List to Tuple


t = tuple(lst)
print(t)

5. Check Existence
print(3 in t)

6. Length of Tuple
print(len(t))
7. Max and Min in Tuple
print(max(t), min(t))

8. Sum of Tuple
print(sum(t))

9. Iterate Through Tuple


for item in t:
print(item)

10. Concatenate Tuples


t1 = (1, 2)
t2 = (3, 4)
print(t1 + t2)

DICTIONARY-BASED PROGRAMS

1. Create Dictionary and Display Keys/Values


d = {'a': 1, 'b': 2}
print(d.keys())
print(d.values())
2. Character Frequency in String
s = input("Enter string: ")
print(dict(Counter(s)))

3. Remove Key
d.pop('a', None)
print(d)

4. Check Key Exists


print('b' in d)

5. Update Value of Existing Key


d['b'] = 20
print(d)

6. Count Number of Keys


print(len(d))

You might also like