Basic Program
import math
a=int(input("Enter the first value"))
b=int(input("Enter the second value"))
c=int(input("Enter the third value"))
x = (-b-math.sqrt(b**2-4*a*c))/(2 * a)
y = (-b +math.sqrt(b**2-4*a*c))/(2 * a)
print("The value of x is",x)
print("The value of y is",y)
******************************************************
import cmath
a = 1
b = 4
c = 2
calculating the discriminant
dis = (b**2) - (4 * a*c)
find two results
ans1 = (-b-cmath.sqrt(dis))/(2 * a)
ans2 = (-b + cmath.sqrt(dis))/(2 * a)
Control Conditional Structure
program01****************************************
a=int(input("Enter the percentage : "))
if a>=90 :
print( " A+")
elif a>=80 and a<=89 :
print( " A")
elif a>=70 and a<=79 :
print( " B")
elif a>=60 and a<=69 :
print( " C")
elif a>=50 and a<=59 :
print( " D")
else :
print( " Fail")
program02*****************************************
a=int(input("Enter the Any Number : "))
if (a%2)==0 :
print( "Even")
else :
print("odd")
program03**************************************
a=int(input("Enter the Any Number : "))
if a>0 :
print( " Positive Number ")
if (a%2) == 0 :
print( "Even")
else :
print("odd")
else :
print( " zero or Negative " )
program04****************************************
user_name=str(input("Enter user name : "))
password=int(input("Enter password : "))
if user_name==" admin" and password== "12345" :
print(" Connected " )
else :
print(" Connection Failed")
program05*******************************************
password=input( " Enter any password : ")
if 20>len(password)>=8 :
print("length is valid ")
if " 1" in password or "2" in password :
print("the password has number")
else :
print("the password has no number")
else :
print("length is invalid ")
****************************************************
Loop Structure
for i in range(10):
print(i)
a=int(input(" Enter your Any Number : "))
for x in range(1,11,1):
print (a,"x",x,"=",x*a)
********************************
n = int (input ("Enter a number: "))
f= 1
if n >= 1:
for i in range (1,n+1):
f=f *i
print("Factorial of the given number is: ", f)
**********************************
list1 = [1,2,3,4,5]
even=0
odd=0
for x in list1:
if x % 2 == 0:
even += 1
else:
odd += 1
print("Expected Output : " )
print("Number of Even Numbers: ", even)
print("Number of Odd Numbers: ", odd)
*************************************
n=[3,5,23,6,5,1,2,9,8];
a=0
for i in n:
a+=i*i;
print ("The Sum Of Square is :",a)
****************************************************
Function
*args and **kwargs in Python
def a(*kids):
print("the youngest child is "+kids[2])
a("hina","hiba","kinza")
def dictionary(**abc):
for x in abc.items():
print("items are : \n ",x)
for y in abc.keys():
print("keys are : \n ",y)
for k in abc.values():
print("values are : \n ",k)
dictionary(a=1,b=2,c=3,d=4)
**********************************
import math
x=100 #global
def outer():
x=90 # enclosed
print(x)
def inner(): #local
#x=80
print(x)
print(math.pi)
inner()
outer()
**********************************
x=6
def changer():
global x
x+=1
print(x)
changer()
print(x)
**********************************
def f(x):
x=5
print(x)
a=3
f(a)
print(a)
def recur_factorial(n):
if n == 1:
return n
else :
return n*recur_factorial(n-1)
Python File Open
To open the file, use the built-in open()
function.
The open()
function returns a file object, which has a read()
method for reading the content of the file:
Q1
f=open('abdullah.txt')
print(f.read())
Q2
print(f.read(6))
print(f.read())
Q3
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
Q4
print(f.readlines())
print(f.readlines(16))
No comments:
Post a Comment