Wednesday 6 September 2023

Top 10 Python if .. else programs

Python if else statement is one of the popular decision making statement. Refer following top 10 python programs based on if..else statement.

Top 10 Python if else Programs

1. Age Classifier: Classify a person's age into categories like child, teenager, or adult.

age = int(input("Enter your age: "))
if age < 18:
    print("Child/Teenager")
else:
    print("Adult")

Output:
Enter your age: 16 Child/Teenager

2. Positive, Negative, or Zero: Check if a number is positive, negative, or zero.

num = float(input("Enter a number: "))
if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")

Output:
Enter a number: 5 Positive

3. Even or Odd Checker: Check if a number is even or odd.

num = int(input("Enter a number: "))
if num % 2 == 0:
    print("It's Even")
else:
    print("It's Odd")

Output:
Enter a number: 6 It's Even

4. Grade Calculator: Calculate and display the grade based on a student's score.

score = int(input("Enter your score: "))
if score >= 90:
    print("A Grade")
elif score >= 80:
    print("B Grade")
elif score >= 70:
    print("C Grade")
else:
    print("F Grade")

Output:
Enter your score: 85 B Grade

5. Largest Number Finder: Find the largest among three numbers.

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
if num1 >= num2 and num1 >= num3:
    print("Largest number is", num1)
elif num2 >= num1 and num2 >= num3:
    print("Largest number is", num2)
else:
    print("Largest number is", num3)

Output:
Enter the first number: 10 Enter the second number: 11 Enter the third number: 12.5 Largest number is 12.5

6. Vowel or Consonant: Determine if a character is a vowel or consonant.

char = input("Enter a character: ")
if char in 'aeiouAEIOU':
    print("It's Vowel")
else:
    print("It's Consonant")

Output:
Enter a character: a It's Vowel

7. Positive or Negative Number List: Separate positive and negative numbers in a list.

numbers = [1, -2, 3, -4, 5, -6]
positive = []
negative = []
for num in numbers:
    if num >= 0:
        positive.append(num)
    else:
        negative.append(num)
print("Positive numbers:", positive)
print("Negative numbers:", negative)

Output:
Positive numbers: [1, 3, 5] Negative numbers: [-2, -4, -6]

8. User Authentication: Authenticate a user based on a username and password.

username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin" and password == "password":
    print("Login successful")
else:
    print("Login failed")

Output:
Enter username: admin Enter password: password Login successful

9. Triangle Type Checker: Determine the type of triangle based on its sides.

a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))

if a == b == c:
    print("Equilateral triangle")
elif a == b or b == c or a == c:
    print("Isosceles triangle")
else:
    print("Scalene triangle")

Output:
Enter side a: 6 Enter side b: 6 Enter side c: 6 Equilateral triangle

10. Check if a string is a palindrome.

string = input("Enter a string: ")
reversed_string = string[::-1]
if string == reversed_string:
  print("The string is a palindrome.")
else:
  print("The string is not a palindrome.")

Output:
Enter a string: ABCBA The string is a palindrome.



Sunday 3 September 2023

Python Advantages and Disadvantages

Some of the key advantages and disadvantages of Python programming are listed below:

Advantages of Python:

  • Easy to learn and use. Python has a simple and easy-to-understand syntax that makes it a great language for beginners.
  • Powerful and versatile. Python is a general-purpose language that can be used for a wide variety of tasks, including web development, data science, machine learning, and artificial intelligence.
  • Open-source and free. Python is an open-source language, which means that it is free to use and distribute. There is also a large community of Python developers who contribute to the language and its libraries.
  • Wide range of libraries and frameworks. Python has a wide range of libraries and frameworks that make it easy to develop complex applications. It has a vast library of modules and packages that can be used to perform a variety of tasks.
  • Portable. Python code can be run on a variety of platforms, including Windows, macOS, Linux, and Unix. This makes it a great language for developing cross-platform applications.

Disadvantages of Python:

  • Slow performance. Python is not as fast as some other programming languages, such as C++ or Java. This can be a disadvantage for applications that require high performance.
  • Database access: Python's database access layer is not as mature as some other languages, such as Java or C#. This can make it more difficult to develop database-intensive applications in Python.
  • Dynamically typed. Python is a dynamically typed language, which means that the type of a variable is not checked until it is used. This can lead to errors if the variable is not used correctly.
  • Not as good for low-level programming. Python is not as good as some other languages for low-level programming, such as writing device drivers or operating system kernels.
  • Package management: Python has a large number of packages and libraries, which can make it difficult to manage dependencies and avoid version conflicts.

Despite these disadvantages, Python is a powerful and versatile language that is used by millions of developers around the world. It is a great choice for a variety of tasks, including web development, data science, machine learning, and artificial intelligence. It is a great choice for a variety of tasks, and its advantages often outweigh its disadvantages.