0% found this document useful (0 votes)
3 views12 pages

Cs Practical H

The document contains a series of Python code snippets that demonstrate various programming tasks, including creating patterns, calculating mathematical series, checking properties of numbers and strings, and manipulating lists and dictionaries. Each task is accompanied by user input prompts and example outputs. The tasks cover a range of topics from basic loops and conditionals to more complex data structures.

Uploaded by

swayashpal143
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)
3 views12 pages

Cs Practical H

The document contains a series of Python code snippets that demonstrate various programming tasks, including creating patterns, calculating mathematical series, checking properties of numbers and strings, and manipulating lists and dictionaries. Each task is accompanied by user input prompts and example outputs. The tasks cover a range of topics from basic loops and conditionals to more complex data structures.

Uploaded by

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

1) Make a pyramid of *

num_steps = int(input("Enter the number of steps for


the staircase: "))

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


for j in range(i):
print("*", end="")
print()

Enter the number of steps for the staircase: 5

*
* *
* * *
* * * *
* * * * *

2) Upside down number staircase


num_steps = int(input("Enter the number of steps for the
upside-down staircase: "))

for i in range(num_steps, 0, -1):


for j in range(1, i + 1):
print(j, end=" ")
print()

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

3) Alphabet staicase
num_steps = int(input("Enter the number of steps for the staircase:
"))

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


for j in range(ord('A'), ord('A') + i):
print(chr(j), end="")
print()

A
AB
ABC
ABCD
ABCDE

4) 1 + x + x^2 + x^3…+ x^n


x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n:"))

result = 0
for i in range(n + 1):
result += x ** i

print(f"The result of the series is: {result}")

ENTER THE VALUE OF x: 2


ENTER THE VALUE OF n: 4
THE RESULT OF THE SERIES IS: 31.0

5) 1-x+x^2-x^3…+x^n

x = float(input("Enter the value of x: "))


n = int(input("Enter the value of n: "))

result = 0
for i in range(n + 1):
result += ((-1) ** i) * (x ** i)

print(f"The result of the series is: {result}")


ENTER THE VALUE OF x: 5
ENTER THE VALUE OF n: 6
THE RUSULT OF THE SERIES IS: 13021.0

6) (x^2)/2 + (x^3)/3…+
(x^n)/n
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

result = 0
for i in range(1, n + 1):
result += (x ** i) / i

print(f"The result of the series is: {result}")

ENTER THE VALUE OF x: 2


ENTER THE VALUE OF n: 3
THE RESULT OF THE SERIES IS: 6.6666666666666

7) X + (x^2)/2! + (x^3)/3!…+
(x^n)/n!

import math
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

result = 0
for i in range(1, n + 1):
result += (x ** i) / math.factorial(i)

print(f"The result of the series is: {result}")

ENTER THE VALUE OF x: 3


ENTER THE VALUE OF n: 6
THE RESULT OF THE SERIES IS: 18.41249999999998

8) Check weather the number is palindrome or


normal or armstrong

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

if str(num) == str(num)[::-1]:
print("The number is Palindrome.")
elif num == sum(int(digit) ** len(str(num)) for digit
in str(num)):
print("The number is Armstrong.")
else:
print("The number is Normal.")
ENTER A NUMBER: 242
THE NUMBER IS PALINDROME

9)check weather number is prime or composite

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

if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print("The number is Composite.")
break
else:
print("The number is Prime.")
elif num == 1:
print("The number is neither Prime nor
Composite.")
else:
print("Invalid input. Please enter a positive
integer greater than 1.")

ENTER A NUMBER: 4
THE NUMBER IS COMPOSITE.
9) Print fibonacci series

num_terms = int(input("Enter the number of terms in


the Fibonacci series: "))
fibonacci_series = [0, 1]

for i in range(2, num_terms):


next_term = fibonacci_series[i - 1] +
fibonacci_series[i - 2]
fibonacci_series.append(next_term)

print("Fibonacci Series:")
for term in fibonacci_series:
print(term, end=" ")

ENTER THE NUMBER OF TERMS IN THE FIBONACCI SERIES: 7


FIBONACCI SERIES: 0 1 1 2 3 5 8

11)tell the number of vowels consonants uppercase and


lowercase characters in string

input_string = input("Enter a string: ")

lowercase_count = 0
uppercase_count = 0
vowel_count = 0
consonant_count = 0
vowels = set("aeiouAEIOU")

for char in input_string:


if char.islower():
lowercase_count += 1
elif char.isupper():
uppercase_count += 1
if char.isalpha():
if char.lower() in vowels:
vowel_count += 1
else:
consonant_count += 1

print(f"Number of lowercase characters:


{lowercase_count}")
print(f"Number of uppercase characters:
{uppercase_count}")
print(f"Number of vowels: {vowel_count}")
print(f"Number of consonants: {consonant_count}")

ENTER A STRING: pgobiaaibohp


NUMBER OF LOWERCASE CHARACTERS: 12
NUMBER OF UPPERCASE CHARACTERS: 0
NUMBER OF VOWELS: 6
NUMBER OF CONSONANTS: 6
12)check is string is palindrome

input_string = input("Enter a string: ")

cleaned_string = input_string.replace(" ",


"").lower()

if cleaned_string == cleaned_string[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

ENTER A STRING: PHOBIAAIBOHP


The string is a palindrome.

13) enter a list and switch positions of elements


from even index to odd index

numbers = input("Enter a list of numbers separated by


spaces: ").split()
numbers = [int(num) for num in numbers]

if len(numbers) % 2 == 0:
for i in range(0, len(numbers), 2):
numbers[i], numbers[i + 1] = numbers[i + 1],
numbers[i]

print("Modified List:", numbers)


else:
print("The list should have an even number of
elements for swapping.")

ENTER A LIST OF NUMBER SEPARATED BY SPACES: 2 3 4 5 6


7 8 9

MODIFIED LIST: [3 , 2, 5, 4, 7, 6, 9, 8]

14) make a dictionary or students with roll number


name and marks and display marks of 75% above scorers

num_students = int(input("Enter the number of


students: "))

students_dict = {}

for _ in range(num_students):
roll_number = input("Enter Roll Number: ")
name = input("Enter Name: ")
marks = float(input("Enter Marks: "))
students_dict[roll_number] = {'Name': name,
'Marks': marks}

max_marks = 100.0
cutoff_percentage = 75.0
cutoff_marks = (cutoff_percentage / 100) * max_marks

print("\nStudents with Marks Above 75%:")


for roll_number, details in students_dict.items():
if details['Marks'] > cutoff_marks:
print(f"Roll Number: {roll_number}, Name:
{details['Name']}, Marks: {details['Marks']}")

ENTER THE NUMBER OF STUDENTS: 3


ENTER ROLL NUMBER: 1
ENTER NAME: BIRJU
ENTER MARKS: 23
ENTER ROLL NUMBER: 2
ENTER NAME: AKANSHA
ENTER MARKS: 79
ENTER ROLL NUMBER: 3
ENTER NAME: KURKURE
ENTER MARKS: 3

STUDENTS WITH MARKS ABOVE 75%:


ROLL NUMBER:2, NAME: AKANSHA< MARKS: 79.0

You might also like