Python Essentials Classroom Series – 09/Sept/2021

Exercise

  • Code
number = int(input('Enter the number: '))
index = 2
is_prime = True
while index < number:
    if number%index == 0:
        is_prime = False
        break
    index += 1

if is_prime:
    print(f"{number} is prime")
else:
    print(f"{number} is not prime")
  • Exercise: Extend the program to print all the prime numbers between 10 and 100
number = 10
while number <= 100:
    index = 2
    is_prime = True
    while index < number:
        if number%index == 0:
            is_prime = False
            break
        index += 1
    if is_prime:
        print(number, sep=',', end=',')
    number += 1

Skip Ahead with continue

  • Sometimes, you dont want to break out of a loop but just want to skip ahead to next iteration for some logical reason. This is where the continue can help.
  • The below example shows the python code which skips the iteration if index is divisible of 3
index = 1
while index <= 100:
    if index%3 == 0:
        index += 1
        continue
    print(index)
    index += 1

Check break Use with else

  • If the while loop or for loop ended normally (i.e. no break call), control passes to an optional else block
number = int(input('Enter the number: '))
index = 2
while index < number:
    if number%index == 0:
        print(f"{number} is not prime")
        break
    index += 1
else: # break not called
    print(f"{number} is prime")


Generating Number sequences with range()

  • The range() function returns a stream of numbers with in a specified range
range(start, stop, step)
  • The only required value is stop.
  • If you have not passed start value default start is 0
  • If you have not passed step the default value is 1
  • range() functions returns an iterable object

Iterate with for and in

  • Python makes frequent usage of iterators
  • Any iterable object can be iterated using while but for is better python way
for item in <iterable>:
    #block
  • Samples Preview
  • We can use break, continue and else much like what we have done with while loop

Lets write programs for the problems solved during foundation classes

  • Project Euler Problem 1: Refer Here for problem description
sum = 0
for number in range(1,1000):
    if number % 3 == 0 or number%5 == 0:
        sum += number

print(sum)
first = 1
second = 2
sum_of_even = 2
third = 0
while third < 4000000:
    third = first + second
    if third%2 == 0:
        sum_of_even += third
    first = second
    second = third
print(sum_of_even)
number = 600851475143
for index in range(number//2,1, -1):
    if number% index == 0:
        # This is a factor
        p_index = 2
        while p_index < index//2:
            if index%p_index == 0:
                break
            p_index += 1
        else: # break not called
            print(f"{index} is largest prime factor")
            break



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin