Skip to main content

Basic python programs पाइथन के बेसिक प्रोग्राम

1.) Python program to print message "Hello, World!".

print("Hello, World!")


2.) Python program to check if a Number is Even or Odd.
num = int(input("Enter a number: "))
if num % 2 == 0:
    print(f"{num} is an even number.")
else:
    print(f"{num} is an odd number.")


3.) Python program to find the Sum of Two Numbers.
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
sum = num1 + num2
print(f"The sum of {num1} and {num2} is {sum}.")


4.) Python program to calculate Factorial of a Number.
num = int(input("Enter a Positive Integer Number: "))
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 *= i
    print(f"The factorial of {num} is {factorial}.")


5.) Python program for Simple Calculator.
def calculator(num1, num2, operator):
    if operator == '+':
        return num1 + num2
    elif operator == '-':
        return num1 - num2
    elif operator == '*':
        return num1 * num2
    elif operator == '/':
        return num1 / num2
    else:
        return "Invalid operator"
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
result = calculator(num1, num2, operator)
print(f"Result: {result}")


6.) Python program to check whether given number is Prime Number or not.
num = int(input("Enter a number: "))
if num > 1:
    for i in range(2, num):
        if num % i == 0:
            print(f"{num} is not a prime number.")
            break
    else:
        print(f"{num} is a prime number.")
else:
    print(f"{num} is not a prime number.")


7.) Python program to print Fibonacci Series.
n = int(input("How many terms? "))
a, b = 0, 1
count = 0
if n <= 0:
    print("Please enter a positive integer.")
elif n == 1:
    print("Fibonacci sequence up to 1 term: ")
    print(a)
else:
    print("Fibonacci sequence: ")
    while count < n:
        print(a)
        nth = a + b
        a = b
        b = nth
        count += 1


8.) Python program to find the Largest of Three Numbers.
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(f"The largest number is {largest}.")


9.) Python program to find that given number or word is palindrome or not.

word = input("Enter a word or number: ")

if word == word[::-1]:
    print(f"{word} is a palindrome.")
else:
    print(f"{word} is not a palindrome.")


10.) Python program to count the Number of Vowels in a String.
string = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for char in string:
    if char in vowels:
        count += 1
print(f"The number of vowels in the string is {count}.")


11.) Python program to reverse a String.
string = input("Enter a string: ")
reversed_string = string[::-1]
print(f"Reversed string: {reversed_string}")


12.) Python program to count occurrences of a Character in a String.

string = input("Enter a string: ")
char = input("Enter the character to count: ")
count = string.count(char)
print(f"The character '{char}' appears {count} times in the string.")


13.) Python program to check for an Armstrong Number.
num = int(input("Enter a number: "))
order = len(str(num))
sum = 0
temp = num
while temp > 0:
    digit = temp % 10
    sum += digit ** order
    temp //= 10
if num == sum:
    print(f"{num} is an Armstrong number.")
else:
    print(f"{num} is not an Armstrong number.")


14.) Python program to check Leap Year.

year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")


15.) Python program to calculate Sum of Natural Numbers.
n = int(input("Enter a positive integer: "))
if n < 0:
    print("Enter a positive integer.")
else:
    sum = (n * (n + 1)) // 2
    print(f"The sum of the first {n} natural numbers is {sum}.")


16.) Python program to find the Smallest Number in a List.
numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
smallest = min(numbers)
print(f"The smallest number is {smallest}.")


17.) Python program to find the Largest Word in a Sentence.

sentence = input("Enter a sentence: ")
words = sentence.split()
largest_word = max(words, key=len)
print(f"The largest word is '{largest_word}'.")


18.) Python program to convert Celsius to Fahrenheit.
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F.")


19.) Python program to generate a Multiplication Table.

num = int(input("Enter a number: "))
for i in range(1, 11):
    print(f"{num} x {i} = {num * i}")


20.) Python program to calculate Simple Interest.
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate (in %): "))
time = float(input("Enter the time (in years): "))
simple_interest = (principal * rate * time) / 100
print(f"The simple interest is {simple_interest}.")


21.) Python program to check that given List is Sorted or Not.
numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
if numbers == sorted(numbers):
    print("The list is sorted.")
else:
    print("The list is not sorted.")


22.) Python program to remove Duplicate elements from a List.

numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
unique_numbers = list(set(numbers))
print(f"List after removing duplicates: {unique_numbers}")


23.) Python program to find the GCD (Greatest Common Divisor) of Two Numbers.
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

result = gcd(num1, num2)
print(f"The GCD of {num1} and {num2} is {result}.")


24.) Python program to count the Frequency of Words in a String.
sentence = input("Enter a sentence: ")
words = sentence.split()
word_freq = {}
for word in words:
    word_freq[word] = word_freq.get(word, 0) + 1
for word, freq in word_freq.items():
    print(f"{word}: {freq}")


25.) Python program to swap the values of Two Variables.
a = input("Enter value for a: ")
b = input("Enter value for b: ")
print(f"Before swapping, a = {a} and b = {b}.")
# Swapping
a, b = b, a
print(f"After swapping, a = {a} and b = {b}.")

Comments

Popular posts from this blog

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

The Python Interpreter पायथन इंटरप्रेटर

The Python interpreter is a program that executes Python code. It reads and interprets Python scripts or interactive commands, converting them into machine-readable bytecode for the computer to execute.The interpreter is a crucial component for running Python code, facilitating both learning and development processes. Users can interact with the interpreter in two primary modes: पायथन इंटरप्रेटर एक प्रोग्राम है जो पायथन कोड को निष्पादित करता है। यह पायथन स्क्रिप्ट या इंटरैक्टिव कमांड को पढ़ता है और व्याख्या करता है, उन्हें कंप्यूटर द्वारा निष्पादित करने के लिए मशीन-पठनीय बाइटकोड में परिवर्तित करता है। दुभाषिया पायथन कोड चलाने के लिए एक महत्वपूर्ण घटक है, जो सीखने और विकास दोनों प्रक्रियाओं को सुविधाजनक बनाता है। उपयोगकर्ता इंटरप्रेटर के साथ दो प्राथमिक मोड में बातचीत कर सकते हैं:- 1. Interactive Mode:- In this mode we can launch the Python interpreter in our terminal or command prompt without specifying a script.This mode allows us to enter Python commands and see immediate results...

Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह

Flow of execution represents a general outline of Python program execution. The specific flow can vary based on the program's structure, control flow statements (if, else, for, while), and function calls. निष्पादन का प्रवाह पायथन प्रोग्राम निष्पादन की एक सामान्य रूपरेखा का प्रतिनिधित्व करता है। विशिष्ट प्रवाह प्रोग्राम की संरचना, नियंत्रण प्रवाह विवरण (यदि, अन्यथा, के लिए, जबकि), और फ़ंक्शन कॉल के आधार पर भिन्न हो सकता है। 1.) Start: The program begins execution. 1.) प्रारंभ: प्रोग्राम का निष्पादन प्रारंभ होता है। 2.) Import Modules: If necessary, the program imports external modules or libraries. 2.) मॉड्यूल आयात करें: यदि आवश्यक हो, तो प्रोग्राम बाहरी मॉड्यूल या लाइब्रेरी आयात करता है। 3.) Define Functions: Any functions used in the program are defined. 3.) फंक्शन्स को परिभाषित करें: प्रोग्राम में उपयोग किए गए किसी भी फंक्शन को परिभाषित किया जाता है। 4.) Main Program: The main body of the program starts. 4.) मुख्य प्रोग्राम: प्रोग्राम का मुख्य भाग प्रारंभ होता है। 5.) Initializat...