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

computer

Uploaded by

rajputankit9312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

computer

Uploaded by

rajputankit9312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Mangolpur kalan, New Delhi

School Id. - 1412033

Name : Ankit
Class : 11th ‘A’
Roll no. : 23
Computer Sci. practical
file
Python Programs for Basic Operations and Patterns

1.Welcome Message
Input a welcome message and display it.

Code:
# Input a welcome message and display it.

welcome_message = input("Enter a welcome message: ")


print("Welcome Message:", welcome_message)

2.Find the Larger/Smaller of Two Numbers


Input two numbers and display the larger/smaller number.

Code:
# Input two numbers and display the larger/smaller number.

num1=float(input("Enter first number: ")) Output:


num2=float(input("Enter second number: ")) Enter first number: 5.5
larger = max(num1, num2) Enter second number: 3.2
smaller = min(num1, num2)
print("Larger number:", larger) Larger number: 5.5
print("Smaller number:", smaller) Smaller number: 3.2

3.Find the Largest/Smallest of Three Numbers


Input three numbers and display the largest/smallest number.

Code:
# Input three numbers and display the largest/smallest number.

num1=float(input("Enter first number: ")) Output:


num2=float(input("Enter second number: ")) Enter first number: 12.5
num3=float(input("Enter third number: ")) Enter second number: 9.8
largest = max(num1, num2, num3) Enter third number: 15.3
smallest = min(num1, num2, num3)
print("Largest number:", largest) Largest number: 15.3
print("Smallest number:", smallest) Smallest number: 9.8

4.Patterns with Nested Loops


Generate patterns using nested loops. Output: pattern-1
*
Code: **
# Pattern-1 ***
for i in range(1, 6): ****
print('*' * i) *****
# Pattern-2 Output: pattern-2
12345
for i in range(6, 1,-1): 1234
for j in range(1, i): 123
print(j, end="") 12
print() 1
# Pattern-3 Output: pattern-3

for i in range(1, 6): A


for j in range(1, i+1) AB
print(chr(64 + j) ) ABC
Print() ABCD
ABCDE
5.Sum of Series
Calculate various series sums based on inputs of x and n.
Output:
Code:
Enter value for x: 2
# Series: 1 + x + x^2 + x^3 + ... + x^n
Enter value for n: 5
x =float(input("Enter value for x: "))
n =int(input("Enter value for n: "))
Sum of series (1 + x +
series _sum = sum(x**i for i in range(n+1))
x^2 + ... + x^n): 63.0
print("Sum of series (1 + x + x^2 + ... + x^n):", series _sum)
Sum of alternating
# Series: 1- x + x^2- x^3 + ... ± x^n
series: -21.0
series_sum = sum((-1)**i * x**i for i in range(n+1))
print("Sum of alternating series:", series_sum)
Sum of x/n series:
17.066666666666666
# Series: x + x^2/2 + x^3/3 + ... + x^n/n
series_sum = sum((x**i)/i for i in range(1, n+1))
Sum of series with
print("Sum of x/n series:", series_sum)
factorial:
6.266666666666667
# Series: x + x^2/2! + x^3/3! + ... + x^n/n!
from math import factorial
series_sum = sum((x**i)/factorial(i) for i in range(1, n+1))
print("Sum of series with factorial:", series_sum)
6.Prime or Composite Number
Input a number and check if it is a prime or composite number.
Code:
# Check if a number is prime or composite Output:
num=int(input("Enter a number: ")) Input-1
if num > 1: Enter a number: 7
for i in range(2, int(num**0.5) + 1): 7 is a prime number
if num %i == 0:
print(num, "is a composite number") Input-2
break Enter a number: 10
else: 10 is a composite number
print(num, "is a prime number")
else:
print(num, "is not a prime number“)
7.Number Classification
Determine whether a number is a perfect number, an Armstrong number, or a palindrome.
Code:
# Check if a number is Perfect, Armstrong, or Palindrome

# Check if a number is a perfect number


def is_perfect(num): Output:
total = 0
for i in range(1, num): Input-1
if num %i == 0: Enter a number: 6
total += i Perfect number: True
return total == num Armstrong number: False
Palindrome: False
# Check if a number is an Armstrong number
def is_armstrong(num): Input-2
digits = str(num) Enter a number: 153
total = 0 Perfect number: False
for digit in digits: Armstrong number: True
total += int(digit) ** len(digits) Palindrome: False
return total == num
# Check if a number is a palindrome Input-3
def is_palindrome(num): Enter a number: 121
str_num = str(num) Perfect number: False
return str_num == str_num[::-1] Armstrong number: False
Palindrome: True
# Input number
num=int(input("Enter a number: "))

# Display results
print("Perfect number:", is_perfect(num))
print("Armstrong number:", is_armstrong(num))
print("Palindrome:", is_palindrome(num))

8.Fibonacci Series
Display the terms of a Fibonacci series.

Code:

# Display terms of the Fibonacci series Output:


n =int(input("Enter number of terms: ")) Enter number of terms: 7
a, b = 0, 1 0112358
for _ in range(n):
print(a, end=" ")
a, b = b, a +
9.GCD and LCM of Two Integers
Compute the greatest common divisor and least common multiple of two integers.

Code:
# Compute GCDandLCM import math

num1=int(input("Enter first number: ")) Output:


num2=int(input("Enter second number: ")) Input-1
gcd = math.gcd(num1, num2) Enter first number: 12
lcm = abs(num1 * num2) // gcd Enter second number: 15
print("GCD:", gcd) GCD: 3
print("LCM:", lcm) LCM: 60

10.Vowel, Consonant, Uppercase, Lowercase Counter


Count and display the number of vowels, consonants, uppercase, and lowercase characters
in a string.
Code:
# Count vowels, consonants, uppercase, and lowercase letters
text = input("Enter a string: ")
vowels = consonants = uppercase = lowercase = 0 Output:
for char in text: Enter a string: Python
if char.isalpha(): Programming
if char in "AEIOUaeiou":
vowels += 1 Vowels: 5 Consonants: 13
else: Uppercase: 2 Lowercase: 16
consonants += 1
if char.isupper():
uppercase += 1
else:
lowercase += 1
print("Vowels:", vowels, "Consonants:", consonants, "Uppercase:", uppercase,
"Lowercase:", lowercase)
11.Palindrome and Case Conversion
Input a string, determine if it is a palindrome, and convert the case of characters.
Code:
# Check if a string is a palindrome and convert the case of each character
#Define a function

def isPalindrome(string): Output:


if (string == string[::-1]) : Enter string: "Madam“
return "The string is a palindrome." The string is a palindrome.
else:
return "The string is not a palindrome.“ Enter string: "Hello“
The string is not a palindrome.
#Enter input string

string = input ("Enter string: ")


print(isPalindrome(string))
12.Largest/Smallest Number in a List or Tuple
Find the largest and smallest number in a list or tuple.
Output:
Code: data=5 2 9 1 3
# Find the largest and smallest number in a list/tuple
Largest number: 9
data = list(input("Enter a list or tuple of numbers: ")) Smallest number: 1
largest = max(data)
smallest = min(data)
print("Largest number:", largest, "Smallest number:", smallest)

13.Swap Elements at Even and Odd Indices in a List


Input a list of numbers and swap elements at even indices with elements at odd indices.

Code:
# Swap elements at even and odd indices in a list

data = list(map(int, input("Enter a list of numbers: ").split())) Output:


for i in range(1, len(data), 2): data=1 2 3 4 5 6
data[i-1], data[i] = data[i], data[i-1] Swapped list: [2, 1, 4, 3, 6, 5]
print("Swapped list:", data)

14.Search for an Element in a List or Tuple


Input a list/tuple of elements and search for a given element.
Output:
Code: Enter a list or tuple of
# Search for an element in a list or tuple elements: [1, 2, 3, 4, 5]

data = eval(input("Enter a list or tuple of elements: ")) Enter the element to


element = input("Enter the element to search for: ") search for: 3
found = element in data
print("Element found:", found) Element found: True

15.Dictionary of Students with Marks Above 75


Create a dictionary with the roll number, name, and marks of students, and display those
with marks above 75.

Code:
# Create a dictionary of students and display those with marks above 75

students = {}
n =int(input("Enter number of students: "))
for _ in range(n):
roll_no = input("Enter roll number: ")
name =input("Enter name: ")
marks = int(input("Enter marks: "))
students[roll_no] = {"name": name, "marks": marks}
high_achievers = {k: v["name"] for k, v in students.items() if v["marks"] > 75}
print("Students with marks above 75:", high_achievers)

You might also like