0% found this document useful (0 votes)
34 views

140 Basic to Advances Python Programs

The document contains 140+ Python programs ranging from basic to advanced levels. Each program includes a description, code snippets, and example outputs for various tasks such as arithmetic operations, variable swapping, temperature conversions, and more. It serves as a comprehensive resource for learning Python programming through practical examples.

Uploaded by

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

140 Basic to Advances Python Programs

The document contains 140+ Python programs ranging from basic to advanced levels. Each program includes a description, code snippets, and example outputs for various tasks such as arithmetic operations, variable swapping, temperature conversions, and more. It serves as a comprehensive resource for learning Python programming through practical examples.

Uploaded by

Abdou FILALI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
140+ Basic to Advanced Python Programs Program 1 Write a Python program to print "Hello Python". In [1]: 1 print("Hello Python") Hello Python Program 2 Write a Python program to do arithmetical operations addition and division. # Addition num = float(input("Enter the first number for addition: ")) In [2]: 2 2 3. num2 = float(input("Enter the second number for addition: *)) 4 5 sum_result = numi + num2 print(f"sum: {numa} + {numa} = {sum_result}") Enter the first number for addition: 5 Enter the second number for additio sum: 5.0 + 6.0 = 11.0 6 # Division hum3 = float(input("Enter the dividend for divisio numa. = float(input("Enter the divisor for division: if num4 == 0: print("Error: Division by zero is not allowed.") else: div_result = num3 / numa print(#"Division: {num3} / {num4} = {div_result}") In [3] eNVousune Enter the dividend for division: 25 Enter the divisor for division: 5 Division: 25.0 / 5.0 = 5.0 Program 3 Write a Python program to find the area of a triangle. In [4]: 1 # Input the base and height from the user 2 base = float(input("Enter the length of the base of the triangle: ")) 3 height = float(input("Enter the height of the triangle: ")) 4 # Calculate the area of the triangle 5 area = 0.5 * base * height 6 # Display the result 7 print(f"The area of the triangle is: {area}") Enter the height of the triangle: 15 The area of the triangle is: 75.0 In [5]: In [6]: In [7]: Program 4 Write a Python program to swap two variables. 1 # Input two variables 2 a= input("Enter the value of the first variable (a) 3 b = input("Enter the value of the second variable (b’ 4 # Display the original values 5 print(f"Original values: a = {a}, b = {b}") 6 # Swap the values using a temporary variable 7 temp =a Ba=b 9 b= temp 1e@ # Display the swapped values 11 print(#"Swapped values: a = {a}, b = {b}") Enter the value of the first variable (a): 5 Enter the value of the second variable (b): 9 Original values: a= 5, b= 9 Swapped values: a = 9, b = 5 Program 5 Write a Python program to generate a random number. 1 import random 2 print(f*Random number: {random.randint(1, 1¢0)}") Random number: 89 Program 6 Write a Python program to convert kilometers to miles. kilometers = float(input("Enter distance in kilometers # Conversion factor: 1 kilometer = 0.621371 miles conversion_factor = @.621371 miles = kilometers * conversion_factor eVaunune print(#"{kilometers} kilometers is equal to {miles} miles") Enter distance in kilometers: 100 18.0 kilometers is equal to 62.1371000@0000004 miles Program 7 Write a Python program to convert Celsius to Fahrenheit. In [8]: In [9] celsius = float(input("Enter temperature in Celsius: ")) # Conversion formula: Fahrenheit = (Celsius * 9/5) + 32 fahrenheit = (celsius * 9/5) + 32 Enter temperature in Celsius: 37 37.8 degrees Celsius is equal to 98.6 degrees Fahrenheit Program 8 Write a Python program to display calendar. 1 2 3 4 5 6 7 print(cal) import calendar Enter year: 2023 Enter mont 11 November 2023 Mo Tu We Th Fr A 2 678910 13 14.15 16 17 20 21 22 23 24 27 28 Program 9 sa Su 345 11 12 1a 19 25 26 29 30 year = int(input("Enter year: ")) month = int(input("Enter month: ")) cal = calendar.month(year, month) Write a Python program to solve quadratic equation. The standard form of a quadratic equation is: where a, band care real numbers and a#O ax2+ py +c=0 The solutions of this quadratic equation is given by: (-b + (b? ~ 4ac V2\/(2a) print(f"{celsius} degrees Celsius is equal to {fahrenheit} degrees Fahr 3195, In [10] In [12]: import math # Input coefficients a = float(input("Enter coefficient a: ")) b = float(input("Enter coefficient b: ")) ¢ = float(input("Enter coefficient ¢: ")) # Calculate the discriminant discriminant = b**2 - 4*atc # Check if the discriminant is positive, negative, or zero if discriminant > @: 1 2 3 4 5 6 7 8 9 10 Ft 2 e math.sqrt(discriminant)) / (2%a) — root2 a5 16 wv 18 19 20 # Two real and distinct roots root. = (-b + (be math.sqrt(discriminant)) / (2*a) print(f*Root {root1}") print(F"Root 2: {root2}") elif discriminant == @: # One real root (repeated) 2 root 7 (28a) print(#"Root: {root}") 22 else: 23 # Complex roots real_part = -b / (2*a) imaginary part 24 = math. sqrt(abs(discriminant)) / (2*a) print(f*Root 1: 25 {real_part} + {imaginary part}i") print(f"Root 2: 26 {real_part} - {imaginary_part}i") 27 28 Enter coefficient a: 1 Enter coefficient b: 4 Enter coefficient c: 8 Root 1: -2.0 + 2.01 Root 2: -2.0 - 2.0i Program 10 Write a Python program to swap two variables without temp variable. a=5b=10 # Swapping without a temporary variable a, b=b, a print("After swapping:") print("a print("b = BS cmvauaune After swapping: a=10 bes 4/95 Program 11 Write a Python Program to Check if a Number is Positive, Negative or Zero. In [22]: num = float(input ("Enter a number: ")) if num > @: print("Positive number") elif num == 0: print( "Zero else: print("Negative number”) No weune Enter a number: 6.4 Positive number Program 12 Write a Python Program to Check if a Number is Odd or Even. In [13]: 1 num = int(input("Enter a number: ")) 2 3if num%2 == 0: 4 print("This is a even number") 5 else: 6 print("This is a odd number") Enter a number: 3 This is a odd number Program 13 Write a Python Program to Check Leap Year. In [14]: 1 year = int(input("Enter a year: “)) 2 3. # divided by 100 means century year (ending with 00) # century year 4 divided by 400 is Leap year if (year % 400 == @) and (year % 108 == 0): 5 § _print("{0} is @ leap year". format(year)) 8 % not divided by 100 means not a century year # year divided by 4 ee ‘is a Leap year elif (year % 4 ==0) and (year % 10 != @): a 12 print("{@} is a leap year".format(year)) 3 ja # if not divided by both 4e@ (century year) and 4 (not century year) # year is not Leap year 15 else: 16 print("{®} is not a leap yea format (year)) Enter a year: 2024 2024 is a leap year In [15]: Program 14 Write a Python Program to Check Prime Number. Prime Numbers: ‘A prime number is a whole number that cannot be evenly divided by any other number except for 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers because they cannot be divided by any other positive integer except for 1 and their own value. 1 num = int(input("Enter a number: ")) 2 3# define a flag variable 4flag = False 5 6if num == 2 . print(f"{num}, is not a prime number") 9 elif num > 1: i # check for factors ie for i in range(2, num): a if (num % i) == 0: 13 ‘flag = True # if factor is found, set flag to True vA # break out of Loop 15. break 16 17 # check if flag is True 18 if flag: print(#"{num}, is not a prime number") 19 else: 20 print(#"{num}, is a prime number") Enter a number: 27 27, is not a prime number Program 15 Write a Python Program to Print all Prime Numbers in an Interval of 1-10. In [20]: In [22]: 1. # Python program to display all the prime numbers within an interval 2 3 lower = 1 upper = 10 4 5 print("Prime numbers between", lower, “and”, upper, 6 7 for num in range(lower, upper + 1): are 8 a # all prime numbers are greater than 1 12 af num > a: a for i in range(2, num): 1a if (num % i) == @: 14 break 45 else: print (num) Prime numbers between 1 and 1@ are: 2357 Program 16 Write a Python Program to Find the Factorial of a Number. num = int(input(“Enter a number: ")) factorial = 1 if num <0: print("Factirial does not exist for negative numbers") elif num == @: print("Factorial of @ is 1") else: for i in range(1, num+1): factorial = factorial*i print(f'The factorial of {num} is {factorial}') Bony ausune 1 Enter a number: 4 The factorial of 4 is 24 Program 17 Write a Python Program to Display the multiplication Table. In [22]: num = int(input("Display multiplication table of: ")) 1 2 3 for i in range(i, 11) 4 print(f"{num} x {4} = {numi}") Display multiplication table of: 19 19 X 1 = 19 19 X 2 = 38 19 X 3 = 57 19 X 4 = 76 19 X 5 = 95 19 X 6 = 114 19 X 7 = 133 19 X 8 = 152 19 x 9 = 171 19 xX 10 = 190 Program 18 Write a Python Program to Print the Fibonacci sequence. Fibonacci sequence: The Fibonacci sequence is a series of numbers where each number is the sum of the two. preceding ones, typically starting with 0 and 1. So, the sequence begins with 0 and 1, and the next number is obtained by adding the previous two numbers. This pattern continues indefinitely, generating a sequence that looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and soon. Mathematically, the Fibonacci sequence can be defined using the following recurrence relation: F(0)=0 F())=1 F(n)=F(n-1)+F(n-2)forn>| In (23]:| 1 nterms = intCinput( "How many terms? *)) # first two terns ni, n2 = 0, 1 count = 0 1 2 3 4 5 # check if the number of terms is valid if nterms 6 ee 7 8 9 print("Please enter a positive integer") 32 # if there is only one term, return nt 11 Sie nteres o= 1: 12 eee rine(Flboneccl sequence uptot nterns, 1a print (nt) # generate fibonacci sequence 15 ease: 16 prine(*Ftbonacct sequence:*) i7| tntle coure «nearest & print(na) 3 Rea a FA eet veLand 2 n n2 a n2 = nth 23 count += 1 How many terms? 1 Fibonacci sequence: @11235813 2134 8/95 Program 19 Write a Python Program to Check Armstrong Number? Armstrong Number: tis a number that is equal to the sum of its own digits, each raised to a power equal to the number of digits in the number. For example, let's consider the number 153: + Ithas three digits (1, 5, and 3). * If we calculate 13 + 53+33, we get 14+125+27, which is equal to 153. So, 153 is an Armstrong number because it equals the sum of its of the number of digits in the number. Another example is 9474: ts raised to the power * Ithas four digits (9, 4, 7, and 4). + Ifwe calculate 94+44+74.4*we get 6561 + 256 + 2401 + 256, whichis also equal to 9474. Therefore, 9474 is an Armstrong number as well. In [25]: 1 num = int(input("Enter a number: ")) 2 3 # Calculate the number of digits in num num_str = str(num) 4 num_digits = len(num_str) 5 © # Initialize variables sum_of_powers = @ temp_num = num 7 : # Calculate the sum of digits raised to the power of num_digits 1@ while temp_num > @: 11 12 13 4 15 digit = temp_num % 10 16 sum_of_powers += digit ** num_digits 17 temp_num //= 10 18 19 # Check if it’s an Armstrong number 29 if sum_of_powers == num: print(#"{num} is an Armstrong number." 21 else: 22 print(f"{num} is not an Armstrong number.") 23 Enter a number: 9474 9474 is an Armstrong number. 9995 Program 20 Write a Python Program to Find Armstrong Number in an Interval. In In [26]: [27]: 1 # Input the interval from the user lower 2 the lower limit of the interval: ")) upper int (input (“Enter int (input (“Enter 3 the upper limit of the interval: “)) rt 5 6 for num in range(lower, upper + 1): # Iterate through the numbers i 2 order = len(str(num)) # Find the number of digits in ‘num’ 3 ‘temp_num = num 2 sum = @ 10 11 a while temp_num > 0: 3 digit = temp_num % 10 14 sum += digit ** order 15 temp_num //= 10 16 7 # Check if ‘num’ is an Armstrong number 18 if num == sum: print (num) Enter the lower limit of the interval: 10 Enter the upper limit of the interval: 1000 153 37 371 407 Program 21 Write a Python Program to Find the Sum of Natural Numbers. Natural numbersare a set of positive integers that are used to count and order objects. They are the numbers that typically start from 1. and continue indefinitely, including all the whole numbers greater than 0. In mathematical notation, the set of natural numbers is often denoted as "N" and can be expressed as: N =1,2,3,4,5,6,7,8, Limit = int(input(“Enter the Limit: ")) # Initialize the sum sum = @ 1 2 3 4 5 # Use a for loop to calculate the sum of natural numbers § for i in range(a, Limit + 2): 8 9 e 1 sum += 4 # Print the sum print("The sum of natural numbers up to”, limit, “is:", sum) Enter the limit: 10 0 The sum of natural numbers up to 1@ is: 55 ans

You might also like