0% found this document useful (0 votes)
5 views

Readable_Python_Code_with_Output

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)
5 views

Readable_Python_Code_with_Output

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/ 12

1.

Search for a given element in a list/tuple


Code:

# Input the list of elements

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

# Input the element to search

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

# Check if the element is in the list

found = element in lst

# Print the result

print("Found:", found)

Input:

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

Enter element to search: b

Output:

Found: True

2. Find the smallest and largest number in a list


Code:

# Input the list of numbers

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


# Find smallest and largest numbers

smallest = min(nums)

largest = max(nums)

# Print the results

print("Smallest:", smallest)

print("Largest:", largest)

Input:

Enter numbers (space-separated): 3 5 1 8

Output:

Smallest: 1

Largest: 8

3. Dictionary for students scoring above 75


Code:

# Input the number of students

n = int(input("Number of students: "))

# Create a dictionary for students and their marks

students = {}

for _ in range(n):

name = input("Name: ")

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

students[name] = marks
# Find students scoring above 75

above_75 = [name for name, marks in students.items() if marks > 75]

# Print the result

print("Above 75:", above_75)

Input:

Number of students: 2

Name: Alice

Marks: 80

Name: Bob

Marks: 70

Output:

Above 75: ['Alice']

4. Count words in a line


Code:

# Input the line of text

line = input("Enter a line: ")

# Split the line into words and count them

word_count = len(line.split())

# Print the result


print("Words:", word_count)

Input:

Enter a line: This is simple

Output:

Words: 3

5. Capitalize every other letter


Code:

# Input the string

s = input("Enter string: ")

# Modify the string by capitalizing every other letter

result = ""

for i, c in enumerate(s):

if i % 2 == 0:

result += c.lower()

else:

result += c.upper()

# Print the modified string

print("Result:", result)

Input:

Enter string: hello


Output:

Result: hElLo

6. Perfect number and palindrome check


Code:

# Input the number

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

# Check if the number is a perfect number

sum_of_divisors = sum(i for i in range(1, n) if n % i == 0)

is_perfect = sum_of_divisors == n

# Check if the number is a palindrome

is_palindrome = str(n) == str(n)[::-1]

# Print the results

print("Perfect:", is_perfect)

print("Palindrome:", is_palindrome)

Input:

Enter number: 28

Output:

Perfect: True

Palindrome: False
7. Armstrong number check
Code:

# Input the number

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

# Check if the number is an Armstrong number

armstrong_sum = sum(int(d)**len(str(n)) for d in str(n))

is_armstrong = n == armstrong_sum

# Print the result

print("Armstrong:", is_armstrong)

Input:

Enter number: 153

Output:

Armstrong: True

8. Prime or composite check


Code:

# Input the number

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

# Check if the number is prime or composite

if n <= 1:
result = "Neither prime nor composite"

else:

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

result = "Prime" if is_prime else "Composite"

# Print the result

print(result)

Input:

Enter number: 29

Output:

Prime

9. Fibonacci series
Code:

# Input the number of terms

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

# Generate the Fibonacci series

a, b = 0, 1

for _ in range(n):

print(a, end=" ")

a, b = b, a + b

Input:
Enter terms: 5

Output:

01123

10. GCD and LCM


Code:

# Import the gcd function

from math import gcd

# Input two numbers

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

# Calculate GCD and LCM

g = gcd(a, b)

lcm = a * b // g

# Print the results

print("GCD:", g)

print("LCM:", lcm)

Input:

Enter two numbers: 12 18

Output:

GCD: 6
LCM: 36

11. Count vowels, consonants, uppercase, and lowercase


Code:

# Input the string

s = input("Enter string: ")

# Initialize counters

vowels = consonants = uppercase = lowercase = 0

# Count each type of character

for c in s:

if c.isalpha():

if c.lower() in "aeiou":

vowels += 1

else:

consonants += 1

if c.isupper():

uppercase += 1

if c.islower():

lowercase += 1

# Print the results

print("Vowels:", vowels)

print("Consonants:", consonants)

print("Uppercase:", uppercase)
print("Lowercase:", lowercase)

Input:

Enter string: Hello

Output:

Vowels: 2

Consonants: 3

Uppercase: 1

Lowercase: 4

12. Palindrome check and case conversion


Code:

# Input the string

s = input("Enter string: ")

# Check if the string is a palindrome

is_palindrome = s == s[::-1]

# Convert the case of the string

converted = s.swapcase()

# Print the results

print("Palindrome:", is_palindrome)

print("Case converted:", converted)


Input:

Enter string: Madam

Output:

Palindrome: False

Case converted: mADAM

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


Code:

# Input the numbers

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

# Find the smallest and largest numbers

smallest = min(nums)

largest = max(nums)

# Print the results

print("Smallest:", smallest)

print("Largest:", largest)

Input:

Enter numbers: 7 2 9

Output:

Smallest: 2

Largest: 9
14. Swap elements at even and odd locations
Code:

# Input the list of numbers

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

# Swap elements at even and odd positions

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

nums[i], nums[i + 1] = nums[i + 1], nums[i]

# Print the modified list

print("Swapped:", nums)

Input:

Enter numbers: 1 2 3 4

Output:

Swapped: [2, 1, 4, 3]

You might also like