Class 12


Python scripts have .py file extension.
To run python script:
To exit from the interpreter:
To direct the output of a script to a text file:
python > output.txt
To access the value of PI:
import math

To find the length of a string:
To concatenate two strings:
'hello' + 'world'
To search a substring:
To replace all occurrences of one substring with another:
'hahaha'.replace('h', 'n')

Python program to check if a number is prime

factors = 0
n = int(input('Enter the number: '))
for i in range(1, n + 1):
    if n % i == 0:
        factors = factors + 1
if factors == 2:
    print(str(n) + " is prime.")
    print(str(n) + " is not prime.")

Python program to find the HCF of two numbers

num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
while num1 % num2 != 0:
    rem = num1 % num2;
    num1 = num2
    num2 = rem
print('HCF is ' + str(num2))

Python program to find the factorial of a number using a user-defined function

def factorial(num):
    f = 1
    for i in range(1, num + 1):
        f = f * i
    return f
num = int(input('Enter the number: '))
fact = factorial(num)
print('Factorial is ' + str(fact))

4 replies on “Python”

Sir,how to run a java program in sublime text 3? What is the path to run the java program in sublime text 3?

How to write a python program in sublime text 3? Can you show me in sublime text?
Sir,a program is given which states that:
WAP in python to accept a number and check whether a number is prime or not.

Leave a Reply