Categories
Class 12

Python

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

To find the length of a string:
len("hello")
To concatenate two strings:
'hello' + 'world'
To search a substring:
'welcome'.find('come')
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.")
else:
    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