Python Classroom notes 05/Mar/2026

Lets start writing interactive programs

Problem 1: Findout if the number is prime or not

  • Program
if __name__ == "__main__":
    # get the number from user
    number = int(input("Enter any number: "))
    index = 2
    if number >= 2:
        is_prime = True
        while index < number:
            if number % index == 0:
                is_prime = False
                break
            index += 1
    else:
        is_prime = False
    if is_prime:
        print("Prime")
    else:
        print("Not a prime number")
  • We have written code which can find if the number is prime and in the future whenever we want to findout if the number is prime or not we can use this code.
  • First way of making code reusable is function.
  • Refer Here for programiz docs on functions

Functions

  • A function is a reusable block.
  • Basic syntax
def <func-name>(args):
   ...
   ...
  • A function can return a value or not return a value
  • Sample functions
# defining a function
def print_message(message, count):
    index = 0
    while index < count:
        print(message)
        index += 1

# Define a function which returns a value
def is_even(number):
    return number % 2 == 0
  • using functions
print_message("hello", 5)
is_even(7)
  • Lets introduce docstrings. Google style of docstrings are popular.
  • You can install docstring extension in vscode
    Preview

  • Prompt:

You are an expert in python programming

Give me examples to learn how to document functions in python
by using docstrings according to google style guide
  • It is also recommended to give type hints.
def is_even(number:int) -> bool:
    return number % 2 == 0

def print_message(message:str, count:int) -> None:
    index = 0
    while index < count:
        print(message)
        index += 1

By continuous learner

enthusiastic technology learner

Leave a Reply

Discover more from Direct AI Powered By Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading