Posts

Showing posts with the label Python

Write a Python program that takes a list of numbers as input and returns the sum of all even numbers in the list

Image
  Program Code: Program Explanation: The function sum_even_numbers takes a single argument numbers, which is a list of integers. The function uses a list comprehension to create a new list of even_numbers containing only the even numbers from the input list. This is done using the modulo operator %, which returns the remainder of dividing a number by 2. If the remainder is 0, then the number is even, and it is added to the even_numbers list. The sum function is then called on the even_numbers list to compute the sum of all the even numbers in the list. The sum of the even numbers is returned as the output of the function. Overall, this function is a simple and efficient way to compute the sum of even numbers in a list of integers.

Python Program to Check Whether an Integer is Armstrong Number or Not.

Image
  An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits in the number. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. Program Code: Program Explanation: We first take an input integer from the user using the input() function and convert it to an integer using the int() function. We determine the number of digits in the input number by converting it to a string using the str() function, getting its length using the len() function, and storing the result in the variable num_digits. We initialize a variable called sum_of_cubes to 0, which will hold the sum of the cubes of each digit in the input number. We use a for loop to iterate over each digit in the input number. Since the input number is an integer, we first convert it to a string using the str() function so that we can iterate over each of its digits. For each digit, we convert it back to an integer using the int() function, cube it us...

Fibonacci Series Program in Python using Iteration

Image
  1. Program Code: 2. Program Explanation: We define a function called Fibonacci that takes an integer n as input. We initialize the first two terms of the Fibonacci series to 0 and 1, respectively, using tuple unpacking: a, b = 0, 1. These values will be updated as we generate the series. We check if n is equal to 0 or 1. If n is 0, then we return an empty list because there are no terms in the series. If n is 1, then we return a list with the first term of the series, which is 0. If n is greater than 1, then we initialize a list called fib_series with the first two terms of the series, which are 0 and 1. We use a for loop to iterate over the range from 2 to n - 1, since we've already accounted for the first two terms of the series. Inside the loop, we compute the next term of the series by adding the previous two terms: c = a + b. We then update the values of a and b to prepare for the next iteration: a, b = b, c. Finally, we append the new term c to the list of series terms: f...

Fibonacci Series in Python Using While Loop

Image
So, today we'll be discussing how we can make the Fibonacci series using python. What is the Fibonacci series? In the Fibonacci sequence, the sum of the two preceding numbers. it is commonly started with 0 and 1. You can read more about the history of the Fibonacci series on Wikipedia . Now, let's see how we can calculate it in python. In this python program:  We're first taking input from the user that how many terms he wants to print. (User input) Then, we have made two conditions to check if the user enters a negative number or first number so we print that accordingly. If the above of the two conditions are false then the while loop will run to print the fibonacci numbers.

How to Connect Microsoft SQL Server with Python

Image
Today we'll be discussing how you can connect Microsoft SQL Server Database(2019) with Python. So, you have been here then I'm assuming you know pretty much about Python programming. First, you need to install pyodbc using pip - the python package manager. Now, you'll be thinking what is pyodbc? It is an open-source Python module that makes it super easy for us to access ODBC datasbases. Enter this command in your terminal: pip install pyodbc Now you need two more things to move forward: 1. Your SQL Server Instance Name 2. And your database name which you want to connect. Here's the code to connect your SQL database successfully with Python. Note: My instance is blank because I have used the default instance when installing SQL Server.

Swapping Using Two Variables in Python

Image
So, today we'll learn how we can swap using two variables only. using three variables is popular among programmers and they think it can't be done using two variables.  But it's not the case it can be done, of course in certain conditions, such as only in numbers. We can use some maths and sort the values of variables easily. Let's how to do swapping in python using two variables only. EASY CHEESY So, here's the logic which you need to understand, instead of memorizing the code. Let's consider: a = 1 b = 2 So we'll do some maths in it to swap it. a = 1 + 2, a is now equal to 3  b = 3 - 2, b is now equal to 1  and finally, a = 3 - 1, a is now equal to 2. swapping done using some simple maths. Here's how you can do it in python.