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

Assignment1 (01fe17bar039) PDF

This document contains 12 Python programs submitted by a student. The programs include: 1) Adding two numbers, 2) A basic calculator, 3) Checking if a string is a palindrome, 4) Converting decimal to binary, 5) Calculating factorials, 6) Printing the Fibonacci sequence, 7) Displaying a multiplication table, 8) Finding the largest of three numbers, 9) Finding the smallest element in a list, 10) Splitting and joining a string, 11) Implementing selection sort, and 12) Implementing bubble sort. Each program is followed by sample output.

Uploaded by

Pooja Maath
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)
89 views3 pages

Assignment1 (01fe17bar039) PDF

This document contains 12 Python programs submitted by a student. The programs include: 1) Adding two numbers, 2) A basic calculator, 3) Checking if a string is a palindrome, 4) Converting decimal to binary, 5) Calculating factorials, 6) Printing the Fibonacci sequence, 7) Displaying a multiplication table, 8) Finding the largest of three numbers, 9) Finding the smallest element in a list, 10) Splitting and joining a string, 11) Implementing selection sort, and 12) Implementing bubble sort. Each program is followed by sample output.

Uploaded by

Pooja Maath
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

PYTHON ASSIGNMENT 1

SUBMITTED BY-POOJA C MATH(USN-01FE17BAR039)

1.program to add two numbers


num1=3
num2=4
sum=num1+num2
print(sum)
OUTPUT- 7

2. to make a simple calculator


print("Please the select operation -\n"
"1. Addtion\n" \
"2. Subtraction\n" \
"3. Multiplication\n" \
"4. Division\n")
select = input("Select any operations from 1, 2, 3, 4 :")

number_1 = int(input("Enter first number: "))


number_2 = int(input("Enter second number: "))

if select == '1':
print (number_1 + number_2)

elif select == '2':


print (number_1 - number_2)

elif select == '3':


print(number_1*number_2)

elif select == '4':


print(number_1 / number_2)
else:
print("Invalid input")
OUTPUT:
Please the select operation -
1. Addtion
2. Subtraction
3. Multiplication
4. Division

Select any operations from 1, 2, 3, 4 :2


Enter first number: 2
Enter second number: 1
1

3. To check whether string is palindrome or not


string=input("Enter string:")
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")
OUTPUT-
Enter string:malayalam
The string is a palindrome

4.To convert decimal number into binary


number = int(input("Enter any decimal number: "))
print("Equivalent Binary Number: ", bin(number))
OUTPUT-Enter an integer: 87 binary-1010111
5.to find factorial of number
num = 9
factorial = 1
if num < 0:
print(" factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
OUTPUT-
The factorial of 9 is 362880

6.to print Fibonacci


n = 10
a = 0
b = 1

if n == 0: a = 0
elif n == 1: b = 0
else:
for i in range(2,n):
c = a + b
a = b
b = c
print (b)
OUTPUT-
1
2
3
5
8
13
21
34

7.to display the multiplication table


num = 12
for i in range(1, 11):
print(num,'x',i,'=',num*i)
OUTPUT-
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120

8. to find largest number among three

num1 = 10
num2 = 14
num3 = 12
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number between",num1,",",num2,"and",num3,"is",largest)
OUTPUT- The largest number between 10 , 14 and 12 is 14

9.to find smallest in the list


lst = [3,4,2,5,4,6,7,8,1,]

lst.sort()
print(lst[0])
OUTPUT-
Smallest element is: 1

10.to split and join the string


string = "hello world hello”

string1 = string.split(" ")


string2 = "-".join(string1)

print(string2)

OUTPUT- hello-class-hello

11.for selection sort


lst = [3,4,2,3,5,2,57,6,4,57,5] # Sample list

for x in range(len(lst)-1):
a = x
for y in range(x,len(lst)):
if lst[y] < lst[a]:
a = y

temp = lst[x]
lst[x] = lst[a]
lst[a] = temp

print(lst)

OUTPUT-
Sorted array-[2, 2, 3, 3, 4, 4, 5, 5, 6, 57, 57]

12.for bubble sort


lst = [3,4,2,3,5,2,57,6,4,57,5] # Sample list

for x in range (len(lst)-1,0,-1): # for range of list to go reverse


for y in range(x): # for comparing
if lst[y] > lst[y+1]: # swapping
temp = lst[y]
lst[y] = lst[y+1]
lst[y+1] = temp

print(lst)

OUTPUT-
Sorted array is: [2, 2, 3, 3, 4, 4, 5, 5, 6, 57, 57]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy