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

Python Manual

The document contains a series of Python code snippets demonstrating various programming tasks, including checking Fibonacci numbers, solving quadratic equations, summing natural numbers, displaying multiplication tables, checking for prime numbers, implementing sequential search, creating a calculator, exploring string functions, implementing selection sort, managing a stack, and reading/writing files. Each snippet includes user input for interaction and outputs relevant results based on the computations performed. The examples serve as practical applications of fundamental programming concepts.

Uploaded by

pranav.hb.18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views22 pages

Python Manual

The document contains a series of Python code snippets demonstrating various programming tasks, including checking Fibonacci numbers, solving quadratic equations, summing natural numbers, displaying multiplication tables, checking for prime numbers, implementing sequential search, creating a calculator, exploring string functions, implementing selection sort, managing a stack, and reading/writing files. Each snippet includes user input for interaction and outputs relevant results based on the computations performed. The examples serve as practical applications of fundamental programming concepts.

Uploaded by

pranav.hb.18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

1.

outputs:
1. Check if a number belongs to the Fibonacci Sequence.

def checkFibonacci(n):
a=0
b=1
if (n==a or n==b):
return True
c = a+b;
while(c<=n):
if(c == n):
return True
a=b
b=c
c=a+b
return False

n = int(input("Enter a Number: "))


if(checkFibonacci(n)):
print(f"{n} belongs to Fibonacci sequence.")
else:
print(f"{n} doesn't belongs to Fibonacci sequence.")
2. outputs:
2. Solve quadratic equations. (ax2+bx+c)

#import complex math module


import cmath

a = int(input("Enter the value for 'a': "))


b = int(input("Enter the value for 'b': "))
c = int(input("Enter the value for 'c': "))

dis = (b * b) - (4 * a * c)

com1 = (-b - (cmath.sqrt(dis))) / (2 * a)


com2 = (-b + (cmath.sqrt(dis))) / (2 * a)

#convert complex number to real numbers


ans1 = com1.real
ans2 = com2.real

print(f'The roots are {ans1},{ans2}')


3. outputs:
3. Find the sum of n natural numbers.

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


sum=0
for i in range(1,n+1):
sum=sum+i
print(f"sum of {n} numbers is {sum}")
4. outputs:
4. Display Multiplication tables

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

for i in range(1, 11):


print(f"{num} x {i} = {num*i}")
5. outputs:
5. Check if a given number is prime number or not

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

# If given number is greater than 1


if num> 1:
# Iterate from 2 to n / 2
for i in range(2, int(num/2)+1):
# If num is divisible by any number between 2 and n / 2, it is not prime
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")
6. outputs:
6. Implement a sequential search

def Sequential_Search(ulist, item):


pos = 0
while pos<len(ulist):
if ulist[pos] == item:
return pos
else:
pos = pos + 1
return pos

ulist = []
n = int(input("Enter number of elements: "))
print("Enter the elements: ")
for i in range(0,n):
ulist.append((input()).lower())

item = (input("Enter the item to be found: ")).lower()

SearchPos = Sequential_Search(ulist, item)


if(SearchPos>= n):
print("Item not Found")
else:
print(f"Item found Position: {SearchPos+1}")
7. outputs:
7. Create a calculator program

def calculate(a, b, formula):


if formula == '+':
return a + b
elif formula == '-':
return a - b
elif formula == '*':
return a * b
elif formula == '/':
return a / b
else:
print('Invalid input!')
return 'Closing program...'

a = float(input("Enter first number: "))


b = float(input("Enter second number: "))
formula = input("Do you want to * , / , - or + : ")
answer = calculate(a, b, formula)
print(answer)
8. outputs:
8. Explore string functions

str = input("Enter the String: ")

#Converts the first character to upper case


print(f"capitalize() : {str.capitalize()}")

#Converts a string into upper case


print(f"upper() : {str.upper()}")

#Converts a string into lower case


strlow = str.lower()
print(f"lower() : {strlow}")

#Searches the string for a specified value and returns the position of where it
was found
str2 = (input("Enter the item to be found in specified string: ")).lower()
pos = strlow.find(str2)
if pos == -1:
print("item not found")
else:
print(pos)

#Returns True if all characters in the string are in the alphabet


print(f"isalpha() : {str.isalpha()}")

#Returns True if all characters in the string are digits


print(f"isdigit() : {str.isdigit()}")
9. outputs:
9. Implement selection sort

def selectionSort(data, size):


for step in range(size):
min_idx = step
for i in range(step + 1, size):
# to sort in descending order, change > to < in this line
if data[i] < data[min_idx]:
min_idx = i
# a short way to swap numbers
(data[step], data[min_idx]) = (data[min_idx], data[step])

data = []
size = int(input("Enter the size of array: "))
print("Enter the elemenst: ")
for i in range(0,size):
data.append(int(input()))
selectionSort(data, size)
print('Sorted Array in Ascending Order:')
print(data)
10. outputs:
10. Implement stack

stack = []
while True:
choice = int(input("1.Push\n2.Pop\n3.Display\n4.Exit\nEnter your choice: "))
if(choice == 1):
# append() function to push
stack.append(input("Enter item to push: "))
elif (choice == 2):
stack.pop()
elif (choice == 3):
print(stack)
else:
exit()
11. outputs:
11. Read and write into a file

#Specify File's path.


#if file doesn't exist in the specified path a new file will be created
myFile = open("D:/ test1.txt","w")
str = input("Enter a String to write into file: ")
myFile.write(str)
myFile.close()

myFile = open("D:/ test1.txt ","r")


print("File contains: ")
print(myFile.read())
myFile.close()

You might also like