Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Python_Code_with_Output_Full

Uploaded by

singh.yojit.k
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)
6 views

Python_Code_with_Output_Full

Uploaded by

singh.yojit.k
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/ 8

1.

Search for a given element in a list/tuple


Code:

lst = input("Enter a list of elements (comma-separated): ").split(',')

element = input("Enter the element to search: ")

print("Element found:", element in lst)

Input:

Enter a list of elements (comma-separated): a,b,c,d

Enter the element to search: c

Output:

Element found: True

2. Find the smallest and largest number in a list


Code:

numbers = list(map(int, input("Enter a list of numbers (space-separated): ").split()))

print("Smallest:", min(numbers))

print("Largest:", max(numbers))

Input:

Enter a list of numbers (space-separated): 3 5 1 8 2

Output:

Smallest: 1

Largest: 8
3. Dictionary for students scoring above 75
Code:

n = int(input("Enter the number of students: "))

students = {}

for _ in range(n):

roll_no = input("Enter roll number: ")

name = input("Enter name: ")

marks = int(input("Enter marks: "))

students[name] = marks

print("Students scoring above 75:", [name for name, marks in students.items() if marks > 75])

Input:

Enter the number of students: 3

Enter roll number: 101

Enter name: Alice

Enter marks: 80

Enter roll number: 102

Enter name: Bob

Enter marks: 65

Enter roll number: 103

Enter name: Charlie

Enter marks: 90

Output:

Students scoring above 75: ['Alice', 'Charlie']


4. Count words in a line
Code:

line = input("Enter a line: ")

print("Number of words:", len(line.split()))

Input:

Enter a line: This is a test line

Output:

Number of words: 5

5. Capitalize every other letter


Code:

string = input("Enter a string: ")

result = ''.join([char.upper() if i % 2 else char.lower() for i, char in enumerate(string)])

print("Modified string:", result)

Input:

Enter a string: school

Output:

Modified string: sChOoL

6. Perfect number and palindrome check


Code:

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


print("Is Perfect:", sum(i for i in range(1, num) if num % i == 0) == num)

print("Is Palindrome:", str(num) == str(num)[::-1])

Input:

Enter a number: 28

Output:

Is Perfect: True

Is Palindrome: False

7. Armstrong number check


Code:

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

print("Is Armstrong number:", num == sum(int(digit) ** len(str(num)) for digit in str(num)))

Input:

Enter a number: 153

Output:

Is Armstrong number: True

8. Prime or composite check


Code:

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

if num <= 1:

print("Neither prime nor composite")


else:

is_prime = all(num % i != 0 for i in range(2, int(num ** 0.5) + 1))

print("Prime" if is_prime else "Composite")

Input:

Enter a number: 29

Output:

Prime

9. Fibonacci series
Code:

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

a, b = 0, 1

for _ in range(n):

print(a, end=" ")

a, b = b, a + b

Input:

Enter the number of terms: 5

Output:

01123

10. GCD and LCM


Code:
from math import gcd

a, b = map(int, input("Enter two numbers: ").split())

print("GCD:", gcd(a, b))

print("LCM:", a * b // gcd(a, b))

Input:

Enter two numbers: 12 18

Output:

GCD: 6

LCM: 36

11. Count vowels, consonants, uppercase, and lowercase


Code:

string = input("Enter a string: ")

vowels = "aeiouAEIOU"

vowel_count = sum(1 for char in string if char in vowels)

consonant_count = sum(1 for char in string if char.isalpha() and char not in vowels)

upper_count = sum(1 for char in string if char.isupper())

lower_count = sum(1 for char in string if char.islower())

print("Vowels:", vowel_count, "Consonants:", consonant_count, "Uppercase:", upper_count,

"Lowercase:", lower_count)

Input:

Enter a string: Hello World


Output:

Vowels: 3 Consonants: 7 Uppercase: 2 Lowercase: 8

12. Palindrome check and case conversion


Code:

string = input("Enter a string: ")

print("Is Palindrome:", string == string[::-1])

print("Case converted:", string.swapcase())

Input:

Enter a string: Madam

Output:

Is Palindrome: False

Case converted: mADAM

13. Find the largest/smallest number in a list/tuple


Code:

container = tuple(map(int, input("Enter a list/tuple of numbers (space-separated): ").split()))

print("Smallest:", min(container))

print("Largest:", max(container))

Input:

Enter a list/tuple of numbers (space-separated): 7 2 9 3

Output:
Smallest: 2

Largest: 9

14. Swap elements at even and odd locations


Code:

numbers = list(map(int, input("Enter a list of numbers (space-separated): ").split()))

for i in range(0, len(numbers) - 1, 2):

numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i]

print("Modified list:", numbers)

Input:

Enter a list of numbers (space-separated): 1 2 3 4 5

Output:

Modified list: [2, 1, 4, 3, 5]

You might also like