Python: Prime Numbers in Range

April 16, 2019 Leave a comment

Python: Prime Numbers in a Range

Our task here is simple as the title, we will have a range and will test each number to see if it is a prime then we will add it to a list, once we finish we will print out the list.

To complete this task we will use one of our function we create last time (Read: is prime post).

So, here we will ask the user to input two numbers num1 and num2 the we will pass all the numbers in the range to is_prime() and store the result in a list.

The Code:

#Function to get all Prime numbers in a range.
#Ask the user to enter two numbers
print ‘Get all Prime numbers in a range\n’
num1=int(input(“Enter the first number in the range: “))
num2=int(input(“Enter the last number in the range: “))
#create the list

def get_all_prime(num1,num2):

for x in range (num1,num2):

if is_prime(x) ==’Prime’:


return prime_list

#Call the function and print the list
print prime_list
print len(prime_list)

Follow me on Twitter..


Python Project

April 15, 2019 Leave a comment

Python: Bite 107

Filter numbers with a list comprehension

I was happy to join Pybites and start to solve the code-challenges, my problem from the first bite was that i am solving the challenges in other online python interpreter, and it looks working fine and the output seams as it should be, but once I copy the code in Pybite portal and doing the code test it fail and gives errors.

In this challenge Bite 107, after a list of numbers the task is to return the positive even number. Now in the task description it mentioned to use (elegant list comprehension; one line of code). I start to work on it, immediately it jump to my head a for loop and if each item is even and more than 0 add it to my_list, then return my_list. After coding, the output was a list with positive and even numbers in a Number-list, but it failed in the Bite test portal. WAY!.

This type of failure was against me in other challenges also, and i was just setting all the day thinking what happen!. With Bite 107 I decide to click on “Show Solution” button just to see what is wrong.

One line code:The Solution was in one line of list brackets after a return statement congaing for and if without my_list. just like that. I start smiling, and ask my self “How could i know that i can write it like this??”. Yes it is beautiful, Yes elegant and yes one line of code, and yes my-way of coding gives the answer that we want, my code pass my testing exam.

Concloguen: I think i come up with a good resource of learning python, I don’t want to read book’s but reading codes that doing things and having the alternatives of writing same code; this will help me a lot. I am not coding for NASA or in other sensitive area that processing time is essential and micro-milli seconds make difference, so one line of four-line to-me is OK. 🙂

Acknowledge: I’d like to thank Pybite community for there contribution in spreading knowledge and codes, and for this challenges. I will read the challenges, and try my best to solve them as elegant as i can, also i will not stop in one point because my code is 2 lines more than what should be.

Last thing, Bite 107 here is my code and the elegant code. Be calm and positive.

The Code:

#First: My Code
def filter_positive_even_numbers(numbers):


for i in range(len(numbers)):

if (i>0 and i%2==0) :my_list.append(i)

return my_list

print (filter_positive_even_numbers(numbers))

#The optimal solution
def filter_positive_even_numbers(numbers):

return[n for n in numbers if n > 0 and n % 2 == 0]

Follow me on Twitter..

Categories: Uncategorized

Python Project

April 14, 2019 Leave a comment

Python: Sum of the Square and Square of the Sum

Difference of sum of the square and the square of the sum

I fond this on Usually I add some steps to there problems to make it more application look and feel. Later on, we’ll see how.

Problem assumption: If we said that we have a range of numbers (1,10) then the sum of this range is 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55, Now the Square of the sum is 55^2; thats mean the Square of the sum of (55) = 3025.

And for the same range, the Sum of Square means that we will get the Square of each number in the range then will get there summation. So with our range (1,10) Sum of Square is 1^2 + 2^2 + 3^2 + 4^2 + 5^2 …. + 10^2 = 385

The Problem #6 in ProjectEuler

The sum of the squares of the first ten natural numbers is,12 + 22 + … + 102= 385

The square of the sum of the first ten natural numbers is,(1 + 2 + … + 10)2 = 552= 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

The Task:Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Inhancment Now to make this task workking as application and to get more general output of it, first we will ask the user to input a range of the numbers, then we will applay the function on that range.

The Code:

# Problem 6 in ProjectEuler.

def square_of_sum(num1, num2):

tot = 0

for x in range(num1, num2+1):

tot = tot+x

print(‘The Square of the Sun’,tot*tot)

return tot*tot

def sum_of_square(num1, num2):

tot = 0

for x in range(num1, num2+1):

tot = tot+(x*x)

print(‘The Sum of the Square ‘, tot)

return tot

#Ask the user for his input.
num1 = int(input(‘Enter First number in the range: ‘))
num2 = int(input(‘Enter the last number in the range: ‘))

#Call the functions.
w1 = square_of_sum(num1, num2)
w2 = sum_of_square(num1, num2)

#Output the Difference.
print(‘The Difference is: ‘, w1-w2)

Follow me on Twitter..

Python Project

April 10, 2019 1 comment

Python: Prime Numbera

Is it Prime

In a simple way, a Prime number is a number that cannot be made by multiplying other numbers. So 4 is Not Prime because we can say that 4 = 2 x 2, 6 is Not Prime because it can be produced by multiplying 3 x 2; but 5 Is Prime because we can’t find any integer numbers that can produce 5.

Numbers such as 1, 3, 5, 7, 11, 13 … all are Prime Numbers.

This function will ask the user to write a number then we will examine it to see whether it is a prime or not.

The Code:

num=int(input(“Enter a number: “))
def is_prime(num):


for t in range (2,num):

if num%t==0 :

result=”Not Prime”


return result
print num,’is’,is_prime(num)

Follow me on Twitter..

Python Project

April 10, 2019 2 comments

Python: Fibonacci Numbers

Even Fibonacci Numbers

Fibonacci Sequence is generated by adding the previous two terms, so the N number in the sequence will be Xn=(Xn-1) + (Xn-2). Example: by starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

The Task: Get the Fibonacci sequence range from the user and count how many even valued terms is there. The user range should not be more than 10000 number.

The Code:

def get_Fibonacci_Sequence(rfrom,rto):
for x in range (rfrom,rto):




# Check if the fab is even, the increment the counter

if fab%2==0 :

count =count +1
# After finish print the output
print”Between”,rfrom,” and “,rto,” there is “,count,”even Fibonacci number.”

range_ok =”no”
while range_ok==”no”:

print(‘Your range must be less than 10,000:’)

# Get the numbers from the user.

rfrom=int(input(‘Enter the range From:’))

rto=int(input(‘Enter the range To:’))

# Check if the numbers are less than 10000

if (rto – rfrom) < 10000 : range_ok ="yes"

#Call the function

Follow me on Twitter..

Python Project

Python: Pluralise a Word

Loop through a dictionary and pluralise a word

One of Python Exercises on @PyBites codechalleng is to loop through a given dictionary of people and the number of games they’ve won then print out the users name and how many games they’ve won in the following format: “sara has won n games” and pluralise the word game to suit the number of games won.

we assume the dictionary is
games_won = dict={‘sara’:0, ‘bob’:1, ‘tim’:5, ‘julian’:3, ‘jim’:1}

The Code:

games_won ={‘sara’:0, ‘bob’:1, ‘tim’:5, ‘julian’:3, ‘jim’:1}
def print_game_stats(games_won):

for key,val in games_won.items():

print(“{} has won {}{p}”.format(key, val,p=” game” if val ==1 else ” games” ))


Follow me on Twitter..

Python Project

Python: Sum n numbers

Bites of Python exercises

I found this Python Exercises on the at PyBites codechalleng and i love the idea of solving problems and this will let me learn more, so thanks guys @pybites.

So this time the challenge is to Write a function that can sum up N numbers. The function should receive a list of N numbers, If no argument is provided, return sum of numbers 1..100.

I hope i did this right, here is my function; the result seems to be fine i am still learning so … sorry if i misunderstood.

def sum_up(my_nums=range(1,100)):


print”The List of number/s “,my_nums

for x in range (len(my_nums)):


print”Total is:”,total

print(“This function will sum up a given list of numbers.\nWhen you finish typping the numbers just press “”Enter””\nIf you did not enter any numbers i will assume the list is [1,100]”)

#Get the list from the user
n=input(“Type the numbers:”)

#In case the user enter some spaces in his number, we should remove it
the_n=n.replace(” “, “”)

#Here we are calling the functin
sum_up() if the_n ==”” else sum_up(the_n)

Follow me on Twitter..