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

Merged_Python_Programs

This document provides an overview of basic Python list operations suitable for 9th/10th graders, including methods such as append, extend, insert, remove, pop, sort, and reverse. It includes example code snippets demonstrating each operation and additional Python programs for tasks like checking prime numbers, generating Fibonacci sequences, and counting vowels. The document serves as a practical guide for students to learn and practice Python programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Merged_Python_Programs

This document provides an overview of basic Python list operations suitable for 9th/10th graders, including methods such as append, extend, insert, remove, pop, sort, and reverse. It includes example code snippets demonstrating each operation and additional Python programs for tasks like checking prime numbers, generating Fibonacci sequences, and counting vowels. The document serves as a practical guide for students to learn and practice Python programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Python List Operations

This document contains basic Python list operations for 9th/10th graders, including append, extend,

insert, remove, pop, sort, reverse, and more.


Appending an Element

fruits = ["apple", "banana", "cherry"]

fruits.append("orange")

print(fruits)
Extending a List

numbers = [1, 2, 3]

numbers.extend([4, 5, 6])

print(numbers)
Inserting an Element

colors = ["red", "blue", "green"]

colors.insert(1, "yellow")

print(colors)
Removing an Element

animals = ["dog", "cat", "elephant"]

animals.remove("cat")

print(animals)
Popping an Element

students = ["John", "Mike", "Sara"]

removed_student = students.pop(1)

print(students)

print("Removed:", removed_student)
Finding Index

numbers = [10, 20, 30, 40, 50]

index = numbers.index(30)

print("Index of 30:", index)


Counting Occurrences

marks = [85, 90, 85, 80, 85]

count = marks.count(85)

print("85 appears", count, "times")


Sorting a List

ages = [25, 18, 30, 22]

ages.sort()

print("Sorted ages:", ages)


Reversing a List

letters = ["a", "b", "c", "d"]

letters.reverse()

print("Reversed list:", letters)


Python List Operations and Examples

1. Append to a List

my_list = [1, 2, 3]

my_list.append(4)

print("After append:", my_list)

2. Insert into a List

my_list = [1, 2, 4]

my_list.insert(2, 3)

print("After insert:", my_list)

3. Remove from a List

my_list = [1, 2, 3, 4]

my_list.remove(3)

print("After remove:", my_list)

4. Pop from a List

my_list = [1, 2, 3, 4]

popped_element = my_list.pop()

print("Popped element:", popped_element)


print("After pop:", my_list)

5. Clear a List

my_list = [1, 2, 3, 4]

my_list.clear()

print("After clear:", my_list)

6. List Index and Count

my_list = [1, 2, 3, 2, 4, 2]

index_of_3 = my_list.index(3)

count_of_2 = my_list.count(2)

print("Index of 3:", index_of_3)

print("Count of 2:", count_of_2)

7. Sorting a List

my_list = [4, 2, 3, 1]

my_list.sort()

print("Sorted list:", my_list)

8. Reverse a List

my_list = [1, 2, 3, 4]
my_list.reverse()

print("Reversed list:", my_list)

9. List Slicing

my_list = [10, 20, 30, 40, 50]

print("Sliced list [1:4]:", my_list[1:4])

print("Sliced list with step [::2]:", my_list[::2])

10. List Comprehension

squares = [x**2 for x in range(1, 6)]

print("Squares using list comprehension:", squares)

11. Copying a List

original = [1, 2, 3]

copy_list = original.copy()

copy_list.append(4)

print("Original list:", original)

print("Copied list:", copy_list)

12. Joining Two Lists

list1 = [1, 2, 3]
list2 = [4, 5, 6]

joined_list = list1 + list2

print("Joined list:", joined_list)

13. Nested Lists

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print("Matrix:")

for row in matrix:

print(row)

14. List Membership Check

my_list = ['apple', 'banana', 'cherry']

item = 'banana'

if item in my_list:

print(item, "is in the list.")

else:

print(item, "is not in the list.")

15. List Length

my_list = [1, 2, 3, 4, 5]

length = len(my_list)

print("Length of the list:", length)


Python Programs for 9th/10th Grade

1. Check if a Number is Prime

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

if num > 1:

for i in range(2, int(num / 2) + 1):

if num % i == 0:

print(num, "is not a prime number.")

break

else:

print(num, "is a prime number.")

else:

print(num, "is not a prime number.")

2. Fibonacci Sequence

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

a, b = 0, 1

print("Fibonacci Sequence:")

for _ in range(terms):

print(a, end=" ")

a, b = b, a + b

3. Factorial of a Number
num = int(input("Enter a number: "))

factorial = 1

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

factorial *= i

print(f"Factorial of {num} is {factorial}")

4. Palindrome Check for Strings

text = input("Enter a string: ")

if text == text[::-1]:

print("The string is a palindrome.")

else:

print("The string is not a palindrome.")

5. Count Vowels and Consonants

text = input("Enter a string: ").lower()

vowels = "aeiou"

vowel_count = 0

consonant_count = 0

for char in text:

if char.isalpha():

if char in vowels:

vowel_count += 1

else:

consonant_count += 1
print("Vowels:", vowel_count)

print("Consonants:", consonant_count)

6. Armstrong Number

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

sum_of_powers = sum(int(digit) ** len(str(num)) for digit in str(num))

if num == sum_of_powers:

print(num, "is an Armstrong number.")

else:

print(num, "is not an Armstrong number.")

7. Pyramid Pattern

rows = int(input("Enter number of rows: "))

for i in range(rows):

print(' ' * (rows - i - 1) + '* ' * (i + 1))

8. Inverted Pyramid Pattern

rows = int(input("Enter number of rows: "))

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

print(' ' * (rows - i) + ' '.join(str(x) for x in range(1, i + 1)))

9. Floyd's Triangle
rows = int(input("Enter number of rows: "))

num = 1

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

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

print(num, end=" ")

num += 1

print()

10. Reverse Words in a Sentence

sentence = input("Enter a sentence: ")

reversed_sentence = ' '.join(sentence.split()[::-1])

print("Reversed Sentence:", reversed_sentence)

11. Count Words in a Sentence

sentence = input("Enter a sentence: ")

word_count = len(sentence.split())

print("Number of words:", word_count)

12. Remove Punctuation from a String

import string

text = input("Enter a string: ")


cleaned_text = text.translate(str.maketrans('', '', string.punctuation))

print("String without punctuation:", cleaned_text)

13. Random Number Guessing Game

import random

number_to_guess = random.randint(1, 100)

attempts = 0

guess = 0

print("Guess the number between 1 and 100!")

while guess != number_to_guess:

guess = int(input("Enter your guess: "))

attempts += 1

if guess < number_to_guess:

print("Too low! Try again.")

elif guess > number_to_guess:

print("Too high! Try again.")

else:

print(f"Congratulations! You guessed it in {attempts} attempts.")

14. Sum of Digits of a Number

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

sum_of_digits = sum(int(digit) for digit in str(num))

print("Sum of digits:", sum_of_digits)


15. Find GCD of Two Numbers

def gcd(a, b):

while b:

a, b = b, a % b

return a

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

num2 = int(input("Enter second number: "))

print("GCD of", num1, "and", num2, "is", gcd(num1, num2))

You might also like