0% found this document useful (0 votes)
2 views5 pages

Essential Python Programs and Examples

The document contains various Python programs demonstrating fundamental programming concepts, including checking for prime numbers, palindromes, calculating factorials, string manipulation, file operations, and basic data structures like lists, tuples, and dictionaries. It also provides steps to create and run a Django project. Each program is accompanied by code snippets and explanations of their functionality.

Uploaded by

26sidayan
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)
2 views5 pages

Essential Python Programs and Examples

The document contains various Python programs demonstrating fundamental programming concepts, including checking for prime numbers, palindromes, calculating factorials, string manipulation, file operations, and basic data structures like lists, tuples, and dictionaries. It also provides steps to create and run a Django project. Each program is accompanied by code snippets and explanations of their functionality.

Uploaded by

26sidayan
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 PROGRAMS

 Python Program to Check Whether a Number is Prime or


Not

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

if num <= 1:
print("Not a Prime Number")
else:
for i in range(2, num):
if num % i == 0:
print("Not a Prime Number")
break
else:
print("Prime Number")

 Python Program to Check Palindrome Number

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


temp = num
rev = 0

while num > 0:


digit = num % 10
rev = rev * 10 + digit
num = num // 10

if temp == rev:
print("Palindrome Number")
else:
print("Not a Palindrome Number")

 Python Program to Print Factorial of a Number

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


fact = 1

if num < 0:
print("Factorial not defined for negative numbers")
else:
for i in range(1, num + 1):
fact = fact * i
print("Factorial =", fact)

1. Python Program to Find Factorial Using Function

def factorial(n):
fact = 1
for i in range(1, n + 1):
fact = fact * i
return fact

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

if num < 0:
print("Factorial not defined for negative numbers")
else:
result = factorial(num)
print("Factorial =", result)

2. Python Program to Print Multiplication Table Using


Function

def table(n):
for i in range(1, 11):
print(n, "x", i, "=", n * i)

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


table(num)

3. Python Program to Find Length of a String

string = input("Enter a string: ")


count = 0

for ch in string:
count = count + 1

print("Length of the string =", count)

4. Python Program to Reverse a String

string = input("Enter a string: ")


rev = ""

for ch in string:
rev = ch + rev

print("Reversed string =", rev)

5. Python Program to Read a File and Display Its Content

file = open("[Link]", "r")


content = [Link]()
print(content)
[Link]()

6. Python Program to Count Number of Words in a File

file = open("[Link]", "r")


content = [Link]()
words = [Link]()
count = len(words)
print("Number of words =", count)
[Link]()

7. Python Program to Write Data to a File and Read It Back


file = open("[Link]", "w")
[Link]("Python programming is easy")
[Link]()

file = open("[Link]", "r")


content = [Link]()
print(content)
[Link]()

8. Write the steps to create and run a Django project.

Install Python
Install Python on the system since Django is a Python-based web
framework.

Install Django
Django is installed using pip command:
pip install django
Create a Django Project
A new Django project is created using the command:
django-admin startproject projectname

Move to Project Directory


Change the current directory to the project folder using:
cd projectname

Run the Development Server


Start the Django development server using the command:
python [Link] runserver

9. Python Program to Print Elements of a List Using Loop

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

for item in list1:

print(item)

10. Python Program to Demonstrate List Slicing

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

print("Original list:", list1)

print("Elements from index 2 to 5:", list1[2:5])

11. Python Program to Print Elements of Tuple Using For Loop

tuple1 = (5, 10, 15, 20)

for item in tuple1:

print(item)

12. Python Program to Demonstrate Dictionary Operations

student = {

"name": "Rahul",

"roll_no": 101,
"marks": 85

# Access value

print("Name:", student["name"])

# Update value

student["marks"] = 90

# Add new key-value pair

student["grade"] = "A"

# Delete a key

del student["roll_no"]

print("Updated Dictionary:", student)

You might also like