Skip to main content

Python Basic Programs to Practice

 

Python Programming Introduction

Python is a high-level, interpreted programming language known for its simplicity, readability, and ease of learning. It's versatile, suitable for various applications, including web development, data science, automation, artificial intelligence, and more. Python's popularity stems from its vast standard library, extensive community support, and compatibility with other programming languages.

Key Features of Python

  1. Simple and Easy to Learn: Python's syntax is straightforward, making it a great choice for beginners.
  2. Interpreted Language: Code is executed line-by-line, making debugging easier.
  3. Object-Oriented: Supports classes and objects, promoting organized, reusable code.
  4. Extensive Libraries: Python has a large standard library for handling various tasks and third-party libraries for specialized needs.
  5. Cross-Platform: Python can run on various operating systems, including Windows, macOS, and Linux.

Python Basic Syntax

  • Variables: Used to store data values.
    name = "Alice"
    age = 25
    
  • Data Types: Common types include integers, floats, strings, lists, tuples, sets, and dictionaries.
  • Comments: Lines that begin with # are comments and are not executed.
    # This is a comment
    

Basic Programs in Python

1. Hello, World! Program

The most basic program to start learning Python.

print("Hello, World!")

2. Simple Calculator Program

A program to perform basic arithmetic operations.

# Get user input
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# Perform operations
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)

3. Check Even or Odd

Determine if a number is even or odd.

num = int(input("Enter a number: "))
if num % 2 == 0:
    print(f"{num} is Even")
else:
    print(f"{num} is Odd")

4. Finding the Largest Number

Find the largest of three numbers.

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

largest = max(a, b, c)
print("The largest number is:", largest)

5. Factorial of a Number

Calculate the factorial of a number using a loop.

num = int(input("Enter a number: "))
factorial = 1

for i in range(1, num + 1):
    factorial *= i

print(f"The factorial of {num} is {factorial}")

6. Fibonacci Sequence

Generate the Fibonacci sequence up to a certain number of terms.

n = int(input("Enter number of terms: "))
a, b = 0, 1

for _ in range(n):
    print(a, end=" ")
    a, b = b, a + b

7. Prime Number Checker

Check if a number is prime.

num = int(input("Enter a number: "))
if num > 1:
    for i in range(2, int(num**0.5) + 1):
        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")

8. Reverse a String

Reverse a string entered by the user.

text = input("Enter a string: ")
reversed_text = text[::-1]
print("Reversed string:", reversed_text)

Summary

These examples cover fundamental concepts such as input/output, conditional statements, loops, and simple data manipulation, laying a foundation for more advanced topics in Python programming.

Comments