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

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...

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...

Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं

Python is a versatile and popular programming language known for its simplicity and readability. It has a wide range of features that make it suitable for various types of projects. Here are some key features of the Python programming language:- पायथन एक बहुमुखी और लोकप्रिय प्रोग्रामिंग भाषा है जो अपनी सरलता और पठनीयता के लिए जानी जाती है। इसमें कई प्रकार की विशेषताएं हैं जो इसे विभिन्न प्रकार की परियोजनाओं के लिए उपयुक्त बनाती हैं। पायथन प्रोग्रामिंग भाषा की कुछ प्रमुख विशेषताएं निम्न हैं:- 1. Open Source ओपन सोर्स:-   Python is an open-source language, which encourages collaboration and allows developers to contribute to its ongoing development. पायथन एक ओपन-सोर्स भाषा है, जो सहयोग को प्रोत्साहित करती है और डेवलपर्स को इसके चल रहे विकास में योगदान करने की अनुमति देती है। 2. Integrated Development & Learning Environments एकीकृत विकास एवं अध्ययन वातावरण  (IDLEs/IDEs):-  Python has several powerful IDLEs/IDEs, such as PyCharm, VSCode, and Jupyter, that provide too...