Basic Programs:
Question : Write program to find compilation(pyc) file.
Program:
C:\Users\laptop>cd Desktop C:\Users\laptop\Desktop>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import py_compile >>> py_compile.compile("abc.py") '__pycache__\\abc.cpython-38.pyc' >>>
Output:
Question 2 Write program to find random number program
Program with Output
C:\Users\laptop>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import random >>> random.randint(1,100) 62 >>> random.randint(1,100) 41 >>> random.randint(1,100) 19 >>> random.randint(1,100) 49 >>> random.randint(1,100) 49
Question 3Writes program to make calculator
Program
print("Enter Two Number") a=int(input()) b=int(input()) print("***********************************") print("Sum ",a+b) print("***********************************") print("Sub ",a-b) print("***********************************") print("Mul ",a*b) print("***********************************") print("Div ",a/b) print("***********************************") print("Mod ",a%b)
Output
C:/Users/laptop/AppData/Local/Programs/Python/Python38-32/aaaaa.py Enter Two Number 45 44 *********************************** Sum 89 *********************************** Sub 1 *********************************** Mul 1980 *********************************** Div 1.0227272727272727 *********************************** Mod 1
Question 4 Write a program to swap the number without taking the third var
Program
a=int(input("A ")) b=int(input("B ")) a=a+b b=a-b a=a-b print("After swap") print("A",a) print("B",b)
Output
A 33 B 56 After swap A 56 B 33
Question 5 writes program to find area of Rectangle
Program
print("Give h and w ") a=int(input()) b=int(input()) c=a*b print("Area",c)
Output
Give h and w 45 44 Area 1980
Python Range Functions
range function returns the sequence of the number, by default start from the 0,and increment is default is 1, and we have to give a specific number to stop.
Program 1
a=range(1,10,1) >>> print(a) range(1, 10)
Program 2
>>> a=range(1,10,1) >>> for x in a: print(x) 1 2 3 4 5 6 7 8 9 >>>
Example of str() Method
>>> result=str(34) >>> print(result) 34 >>> type(result)>>>
Print example
>>> print("Hello I am using Print Statement") Hello I am using Print Statement
>>> print("Hello world") Hello world >>>
Example of Len function
>>> mystring="string" >>> l=len(mystring) >>> print(l) 6 >>>
>>> another_string="Hello How are You ?" >>> l=len(another_string) >>> print(l)
Example of ord()
>>> ord("A") 65 >>> ord("3") 51 >>> ord("@") 64 >>>
Example of chr()
>>> chr(22) '\x16' >>> chr(65) 'A' >>> chr(98) 'b' >>>
Example of print time
import datetime print(datetime.datetime.now()) 2020-08-03 14:28:43.284369
Example of Regular expression
import re text="salesforcedrillers" a= re.findall("ll", text) print(a)
Output
==== RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/abc.py === ['ll'] >>>
Program 2
import re text = "sales force drillers" y= re.search("^sales.*drillers$", text) print(y)
Output
<re.Match object; span=(0, 20), match='sales force drillers'>
Program 3
import re text = "sales force drillers team" y = re.split("\s", text) print(y)
Output
['sales', 'force', 'drillers', 'team']
Unittest example
Program
import unittest class First_test(unittest.TestCase): def check(self): self.assertTrue(True) if __name__ == '__main__': unittest.main()
Output
==== RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/abc.py === ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK