Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
88 views

Python basic codes

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Python basic codes

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

1. Write a program to reverse an integer in Python.

Output:
Please enter a number: 5642
Before reverse: 5642
After reverse: 2456

2. Write a program in Python to check whether an integer is Armstrong number or not

output:
Please enter a number: 245
The number is not an Armstrong number
3. Write a program in Python to check given number is prime or not

Output:
Please enter a number: 7
The given number is a prime number

4.Write a program in Python to print the Fibonacci series using iterative method

Output:
Please enter a number to generate the Fibonacci series: 7
The Fibonacci series is:
0
1
1
2
3
5
8

5. Write a program in Python to print the Fibonacci series using recursive method

Output:
Please enter a number to generate the Fibonacci series: 5
The Fibonacci series is:
0
1
1
2
3

6. Write a program in Python to check whether a number is palindrome or not using


iterative method
Output:
If you entered 121 as the input, the code would produce the following output:
Please enter a number: 121
The number is a palindrome.

7. Write a program in Python to check whether a number is palindrome or not using


recursive method

Output: If you enter 535 as the input, the output will be:
Please enter a number: 535
The number is a palindrome

8. Write a program in Python to find greatest among three integers


Output:
If you enter 20, 30, and 10 as the inputs for n1, n2, and n3 respectively, the output will be:
Please enter the first number: 20
Please enter the second number: 30
Please enter the third number: 10
n2 is the greatest

9. Write a program in Python to check if a number is binary

Output:
Please enter a number:12345
num is not binary

10. Write a program in Python to find sum of digits of a number using recursion
Output:
If you enter 10 as the input, the output would be:
Enter a number: 10
Sum of digits of the number is: 1

11. Write a program in Python to swap two numbers without using third variable

Output:
If the input is a=5 and b=7,
After swapping
value of a is : 7
value of b is : 5

12. Write a program in Python to swap two numbers using third variable

Output:
If the input is “10”, “20”, the output will be:
After swapping
value of a is : 20 value of b is : 10
13. Write a program in Python to find prime factors of a given integer

Output:
Enter a number: 20
The prime factors of 20 are: [2, 2, 5]

14. Write a program in Python to add two integer without using arithmetic operator

Output:
Please give first number: 20
Please give second number: 10
Sum = 30;
15. Write a program in Python to find given number is perfect or not?
What is Perfect Number

Output:
please give first number a: 6
given no. is perfect number
please give first number a: 8
given no. is not a perfect number

16. Python Program to find the Average of numbers with explanations

Output:
Enter the number of elements you want in array: 3
Please give value for index 0:10
Please give value for index 1:20
Please give value for index 2:30
Average of the array elements is 20.0
17. Python Program to calculate factorial using iterative method

Output:
Enter a number: 5
The factorial of 5 is 120

18. Python Program to calculate factorial using recursion

Output:
Enter the number: 6
The factorial of 6 is 720

19.Python Program to check a given number is even or odd


Output:
Enter a number: 5
The number is Odd

20. Python program to print first n Prime Number with explanation

Output:
Enter the value of n: 5
First 5 prime numbers are: [2, 3, 5, 7, 11]
21. Python Program to print Prime Number in a given range

22. Python Program to find Smallest number among three

Example Output:
Enter the first number: 25
Enter the second number: 12
Enter the third number: 36
The smallest number is 12

23. Python program to calculate the power using the POW method

Output:
Enter the base number: 2
Enter the exponent: 3
The result of 2 raised to the power of 3 is 8

24. Python Program to calculate the power without using POW function.(using for loop)

Output:
Enter the value for base : 5
Enter the value for exponent : 4
The result of 5 raised to the power of 4 is 625
25. Python Program to calculate the power without using POW function.(using while loop)

Output:
Enter the value for base : 5
Enter the value for exponent : 4
5 to power of 4 = 625

26. Python Program to calculate the square of a given number

Output:

27. Python Program to calculate the cube of a given number

Output:
28. Python Program to calculate the square root of a given number

Output 1:

29. Python program to calculate LCM of given two numbers

Output 1:
30. Python Program to find GCD or HCF of two numbers

Output :

31. Python Program to find GCD of two numbers using recursion

Output :
32. Python Program to Convert Decimal Number into Binary

Output
100011

33.Python Program to convert Decimal number to Octal number

Output:
41
34. Python Program to check the given year is a leap year or not

Output:
Enter the number: 1700
Given year is not a leap Year

35. Python Program to convert Celsius to Fahrenheit

Output:
Temperature value in degree Celsius: 34
The 34.00 degree Celsius is equal to: 93.20 Fahrenheit
----OR----
Temperature value in degree Celsius: 23
The 23.00 degree Celsius is equal to: 73.40 Fahrenheit

36. Python Program to convert Fahrenheit to Celsius

Output 1:

37. Python program to calculate Simple Interest with explanation

Output :
1. Python program to remove given character from String

Output:

Another method:

2. Python Program to count occurrence of a given characters in string


Output:

3. Python Program to check if two Strings are Anagram

Output:

4. Python program to check a String is palindrome or not


Output:

5.Python program to check given character is vowel or consonant

Output :

6. Python program to check given character is digit or not

Output :
7.Python program to check given character is digit or not using isdigit() method

Output :

8. Python program to replace the string space with a given character

Output:

end-of-the-day
Python program to print the highest frequency character in a String

Output:

Original String: end of the day

Character with highest frequency: 'd'

Python program to count alphabets, digits and special characters


Output:

Python program to check given character is digit or not using isdigit() method

Output :

Python program to replace the string space with a given character

Output:

end-of-the-day
Python program to replace the string space with a given character using replace()
method

Output:

end-of-the-day

Python program to convert lowercase vowel to uppercase in string

Output:

Original String: end of the day

String with Uppercase Vowels: End Of thE dAy


Python program to print the highest frequency character in a String

Output:

Original String: end of the day

Character with highest frequency: 'd'

Python program to Replace First Occurrence Of Vowel With ‘-‘ in String

output:

H-llo World!
Python program to separate characters in a given string

output:

['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!']

Python program to concatenate two strings using join() method

Output:

Python program to remove repeated character from string


output:

example string

Python program to calculate sum of integers in string

Output :

Python program to print all non repeating character in string

Output:
Python Program to sort characters of string in ascending order

Output 1:

Write a program in Python for, In array 1-100 numbers are stored, one number is
missing how do you find it

Write a program in Python for, In a array 1-100 multiple numbers are duplicates, how
do you find it
Write a program in Python for, How to find all pairs in array of integers whose sum is
equal to given number

Write a program in Python for, How to compare two array is equal in size or not

Write a program in Python to find largest and smallest number in array


Write a program in Python to find second highest number in an integer array

Write a program in Python to find top two maximum number in array?

Write a program in Python to remove duplicate elements form array


Output:

Python program to find top two maximum number in array

Python program to print array in reverse Order


Python program to reverse an Array in two ways

Python Program to calculate length of an array

Python program to insert an element at end of an Array


Python program to insert element at a given location in Array

Python Program to delete element at end of Array

Python Program to find sum of array elements

Python Program to print all even numbers in array


Python program to perform left rotation of array elements by two positions

Python program to perform right rotation in array by 2 positions

Python Program to merge two arrays

Python Program to find highest frequency element in array

You might also like