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

Python Basic Intermediate Programs

Uploaded by

sneha2332122
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)
15 views3 pages

Python Basic Intermediate Programs

Uploaded by

sneha2332122
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
You are on page 1/ 3

Basic and Intermediate Python Programs

Hello World

print("Hello, World!")

Basic Arithmetic

a = 10

b = 20

print("Sum:", a + b)

print("Difference:", a - b)

print("Product:", a * b)

If-else Condition

num = 10

if num % 2 == 0:

print("Even")

else:

print("Odd")

For Loop

for i in range(5):

print("Number:", i)

Factorial using Recursion

def factorial(n):

if n == 0:

return 1

return n * factorial(n-1)

print("Factorial of 5:", factorial(5))

Page 1
Basic and Intermediate Python Programs

List Operations

my_list = [1, 2, 3]

my_list.append(4)

print("List after appending:", my_list)

String Manipulation

text = "Hello, World!"

print("Upper:", text.upper())

print("Lower:", text.lower())

File Handling

with open("sample.txt", "w") as file:

file.write("This is a sample text.")

with open("sample.txt", "r") as file:

print(file.read())

Bubble Sort Algorithm

def bubble_sort(arr):

n = len(arr)

for i in range(n):

for j in range(0, n-i-1):

if arr[j] > arr[j+1]:

arr[j], arr[j+1] = arr[j+1], arr[j]

return arr

print("Sorted:", bubble_sort([64, 34, 25, 12, 22, 11, 90]))

Class and Object

Page 2
Basic and Intermediate Python Programs

class Dog:

def __init__(self, name):

self.name = name

def bark(self):

print(self.name + " says Woof!")

d = Dog("Buddy")

d.bark()

Page 3

You might also like