Python Classroom Series – 28/Feb/2020

Identical vs Equal

  • == is used to verify value equality
  • is is used to verify variable ids are same or not (Identical)

Python Executing programs

Some important functions for our survival

  • Till now we know
    • type()
    • id()
    • print()
  • Today lets add two more
    • input(): This function is used to take inputs from command line
    • int(): converting the values to integers

Solving Project Euler: 1

  • Refer Here for problem statement
  • Informal Language/Psuedo Code:
1: max_number = 10
2: number1 = 3
3: number2 = 5
4: result = 0

:IMP
5: start from 1 till max_number and store in index
6:   check if index%3 == 0 | index%5 == 0 is true
7:   if its true add index to result ( result += index)
8:  index += 1
9:   Go to IMP:


  • If you look at statements from line 6 to 9, this should be executed for all the values from 1 to max_number and we need to take the help of looping constructs
  • Now lets try to write the program with while loop Refer Here
max_number = 10
number1 = 3
number2 = 5
result = 0
index = 1
while index < max_number:
    print('checking for index = '+ str(index))
    is_divisible = (index%3 == 0 or index%5 ==0)
    print('is_divisble by 3 or 5 = '+str(is_divisible))
    if is_divisible:
        result = result + index	
    print('result so far = '+str(result))
    index += 1
print(result)
  • The improvised version is enable extra print statements on demand
extra_checks = True
max_number = 10
number1 = 3
number2 = 5
result = 0
index = 1
while index < max_number:
    if extra_checks:
        print('checking for index = '+str(index))
    is_divisible = (index%3 == 0 or index%5 ==0)
    if extra_checks:
        print('is_divisble by 3 or 5 = '+str(is_divisible))
    if is_divisible:
        result = result + index	
    if extra_checks:
        print('result so far = '+str(result))
    index += 1
print(result)
  • Actual version of program is
max_number = 10
number1 = 3
number2 = 5
result = 0
index = 1
while index < max_number:
    is_divisible = (index%3 == 0 or index%5 ==0)
    if is_divisible:
        result = result + index	
    index += 1
print(result)
  • The above program can be implemented in for
max_number = 10
number1 = 3
number2 = 5
result = 0
for index in range(1,max_number):
    is_divisible = (index%3 == 0 or index%5 ==0)
    if is_divisible:
        result = result + index	
print(result)
  • One Best Practice: Please add comments explaining the code if necessary
# extra checks will print statements for verifying correctness of program
extra_checks = (input('Enter 1 for checks 0 for no checks') == '1')
# maximum number below  which the program will 
# execute for every number starting from 1
max_number = 10
# assuming result as zero to start with
result = 0
for index in range(1,max_number):
    if extra_checks:
        print('checking for index = '+str(index))
    is_divisible = (index%3 == 0 or index%5 ==0)
    if extra_checks:
        print('is_divisble by 3 or 5 = '+str(is_divisible))
    if is_divisible:
        result = result + index	
    if extra_checks:
        print('result so far = '+str(result))
print(result)

Exercise: Write a python program to print the following

The current number is 1
The current number is 2
...
...
...
The current number is 100

Leave a Comment

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

About learningthoughtsadmin