Sunteți pe pagina 1din 23

CONTENT

1. Program to find the area of a triangle

2. Program to generate a random number

3. Program to convert kilometers to miles

4. Program to check if a Number is Positive, Negative or Zero


5. Program to Check if a Number is Odd or Even

6. Program to Check Leap Year

7. Program to Check Prime Number

8. Program to Print all Prime Number between an Interval


9. Program to Find the Factorial of a Number

10. Program to Display the multiplication Table

11. Program to Print the Fibonacci sequence


12. Program to Check Armstrong Number

13. Program to Find the Sum of Natural Numbers

14. Program to Find LCM

15. Program to Find HCF

16. Program to Display Fibonacci Sequence Using Recursion


17. Program to Convert Decimal to Binary, Octal and Hexadecimal
18. Program To Find ASCII value of a character

19. Program to Find Factorial of Number Using Recursion

20. Program to Sort Words in Alphabetic Order

21. Program to Remove Punctuation from a String

22. CREATING DATABASE TABLE STUDENTS


23. INSERTING NEW ROW

24. CREATING DATABASE TABLE EMPLOYEE USING PYTHON

25. INSERT STATEMENT TO CREATE A RECORD INTO EMPLOYEE TABLE

26. PYTHON PROGRAM TO IMPLEMENT THE WEB SERVER TO WRITE USER DATA TO A CSV FILE
1. Program to find the area of a triangle
1. # Three sides of the triangle is a, b and c:

2. a = float(input('Enter first side: '))

3. b = float(input('Enter second side: '))

4. c = float(input('Enter third side: '))

5.

6. # calculate the semi-perimeter

7. s = (a + b + c) / 2

8.

9. # calculate the area

10. area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

11. print('The area of the triangle is' area)

2. Program to generate a random number


import random

print(random.randint(100,500))
3. Program to convert kilometers to miles

1. # Collect input from the user

2. kilometers = float(input('How many kilometers?: '))

3. # conversion factor

4. conv_fac = 0.621371

5. # calculate miles

6. miles = kilometers * conv_fac

7. print('%0.3f kilometers is equal to %0. 3f miles' % (kilometers,miles))

4. Program to check if a Number is Positive,


Negative or Zero

1. num = float(input("Enter a number: "))


2.

3. if num > 0:

4. print("{0} is a positive number".format(num))

5. elif num == 0:

6. print("{0} is zero".format(num))

7. else:

8. print("{0} is negative number".format(num))

5. Program to Check if a Number is Odd or


Even
1. num = int(input("Enter a number: "))

2. if (num % 2) == 0:

3. print("{0} is Even number".format(num))

4. else:

5. print("{0} is Odd number".format(num))

6.Program to Check Leap Year

1. year = int(input("Enter a year: "))

2. if (year % 4) == 0:

3. if (year % 100) == 0:

4. if (year % 400) == 0:

5. print("{0} is a leap year".format(year))

6. else:

7. print("{0} is not a leap year".format(year))


8. else:

9. print("{0} is a leap year".format(year))

10. else:

11. print("{0} is not a leap year".format(year))

7.Program to Check Prime Number

1. num = int(input("Enter a number: "))

2.

3. if num > 1:

4. for i in range(2,num):

5. if (num % i) == 0:

6. print(num,"is not a prime number")

7. print(i,"times",num//i,"is",num)

8. break

9. else:

10. print(num,"is a prime number")

11.

12. else:
13. print(num,"is not a prime number")

8.Program to Print all Prime Numbers


between an Interval
1. #Take the input from the user:

2. lower = int(input("Enter lower range: "))

3. upper = int(input("Enter upper range: "))

4. for num in range(lower,upper + 1):

5. if num > 1:

6. for i in range(2,num):

7. if (num % i) == 0:

8. break

9. else:
10. print(num)

9.Program to Find the Factorial of a


Number

1. num = int(input("Enter a number: "))

2. factorial = 1

3. if num < 0:

4. print("Sorry, factorial does not exist for negative numbers")

5. elif num == 0:

6. print("The factorial of 0 is 1")

7. else:

8. for i in range(1,num + 1):

9. factorial = factorial*i

10. print("The factorial of",num,"is",factorial)


10.Program to Display the multiplication
Table
1. num = int(input("Show the multiplication table of? "))

2. # using for loop to iterate multiplication 10 times

3. for i in range(1,11):

4. print(num,'x',i,'=',num*i)
11.Program to Print the Fibonacci sequence
1. nterms = int(input("How many terms you want? "))

2. if nterms <= 0:

3. print("Plese enter a positive integer")

4. elif nterms == 1:

5. print("Fibonacci sequence:")

6. print(n1)

7. else:

8. print("Fibonacci sequence:")

9. print(n1,",",n2,end=', ')

10. while count < nterms:

11. nth = n1 + n2

12. print(nth,end=' , ')

13. # update values

14. n1 = n2

15. n2 = nth

16. count += 1
12.Program to Check Armstrong Number

17. num = int(input("Enter a number: "))

18. sum = 0

19. temp = num

20.

21. while temp > 0:

22. digit = temp % 10

23. sum += digit ** 3

24. temp //= 10

25.

26. if num == sum:

27. print(num,"is an Armstrong number")

28. else:

29. print(num,"is not an Armstrong number")


13.Program to Find the Sum of Natural
Numbers

1. num = int(input("Enter a number: "))

2. if num < 0:

3. print("Enter a positive number")

4. else:

5. sum = 0

6. # use while loop to iterate un till zero

7. while(num > 0):

8. sum += num

9. num -= 1

10. print("The sum is",sum)


14.Program to Find LCM
1. def lcm(x, y):

2. if x > y:

3. greater = x

4. else:

5. greater = y

6. while(True):

7. if((greater % x == 0) and (greater % y == 0)):

8. lcm = greater

9. break

10. greater += 1

11. return lcm

12. num1 = int(input("Enter first number: "))

13. num2 = int(input("Enter second number: "))

14. print("The L.C.M. of", num1,"and", num2,"is", lcm(num1, num2))


15.Program to Find HCF

1. def hcf(x, y):

2. if x > y:

3. smaller = y

4. else:

5. smaller = x

6. for i in range(1,smaller + 1):

7. if((x % i == 0) and (y % i == 0)):

8. hcf = i

9. return hcf

10.

11. num1 = int(input("Enter first number: "))

12. num2 = int(input("Enter second number: "))

13. print("The H.C.F. of", num1,"and", num2,"is", hcf(num1, num2


16.Program to Display Fibonacci Sequence
Using Recursion
1. def recur_fibo(n):

2. if n <= 1:

3. return n

4. else:

5. return(recur_fibo(n-1) + recur_fibo(n-2))

6. nterms = int(input("How many terms? "))

7. if nterms <= 0:

8. print("Plese enter a positive integer")

9. else:

10. print("Fibonacci sequence:")

11. for i in range(nterms):

12. print(recur_fibo(i))
17. Program to Convert Decimal to Binary,
Octal and Hexadecimal

1. dec = int(input("Enter a decimal number: "))

2. print(bin(dec),"in binary.")

3. print(oct(dec),"in octal.")

4. print(hex(dec),"in hexadecimal.")

18.Program To Find ASCII value of a


character
1. c = input("Enter a character: ")

2. print("The ASCII value of '" + c + "' is",ord (c))


19.Program to Find Factorial of Number
Using Recursion

1. def recur_factorial(n):

2. if n == 1:

3. return n

4. else:

5. return n*recur_factorial(n-1)

6. num = int(input("Enter a number: "))

7. if num < 0:

8. print("Sorry, factorial does not exist for negative numbers")

9. elif num == 0:

10. print("The factorial of 0 is 1")

11. else:

12. print("The factorial of",num,"is",recur_factorial(num))


20.Program to Sort Words in Alphabetic
Order

30. my_str = input("Enter a string: ")

31. # breakdown the string into a list of words

32. words = my_str.split()

33. # sort the list

34. words.sort()

35. # display the sorted words

36. for word in words:

37. print(word)
21. Program to Remove Punctuation from a
String

1. punctuation = '''''!()-[]{};:'"\,<>./?@#$%^&*_~'''

2. # take input from the user

3. my_str = input("Enter a string: ")

4. # remove punctuation from the string

5. no_punct = ""

6. for char in my_str:

7. if char not in punctuation:

8. no_punct = no_punct + char

9. # display the unpunctuated string

10. print(no_punct)
CREATING DATABASE TABLE STUDENTS

1. SQL> CREATE TABLE STUDENTS (

2. ID INT NOT NULL,

3. NAME VARCHAR (20) NOT NULL,

4. AGE INT NOT NULL,

5. ADDRESS CHAR (25),

6. PRIMARY KEY (ID)

7. );

8. SQL> DESC STUDENTS;

INSERTING NEW ROW

1. INSERT INTO CUSTOMERS

2. VALUES ( 1,ABHIRAM,22,ALLAHABAD);
CREATING DATABASE TABLE EMPLOYEE USING PYTHON

import MySQLdb

# Open database connection


db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

# prepare a cursor object using cursor() method


cursor = db.cursor()

# Drop table if it already exist using execute() method.


cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")

# Create table as per requirement


sql = """CREATE TABLE EMPLOYEE (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )"""

cursor.execute(sql)

# disconnect from server


db.close()

INSERT STATEMENT TO CREATE A RECORD INTO EMPLOYEE TABLE

import MySQLdb

# Open database connection


db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

# prepare a cursor object using cursor() method


cursor = db.cursor()

# Prepare SQL query to INSERT a record into the database.


sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()

# disconnect from server


db.close()

PYTHON PROGRAM TO IMPLEMENT THE WEB SERVER TO WRITE USER DATA TO A CSV FILE.

INPUT

OUTPUT

S-ar putea să vă placă și