python lab manual
python lab manual
PYTHON PROGRAMMING
LAB MANUAL
Week-1
1. i) Use a web browser to go to the Python website http://python.org. This
page contains information
about Python and links to Python-related pages, and it gives you the ability to
search the Python
documentation
In Python, webbrowser module is a convenient web browser controller. It
provides a high-level interface that allows displaying Web-based documents to
users.
webbrowser can also be used as a CLI tool. It accepts a URL as the argument
with the following optional parameters: -n opens the URL in a new browser
window, if possible, and -t opens the URL in a new browser tab.
Python
import webbrowser
webbrowser.open('http://www.python.org')
Output :
True
This opens the requested page using the default browser. To have a bit more
control over how the page gets opened, use one of the following functions given
below in the code –
Code #2 : Open the page in a new browser window.
Python3
webbrowser.open_new('http://www.python.org')
Output :
True
Code #3 : Open the page in a new browser tab.
Python3
webbrowser.open_new_tab('http://www.python.org')
Output :
True
These will try to open the page in a new browser window or tab, if possible and
supported by the browser. To open a page in a specific browser, use the
webbrowser.get() function to specify a particular browser.
Code #4 :
Python3
c = webbrowser.get('firefox')
c.open('http://www.python.org')
c.open_new_tab('http://docs.python.org')
Output :
True
True
Being able to easily launch a browser can be a useful operation in many scripts.
For example, maybe a script performs some kind of deployment to a server and
one would like to have it quickly launch a browser so one can verify that it’s
working. Or maybe a program writes data out in the form of HTML pages and
just like to fire up a browser to see the result. Either way, the webbrowser
module is a simple solution.
ii) Start the Python interpreter and type help() to start the online help utility.
The Python help() function invokes the interactive built-in help system. If the
argument is a string, then the string is treated as the name of
a module, function, class, keyword, or documentation topic, and a help page is
printed on the console. If the argument is any other kind of object, a help page on
the object is displayed.
Syntax:
help(object)
Parameters:
Return Vype:
Returns a help page.
The following displays the help on the builtin print method on the Python
interactive shell.
print(...)
flush: whether to forcibly flush the stream.on built-in function print in module
builtins:
>>> he'print'
streamAbove, the specified string matches with the built-in print function, so it
displays help on it.
>> help('math')
Help on built-in module math:
NAME
math
DESCRIPTION
This module is always available. It provides access to the
mathematical functions defined by the C standard.
FUNCTIONS
acos(x, /)
Return the arc cosine (measured in radians) of x.
acosh(x, /)
Return the inverse hyperbolic cosine of x.
asin(x, /)
Return the arc sine (measured in radians) of x.
asinh(x, /)
Return the inverse hyperbolic sine of x.
atan(x, /)
Return the arc tangent (measured in radians) of x.
-- More -->> help('math')
Help on built-in module math:
NAM-
Above, the specified string matches with the Math module, so it displays the
help of the Math functions. -- More -- means there is more information to display
on this by pressing the Enter or space key.
2. Start a Python interpreter and use it as a Calculator
return x + y
return x - y
return x * y
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
try:
except ValueError:
continue
if choice == '1':
if next_calculation == "no":
break
else:
print("Invalid Input")
output:
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 1
Output
133.0992000000001
ii. Given coordinates (x1, y1), (x2, y2) find the distance between two points
import math
p1 = [4, 0]
p2 = [6, 6]
distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) )
print(distance)
output:
6.324555320336759
4.Read name, address, email and phone number of a person through
keyboard and print the details.
name=input()
address=input()
email=input()
contactno=input()
print("name:",name)
print("address:",address)
print("email:",email)
print("contactno",contactno)
name: priyanka
address: knr
email: [email protected]
contactno 9502780251
Week – 2
n=5
for i in range(rows):
for j in range(i+1):
print(n, end="")
n=n-1
print("\n")
Output:
4 4
3 3 3
2 2 2 2
1 1 1 1 1
2.Write a program to check whether the given input is digit or
lowercasecharacter or uppercase character or a special character (use 'if-else-
if' ladder
elif(ch.isupper()):
elif(ch.islower()):
else:
print("The Given Character ", ch, "is Not a Lower or Uppercase Alphabet")
n1, n2 = 0, 1
count = 0
if nterms <= 0:
elif nterms == 1:
print(n1)
else:
print("Fibonacci sequence:")
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
output:
output:
import numpy as np
my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print(np.asarray(my_list))
print(np.asarray(my_tuple))
output:
List to array:
[1 2 3 4 5 6 7 8]
Tuple to array:
[[8 4 6]
[1 2 3]]
import numpy as np
ar2 = [1, 3, 4]
print(np.intersect1d(ar1, ar2))
output:[1 3 4]
2.Write a function called gcd that takes parameters a and b and returns their
greatest common divisor.
import math
# prints 12
print(math.gcd(60, 48))
# palindrome or not
def isPalindrome(str):
if str[i] != str[len(str)-i-1]:
return False
return True
# main function
s = "malayalam"
ans = isPalindrome(s)
if (ans):
print("Yes")
else:
print("No")
Output:
The gcd of 60 and 48 is : 12 Yes
else:
return False
# Driver Code
s = "MalaYaLam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")
output: Yes