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

ML Python Lab Programs

The document contains various Python programs demonstrating basic programming concepts such as printing messages, performing arithmetic operations, swapping variables, reversing numbers and strings, calculating factorials, checking for odd/even numbers, identifying Armstrong numbers, generating Fibonacci sequences, and sorting lists. Each program includes code snippets along with their expected outputs. Additionally, it provides examples of user input handling and basic string manipulation.

Uploaded by

yaary947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

ML Python Lab Programs

The document contains various Python programs demonstrating basic programming concepts such as printing messages, performing arithmetic operations, swapping variables, reversing numbers and strings, calculating factorials, checking for odd/even numbers, identifying Armstrong numbers, generating Fibonacci sequences, and sorting lists. Each program includes code snippets along with their expected outputs. Additionally, it provides examples of user input handling and basic string manipulation.

Uploaded by

yaary947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

# This program prints Hello, world!

print('Hello, world!')

Output

Hello, world!

# This program adds two numbers

num1 = 1.5
num2 = 6.3

# Add two numbers


sum = num1 + num2

# Display the sum


print('The sum of {0} and {1} is
{2}'.format(num1, num2, sum))

Output

The sum of 1.5 and 6.3 is 7.8

# Python program to swap two variables

x = 5
y = 10
# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')

# create a temporary variable and swap the


values
temp = x
x = y
y = temp

print('The value of x after swapping:


{}'.format(x))
print('The value of y after swapping:
{}'.format(y))

Output

The value of x after swapping: 10


The value of y after swapping: 5

Example 1: Reverse a Number using a while loop


num = 1234
reversed_num = 0

while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

print("Reversed Number: " +


str(reversed_num))
Reversed Number: 4321

Factorial of a Number using Loop


# Python program to find the factorial of a
number provided by the user.

# change the value for a different result


num = 7

# To take input from the user


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

factorial = 1

# check if the number is negative, positive


or zero
if num < 0:
print("Sorry, 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 7 is 5040

# Python program to check if the input number


is odd or even.
# A number is even if division by 2 gives a
remainder of 0.
# If the remainder is 1, it is an odd number.

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


if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))

Output 1

Enter a number: 43
43 is Odd

Output 2

Enter a number: 18
18 is Even
# Python program to check if the number is an
Armstrong number or not

# take input from the user


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

# initialize sum
sum = 0

# find the sum of the cube of each digit


temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

# display the result


if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an


Armstrong number.

Output 1
Enter a number: 663
663 is not an Armstrong number

Output 2

Enter a number: 407


407 is an Armstrong number

# Sum of natural numbers up to num

num = 16

if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)

Output

The sum is 136


n*(n+1)/2

Write a program to Reverse a string


seq_string = 'Python'
# reverse of a string
print(list(reversed(seq_string)))
['n', 'o', 'h', 't', 'y', 'P']
Check whether a string is palindrome
# Program to check if a string is palindrome
or not

my_str = 'aIbohPhoBiA'

# make it suitable for caseless comparison


my_str = my_str.casefold()

# reverse the string


rev_str = reversed(my_str)

# check if the string is equal to its reverse


if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

Output

The string is a palindrome.

Program to print prime numbers from 1 to 100


lower = 900
upper = 1000
print("Prime numbers between", lower, "and",
upper, "are:")

for num in range(lower, upper + 1):


# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)

Output

Prime numbers between 900 and 1000 are:


907
911
919
929
937
941
947
953
967
971
977
983
991
997

Write a program to find Fibonacci sequence


# Program to display the Fibonacci sequence
up to n-th term

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence
upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Output

How many terms? 7


Fibonacci sequence:
0
1
1
2
3
5
8

Calculate the number of vowels and consonants in a string


# Program to count the number of each vowels

# string of vowels
vowels = 'aeiou'

ip_str = 'Hello, have you tried our tutorial


section yet?'

# make it suitable for caseless comparisions


ip_str = ip_str.casefold()

# make a dictionary with each vowel a key and


value 0
count = {}.fromkeys(vowels,0)
# count the vowels
for char in ip_str:
if char in count:
count[char] += 1

print(count)

Output

{'o': 5, 'i': 3, 'a': 2, 'e': 5, 'u': 3}

prime_numbers = [11, 3, 7, 5, 2]

# sorting the list in ascending order


prime_numbers.sort()

print(prime_numbers)

# Output: [2, 3, 5, 7, 11]

umbers = [4, 2, 12, 8]

sorted_numbers = sorted(numbers)

print(sorted_numbers)

# Output: [2, 4, 8, 12]


Determine the second largest element of an array
Numbers =[10,5,6,54,36]
Numbers.sort()
Print(numbers[-2]

Q) WRITE A PROGRAM TO PRONINT


MULTIPLICATION TABLE IN REVERSE
ORDER.

num= int( input(' enter a number :'))


for i in range (10,1,-1)
print( nunum,'x',i,'=',num*i)

output:
2*10=20 2*5=10
2*9=18 2*4=8
2*8=16 2*3=6
2*7=14 2*2=4
2*6=12

You might also like