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

Popular posts from this blog

Python Calculator Program with GUI Intercface

   Python Calculator Program with GUI Intercface Here is a Python program using the tkinter library to create a simple GUI-based calculator. This calculator can perform basic arithmetic operations like addition, subtraction, multiplication, and division.   import tkinter as tk # Function to update the input field when a button is clicked def button_click(item):     global expression     expression = expression + str(item)     input_text.set(expression) # Function to clear the input field def button_clear():     global expression     expression = ""     input_text.set("") # Function to evaluate the expression and display the result def button_equal():     global expression     try:         result = str(eval(expression))  # Evaluate the expression         input_text.set(result) ...

Python GUI Program to Calculate Age

Python GUI Program to Calculate Age     Certainly! Below is a Python program using the tkinter library to create a simple GUI that accepts a date of birth and calculates the age.   import tkinter as tk from tkinter import messagebox from datetime import datetime def calculate_age():     dob_str = entry_dob.get()     try:         dob = datetime.strptime(dob_str, "%Y-%m-%d")         today = datetime.today()         age = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))         messagebox.showinfo("Age Calculator", f"Your age is: {age} years")     except ValueError:         messagebox.showerror("Error", "Please enter the date in YYYY-MM-DD format") # Create the main window root = tk.Tk() root.title("Age Calculator") # Create a labe...

Simple Number Guessing Game in Python

  Build a Simple Number Guessing Game in Python: A Beginner-Friendly Project Are you looking to dive into Python programming with a fun and interactive project? The number guessing game Python project is an excellent starting point for beginners. This classic game challenges players to guess a randomly generated number within a limited number of attempts, helping you practice essential concepts like loops, conditionals, and user input. In this blog post, we'll walk through how to create your own Python guessing game project , complete with code and detailed explanations. Whether you're a student, hobbyist, or aspiring developer, this tutorial will boost your skills while keeping things engaging. Why Choose a Number Guessing Game as Your Python Project? The number guessing game Python is one of the most searched beginner projects because it's simple yet versatile. It teaches core programming fundamentals without overwhelming you with complex libraries or frameworks. Her...