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

Python Programming Basics: 21 Practices

Python to study

Uploaded by

sandeshnakhate0
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)
13 views22 pages

Python Programming Basics: 21 Practices

Python to study

Uploaded by

sandeshnakhate0
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

Practical No:- 1

Aim:- Write a program in python to print Hello World.


Code:-
print("hello world")

Output:-
Practical No:- 2
Aim:- Write a Python program to find and calculate the square root of a number.

Code 1:-
num = float(input('Enter a number:'))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f' % (num, num_sqrt))

Output:-

Aim:- using pow() function


Code 2:-
import math
num = float(input("Enter a number: "))
sqRoot = [Link](num, 0.5)
print("The square root of a given number {0} = {1}".format(num, sqRoot))

Output:-
Practical No:- 3
Aim:- Write a program in Python to swap two variables.
Code:-
x = input('Enter value of x: ')
y = input('Enter value of y: ')
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:-
Practical No:- 4
Aim:- Write a program in Python is a number positive, Negative, or Zero
Code:-
num = float(input("Input a number: "))
if num > 0:
print("It is a positive number")
elif num == 0:
print("It is zero")
else:
print("It is a negative number")
Output:-
Practical No:- 5
Aim:- Write a program in Python if a number is Odd or Even.
Code:-
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))

Output:-
Practical No:- 6
Aim:- Write a program in Python to find the Largest among three numbers.
Code:-
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)

Output:-
Practical No:- 7
Aim:- Write a program in Python to check whether the number is Prime or not.
Code:-
num = int(input("Enter a number: "))
flag = 0
for i in range(2, num):
if num % i == 0:
flag = 1
break
if flag == 1:
print('Not Prime')
else:
print('Prime')
Output:-
Practical No:- 8
Aim:- Write a program in Python to display the Multiplication of numbers.
Code:-
num = int(input("Display multiplication table of? ")
for i in range(1, 11):
print(num, 'x', i, '=', num*i)

Output:-
Practical No:- 9
Aim:- Write a program in Python to print the Fibonacci series.
Code:-
n_terms = int(input("How many terms do you want to print:"))
n_1 = 0
n_2 = 1
count = 0
if n_terms <= 0:
print("Please enter a positive integer. The given number is not valid.")
elif n_terms == 1:
print(f"The Fibonacci sequence up to {n_terms} term(s):")
print(n_1)
else:
print("The Fibonacci sequence up to", n_terms, "terms:")
while count < n_terms:
print(n_1, end=' ')
nth = n_1 + n_2
n_1 = n_2
n_2 = nth
count += 1

Output:-
Practical No:- 10
Aim:- Write a program in Python to check whether the number is Armstrong or
Not.
Code:-
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")

Output:-
Practical No:- 11
Aim:- Write a program in Python to find the Sum of the Natural number.
Code:-
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
sum = 0
while num > 0:
sum += num
num -= 1
print("The sum is", sum)

Output:-
Practical No:- 12
Aim:- Write a program in Python to find the number is divisible by another
number.
Code:-
print("Enter a Number (Numerator): ")
numn = int(input())
print("Enter a Number (Denominator): ")
numd = int(input())
if numn % numd == 0:
print("\n{} is divisible by {}".format(numn, numd))
else:
print("\n{} is not divisible by {}".format(numn, numd))

Output:-
Practical No:- 13
Aim:- Write a program in Python to convert Decimal number to Binary number,
Octal number.
Code:-
def decimal_to_binary(decimal):
binary = bin(decimal).replace("0b", "")
return binary
def decimal_to_octal(decimal):
octal = oct(decimal).replace("0o", "")
return octal
def decimal_to_hexadecimal(decimal):
hexadecimal = hex(decimal).replace("0x", "")
return hexadecimal
if __name__ == "__main__":
decimal = int(input("Enter a decimal number: "))
binary = decimal_to_binary(decimal)
octal = decimal_to_octal(decimal)
hexadecimal = decimal_to_hexadecimal(decimal)
print("Binary: " + binary)
print("Octal: " + octal)
print("Hexadecimal: " + hexadecimal)

Output:-
Practical No:- 14
Aim:- write a program in a pytthon to find ASCII value of character.
Code:-
character = input("Enter a character: ")
ascii_value = ord(character)
print("The ASCII value of", character, "is", ascii_value)

Output:-
Practical No:- 15
Aim:- Write a program in Python to Add two Matrices.
Code:-
rows = int(input("Enter the Number of rows: "))
columns = int(input("Enter the Number of Columns: "))
print("Enter the elements of First Matrix:")
matrix_a = [[int(input()) for _ in range(columns)] for _ in range(rows)]
print("First Matrix is:")
for row in matrix_a:
print(row)
print("Enter the elements of Second Matrix:")
matrix_b = [[int(input()) for _ in range(columns)] for _ in range(rows)]
print("Second Matrix is:")
for row in matrix_b:
print(row)
result = [[0 for _ in range(columns)] for _ in range(rows)]
for i in range(rows):
for j in range(columns):
result[i][j] = matrix_a[i][j] + matrix_b[i][j]
print("The Sum of Above two Matrices is:")
for row in result:
print(row)
Output:-
Practical No:- 16
Aim:- Write a program in Python to find whether the string is Palindrome or Not.
Code:-
def is_palindrome(s):
if len(s) < 1:
return True
else:
if s[0] == s[-1]:
return is_palindrome(s[1:-1])
else:
return False
a = str(input("Enter string: "))
if is_palindrome(a):
print("String is a palindrome!")
else:
print("String isn't a palindrome!")

Output:-
Practical No:- 17
Aim:- Write a program in Python to sort words in Alphabetic order.
Code:-
my_str = input("Enter a string: ")
words = my_str.split()
[Link]()
for word in words:
print(word)

Output:-
Practical No:- 18
Aim:- Write a program in Python to Reverse a String in Python by Using a Loop.
Code:-
def reverse(s):
str = ""
for char in s:
str = char + str
return str
s = "PrepBytes"
print("The original string is: ", end="")
print(s)
print("The reversed string after using loops is: ", end="")
print(reverse(s))

Output:-
Practical No:- 19
Aim:- Write a program in Python Programs to print number pattern.
Code 1:-
rows = int(input('Enter the number of rows:'))
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(i, end=' ')
print()

Output:-

code 2:-
rows = int(input('Enter the number of rows:'))
b=0
for i in range(rows, 0, -1):
b += 1
for j in range(1, i + 1):
print(b, end=' ')
print()

output:-
Practical No:- 20
Aim:- Write a program in Python Pascal’s triangle pattern using numbers.
Code:-
def print_pascal_triangle(size):
for i in range(size):
for j in range(i + 1):
print(decide_number(i, j), end=" ")
print()
def decide_number(n, k):
num = 1
if k > n - k:
k=n-k
for i in range(k):
num = num * (n - i)
num = num // (i + 1)
return num
rows = 7
print_pascal_triangle(rows)

Output:-
Practical No:- 21
Aim:- Write a program in Python Simple half pyramid pattern using “*”.
Code:-
rows = int(input('Enter the number of rows:'))
for i in range(rows):
for j in range(i + 1):
print("*", end=" ")
print()

Output:-

You might also like