0% found this document useful (0 votes)
7 views3 pages

Python Practical Programs

The document provides practical Python programs covering flow control, string manipulation, list, tuple, and dictionary operations. Each section includes examples demonstrating various concepts such as conditional statements, loops, string functions, and data structure manipulations. It serves as a comprehensive guide for beginners to understand and implement basic Python programming techniques.

Uploaded by

sagekey01
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)
7 views3 pages

Python Practical Programs

The document provides practical Python programs covering flow control, string manipulation, list, tuple, and dictionary operations. Each section includes examples demonstrating various concepts such as conditional statements, loops, string functions, and data structure manipulations. It serves as a comprehensive guide for beginners to understand and implement basic Python programming techniques.

Uploaded by

sagekey01
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

Python Practical Programs (Flow Control, String,

List, Tuple, Dictionary)

1. Flow of Control Programs


1. If statement
num = int(input("Enter a number: "))
if num > 0:
print("Number is positive")

2. If-else (Even or Odd)


num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")

3. If-elif-else (Largest of three numbers)


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b and a > c:
print("A is largest")
elif b > a and b > c:
print("B is largest")
else:
print("C is largest")

4. For loop (Print 1 to 10)


for i in range(1,11):
print(i)

5. While loop (Print 1 to 5)


i = 1
while i <= 5:
print(i)
i += 1

6. Break statement
for i in range(1,10):
if i == 5:
break
print(i)

2. String Manipulation Programs


1. Length of string
s = input("Enter a string: ")
print("Length:", len(s))

2. Uppercase and lowercase


s = input("Enter a string: ")
print("Uppercase:", [Link]())
print("Lowercase:", [Link]())

3. Palindrome check
s = input("Enter a string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not palindrome")

4. Count vowels
s = input("Enter a string: ")
count = 0
for ch in s:
if [Link]() in "aeiou":
count += 1
print("Number of vowels:", count)

5. Reverse string
s = input("Enter a string: ")
print("Reversed string:", s[::-1])

3. List Manipulation Programs


1. Largest element
numbers = [10,25,7,89,45]
print("Largest:", max(numbers))

2. Add element
fruits = ["apple","banana","mango"]
[Link]("orange")
print(fruits)

3. Remove element
numbers = [10,20,30,40,50]
[Link](30)
print(numbers)

4. Sum of list
numbers = [5,10,15,20]
print("Sum:", sum(numbers))

5. Sort list
numbers = [45,12,78,3,25]
[Link]()
print(numbers)

4. Tuple Programs
1. Create tuple
t = (10,20,30,40,50)
print(t)

2. Access elements
t = ("apple","banana","mango")
print(t[0])
print(t[1])
print(t[2])

3. Length of tuple
t = (5,10,15,20,25)
print(len(t))

4. Count occurrences
t = (1,2,3,2,4,2,5)
print([Link](2))

5. Max and Min


t = (12,45,7,89,23)
print("Max:", max(t))
print("Min:", min(t))

5. Dictionary Programs
1. Create dictionary
student = {"name":"Rahul","age":20,"course":"BSc"}
print(student)

2. Access values
student = {"name":"Rahul","age":20}
print(student["name"])
3. Add key-value
student = {"name":"Rahul","age":20}
student["course"] = "BSc"
print(student)

4. Update value
student = {"name":"Rahul","age":20}
student["age"] = 21
print(student)

5. Delete element
student = {"name":"Rahul","age":20,"course":"BSc"}
del student["age"]
print(student)

6. Length of dictionary
student = {"name":"Rahul","age":20,"course":"BSc"}
print(len(student))

7. Print keys and values


student = {"name":"Rahul","age":20,"course":"BSc"}
for k,v in [Link]():
print(k,":",v)

8. Check key existence


student = {"name":"Rahul","age":20,"course":"BSc"}
if "age" in student:
print("Key exists")
else:
print("Key does not exist")

You might also like