Posts

prime number program in python

 Here is a sample Python program that checks if a given number is a prime number: python Copy code def is_prime(n):     if n <= 1:         return False     for i in range(2, int(n**(1/2))+1):         if n % i == 0:             return False     return True num = int(input("Enter a number: ")) if is_prime(num):     print(num, "is a prime number.") else:     print(num, "is not a prime number.") This program takes an input from the user, and checks if that number is prime by dividing it by all integers greater than 1 and less than the square root of that number, if the number is divisible by any of the integers it returns not a prime number otherwise it returns prime number.

fibonacci series in python

Here is an example of a code for generating the Fibonacci series in Python: Python def fibonacci_series(n):     if n <= 0:         return []     elif n == 1:         return [0]     elif n == 2:         return [0, 1]     else:         fib_list = [0, 1]         for i in range(2, n):             fib_list.append(fib_list[i-1] + fib_list[i-2])         return fib_list n = int(input("Enter the number of terms for Fibonacci series: ")) print(fibonacci_series(n)) The function fibonacci_series(n) takes in an integer n as input and returns a list of the first n terms of the Fibonacci series. The function first checks if the input is less than or equal to 0, in which case it returns an empty list. If the input is 1, it returns a list with a single element of 0. If the input is 2, it returns a list with 2 elements, 0 and 1. If the input is greater than 2, it creates a list fib_list and initializes it with the first two elements of the series (0 and 1). Then it uses a for loop to iterat

python code for calculator

 Here is an example of a simple calculator program written in Python: Python Copy code def add(x, y):     return x + y def subtract(x, y):     return x - y def multiply(x, y):     return x * y def divide(x, y):     return x / y print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") choice = input("Enter choice(1/2/3/4): ") num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if choice == '1':     print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2':     print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3':     print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4':     print(num1, "/", num2, "=", divide(num1, num2)) else:     print("Invalid Input") This program allows

python code for snake game

 Here is an example of a simple snake game implemented in Python using the Pygame library: python Copy code import pygame import time # Initialize pygame pygame.init() # Set the size of the screen (width, height). size = (700, 500) screen = pygame.display.set_mode(size) pygame.display.set_caption("My Snake Game") # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() # Initialize the snake's starting position and speed snake_block = 10 snake_speed = 30 snake_list = [] snake_length = 1 # Define colors black = (0,0,0) white = (255, 255, 255) red = (255, 0, 0) # Initialize the snake's starting position x1 = 300 y1 = 300 x1_change = 0 y1_change = 0 font_style = pygame.font.SysFont(None, 50) def message(msg,color):     mesg = font_style.render(msg, True, color)     screen.blit(mesg, [300,250]) # Main game loop while not done:     # --- Main event loop     for event in pygame.event.get():      

how to make artificial intelligence like jarvis

I can provide you with an example of a basic Python script for a personal assistant AI, however, keep in mind that creating a full-fledged personal assistant AI requires a lot of resources and advanced programming skills. Here is an example of a basic Python script for a personal assistant AI using the SpeechRecognition and pyttsx3 libraries: Python ' import speech_recognition as sr import pyttsx3 def speak(text):     engine = pyttsx3.init()     engine.say(text)     engine.runAndWait() def get_audio():     r = sr.Recognizer()     with sr.Microphone() as source:         audio = r.listen(source)         said = ""         try:             said = r.recognize_google(audio)             print(said)         except Exception as e:             print("Exception: " + str(e))     return said text = get_audio() if "hello" in text:     speak("Hello, how can I help you today?") This script uses the SpeechRecognition library to listen for audio input from