[go: up one dir, main page]

0% found this document useful (0 votes)
105 views20 pages

Cs Python Record

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

Python program to use Arithmetic Operators

num1 = int(input('Enter First number: '))


num2 = int(input('Enter Second number '))
add = num1 + num2
dif = num1 - num2
mul = num1 * num2
div = num1 / num2
floor_div = num1 // num2
power = num1 ** num2
modulus = num1 % num2
print('Sum of ',num1 ,'and' ,num2 ,'is :',add)
print('Difference of ',num1 ,'and' ,num2 ,'is :',dif)
print('Product of' ,num1 ,'and' ,num2 ,'is :',mul)
print('Division of ',num1 ,'and' ,num2 ,'is :',div)
print('Floor Division of ',num1 ,'and' ,num2 ,'is :',floor_div)
print('Exponent of ',num1 ,'and' ,num2 ,'is :',power)
print('Modulus of ',num1 ,'and' ,num2 ,'is :',modulus)
Python program to find distance between two points

print("Enter coordinates for point 1: ")


x1 = int(input("x1 = "))
y1 = int(input("y1 = "))
print("Enter coordinates for point 2: ")
x2 = int(input("x2 = "))
y2 = int(input("y2 = "))
distance = (((x2 - x1)**2) + ((y2 - y1)**2))**0.5
print("The distance between the two points is", distance)
Python Program to create a Student Mark sheet

marks=int(input("Enter the marks:"))


if marks>85 and marks<=100:
print("Congrats ! you scored grade A...")
elif marks>60 and marks<=85:
print("You scored grade B++...")
elif marks>40 and marks<=60:
print("you scored grade B...")
elif marks>30 and marks<=40:
print("you scored grade c...")
else:
print("Sorry you are fail")
Python program for fruitful function
pi=3.14
def areaOfCircle(r):
return pi*r*r

r=int(input("Enter radius of circle:"))


area=areaOfCircle(r)
print("Area of cicle is:",area)

Output
Python program for void function with different function arguments
def add_numbers(num1=5, num2=6):
sum = num1 + num2
print("Sum: ",sum)

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


b=int(input("Enter value for b:"))
add_numbers(a,b)
add_numbers(num2=10)
add_numbers()

Output
Python Program to create and import a user defined module

sum.py
def add(a, b):
result = a + b
return result

import_example.py
import sum
a=int(input("Enter a value for a:"))
b=int(input("Enter a Value for b:"))
print(sum.add(a,b))

Output
Python Program for checking a number is divisible by another using Boolean function

def isDivisible(x, y):


if x % y == 0:
result = True
else:
result = False
return result

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


b=int(input("Enter the value for b:"))
print(isDivisible(a, b))

Output
Python Program for Recursive function

def factorial(n):
if n == 1:
return n
else:
return n * factorial(n-1)

num=int(input("Enter the number for factorial:"))


if num < 0:
print("Invalid input ! Please enter a positive number.")
elif num == 0:
print("Factorial of number 0 is 1")
else:
print("Factorial of number", num, "=", factorial(num))

Output
Python Program for Leap of faith

def recursive_fibonacci(n):
if n <= 1:
return n
else:
return(recursive_fibonacci(n-1) + recursive_fibonacci(n-2))

n_terms = int(input("Enter the number for fibonacci:"))


if n_terms <= 0:
print("Invalid input ! Please input a positive value")
else:
print("Fibonacci series:")
for i in range(n_terms):
print(recursive_fibonacci(i))

Output
Python Program to check a number is palindrome or not using while loop

n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")

Output
Python program for searching the character position in a string

def find(word, letter):


index = 0
while index < len(word):
if word[index] == letter:
return index
index = index + 1
return -1

word = input("Enter a word:")


search=input("Enter a letter to search:")
c=find(word, search)
if c==-1:
print("Letter is not found in search")
else:
print("The index of search letter",search, "is in",c, "position")

Output
Python program for search a String and count

word = input("Enter a word:")


search=input("Enter a letter to find and count:")
count = 0
for letter in word:
if letter == search:
count = count + 1

print(count)
if count==0:
print ("Searching letter is not in the word"

Output
Python program to display multiplication table using for loop

num1 = int(input("Enter a number for multiplication: "))


num2 = int(input("Enter a range for muliplication: "))
for i in range(1,num2+1):
mulitply = num1 * i
print(f"{num1} X {i} = {mulitply}")

Output
Python program to create, append and remove lists in python

my_list = ["zero", "one", "two", "three"]


print("Elements of the list, my_list are:")
for ml in my_list:
print(ml);
my_new_list = ["four", "five"]
my_list += my_new_list
print("\nList's items after concatenating:")
for l in my_list:
print(l);
index = int(input("\nEnter index no:"))
print("Deleting the element present at index number",index)
del my_list[index]
print("\nNow elements of the list, my_list are:")
for ml in my_list:
print(ml);

Output
Python program to print the sub-list of a given list to the specified range

r=int(input("Enter the size of the list:"))


m=[]
for i in range(r):
l=int(input("Enter the elements in the list:"))
k=[l]
m.extend(k)
print("\nThe input List is:",m)
q1=int(input("\nEnter the starting index of the sublist:"))
q2=int(input("Enter the ending index of the sublist:"))
sub=m[q1:q2]
print("\nThe Sub List at specified index is:",sub)

Output
Python program to demonstrate work with tuples

T = ("apple", "banana", "cherry","mango","grape","orange")


print("\n Created tuple is :",T)
print("\n Second fruit is :",T[1])
print("\n From 3-6 fruits are :",T[3:6])
print("\n List of all items in Tuple :")
for x in T:
print(x)
if "apple" in T:
print("\n Yes, 'apple' is in the fruits tuple")
print("\n Length of Tuple is :",len(T))

Output
Python Program to read and display the elements of a Dictionary

d={}
n=int(input("Enter the length:"))
for i in range(n):
k=input("Enter the Key:")
v=input("Enter the Value:")
x={k:v}
d.update(x)
print("The Dictionary:",d)

Output
Python program to compute the number of characters, words and lines in a file

file=open("sample.txt","r")
number_of_lines=0
number_of_words=0
number_of_characters=0
for line in file:
line=line.strip("\n")
words=line.split()
number_of_lines+=1
number_of_words+=len(words)
number_of_characters+=len(line)
file.close()
print("lines:", number_of_lines, "words:", number_of_words, "characters:",
number_of_characters)

sample.txt
The New College
Department of Computer Science
Chennai

Output
Python program to perform read and write operations on a file

str =input("Enter the data into a file : ")


f1=open('abc.txt','w')
f1.write(str)
f1.close()
f2=open('abc.txt','r')
data=f2.read()
print(data)
f2.close()

Output
Python program for Exception Handling

def exception():
x=int(input("\nEnter the number1:"))
y=int(input("Enter the number2:"))
try:
z=x/y
except ZeroDivisionError:
print("Number2 Value should not be zero")
exception()
else:
print("The result is:",z)

exception()

Output

You might also like