0% found this document useful (0 votes)
21 views

Study Metrics

This document contains code snippets and explanations related to Python programming concepts like loops, functions, lists, dictionaries, conditionals, strings, and matplotlib plotting. It covers topics like Fibonacci series, numeric patterns, character patterns, checking if a number is in the Fibonacci series, palindrome checking, file handling, factorial calculation, checking leap years, and plotting with matplotlib. The document appears to be notes from a Python lab manual covering a variety of fundamental Python programming concepts.

Uploaded by

shubham tapaniya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Study Metrics

This document contains code snippets and explanations related to Python programming concepts like loops, functions, lists, dictionaries, conditionals, strings, and matplotlib plotting. It covers topics like Fibonacci series, numeric patterns, character patterns, checking if a number is in the Fibonacci series, palindrome checking, file handling, factorial calculation, checking leap years, and plotting with matplotlib. The document appears to be notes from a Python lab manual covering a variety of fundamental Python programming concepts.

Uploaded by

shubham tapaniya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

LD COLLEGE OF ENGINEERING

Name: Shubham tapaniya

Semester: 5th

Enrolment no.: 210280111119

Subject: Python lab manual

Branch: Electronics and Communication


1) Fibonacci series:
a=0
b=1
print(a)
print(b)
for x in range(0,9):
c=a+b
print(c)
a,b=b,c

2) Check whether the number is in fibonacci series or not


n = int(input("Enter any number: "))

a=0
b=1
c=1

if n==0 or n==1:
print("Yes")
else:
while c < n:
c=a+b
a=b
b=c
if n == c:
print("Yes")
else:
print("No")

3) Fibonacci series check using 5n*2+4 0r 5n*2+2

def value(n):
x = int(n**0.5)
return x*x == n

def fibonacci(num):
if num < 0:
print("Give positive number")
else:
return value(5*num*num+4 or 5*num*num-4)

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


if fibonacci(num):
print("It is in fibonacci series")
else:
print("It is not in fibonacci series")

4-A) Program for printing right angle pyramid pattern


using stars.

n = int(input("Enter any number: "))


for i in range(0 , n):
for j in range(0, i+1):
print("*", end="")
print()

4-B) Python Program for printing reverse right angle


pyramid pattern using stars.

n=5
i=0

while(i<=n):
print(" " * (n - i) +"*" * i)
i += 1
4-c) Python Program to print a Triangle
num = int(input("Enter the range: "))

for i in range(num): # for height


for j in range((num - i) - 1):
print(end=" ") # for printing spaces
for j in range(i + 1):
print("*", end=" ") # for printing star pattern
print()

5-A) Python Numeric Pattern

k = 1

# For number of lines to be printed


for i in range(0, 5):
# Iterates from 0 to current value of i
for j in range(0, i+1):
# Print the current value of k with a space
print(k, end=" ")
# Increment k by 1 for the next iteration
k = k + 1

print()

5-B) Python Numeric Pattern

for i in range(0, 5):


num = 1
for j in range(0, i+1):
print(num, end=" ")
num = num + 1
print()
5-C) Python Numeric Pattern

for num in range(10):


for i in range(num):
print (num, end=" ")
print("\n")
6-A) Python Character Pattern
value = 65
for i in range(0, 5):
for j in range(0, i+1):
ch = chr(value)
print(ch, end=" ")
value = value + 1
print()

6-B) Python Character Pattern

value = 65
for i in range(0, 5):
for j in range(0, i+1):
ch = chr(value)
print(ch, end=" ")
value = value + 1
print()
7-A) Using for loop create a list which contains
common elements in them.

list1=['apple', 'banana', 'cherry']


list2=[2,3,'banana','apple']
common_elements=[]
for items in list1:
if items in list2:
common_elements.append(items)
print(common_elements)

7-B) Using set

list1=['apple', 'banana', 'cherry']


list2=[2,3,'banana','apple']
y=set(list1)
z=set(list2)
a=y.intersection(z)
b=list(a)
print(b)

8) Print 'PYTHON' in a pattern


v="python"

for i in range(len(v)+1):

print(v[0:i])

9) Palindrome:
 First method;
v="abba"
if v==v[::-1]:
print('yes')
else:
print('no')

 second method;
v="qabccbaq"
l=int(len(v)/2)
x=1
for i in range(l):

if (v[i]==v[-(i+1)]):
x=1
else:
x=0

break
if (x==1):
print('palyndrome')
else:
print('not palyndrome')
10) Print the square value of the given number using
dictionary.

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


dict1 = {}
for i in range(1,num+1):
dict1[i] = i**2
print(dict1)

11) Using dictionary ,print how many times a


character is repeated in a string

a= "Today is a beautiful day."


dict1 = {}
for i in a:
dict1[i] = a.count(i)
print(dict1)
OR
a= "Today is a beautiful day."
dict1 = {}
for i in a:
if i in dict1:
v = dict1[i]
v=v+1
dict1[i]=v
else:
dict1 [i]=1
print(dict1)
s= input ('Enter the number').split(',')
l= list(map(int(input('Enter a number').split(','))))
D={}
count=0
for i in s:
t=i.split(':')
D[int(t[o])] = int(t[1])

for val in l:
count+=val
if count in D:
count=D[count]

if count>=100:
print('succeed')
else:
print('failed')

12) Enter 3 values by the user which are three sides of


triangle and identify whether it is right angled triangle or
not. and take inputs in list.
a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
c = float(input("Enter the length of side c: "))

if (a**2 + b**2 == c**2 or a**2 + c**2 == b**2 or a**2 == b**2+ c**2):


print("the triangle is a right angled triangle")
else :
print("the triangle is not a right angled triangle ")
13)Using bubble sort check a right angle triangle

a = float(input("Enter the value of first side: "))


b = float(input("Enter the value of first side: "))
c = float(input("Enter the value of first side: "))

list = [a,b,c]

for i in range (len(list)):


for j in range (i+1,len(list)):
if list[i]>list[j]:
list[i],list[j] = list[j],list[i]

if list[2]**2 == list[0]**2 + list[1]**2:


print("Right angle")
else:
print("Not a right angle")
14) The given string is a pangram or not.
A = input("Enter a string: ").lower()
B = set(A) >= set('abcdefghijklmnopqrstuvwxyz')
if B:
print("The input is a pangram." )
else :
print("The input is not a pangram”)

15) The given string is a anagram or not.


a = "savai"
b = "vasi"
c =list(a)
d = list(b)
c.sort()
d.sort()
x= set(c)
y=set(d)
print(x)
print(y)
if x==y:
print("They are anagrams")
else:
print("No they are not anagrams")

16) Open the file "demofile2.txt" and append content to the file.

f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()

#open and read the file after the appending:


f = open("demofile2.txt", "r")
print(f.read())

16) Check if file exists, then delete it

import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")

17) Find out that given number is perfect cuberoot or not.


num = int(input("enter the number"))
a=num**(1/3)
b =(a**3)

print(a,b)
if(b==num):
print("yes this is the perfect cuberoot")
else:
print("no this is not a perfect cuberoot")

18) check the number is devisible by 2 and 3 or not


num =int(input("Enter the number :"))

if((num%2==0) and (num%3==0)):


print("The number is divisible by 2 and 3")
elif((num%2==0) and (num%3!=0)):
print("The number is only divisible by 2")
elif((num%2!=0) and (num%3==0)):
print("The number is only divisible by 2")
else:
print("the number is not divisible by both 2 anad 3")

19) find out the factorial of the given number.


a = int(input("enter the number"))
def fact(a):
if(a==1):
return 1
elif(a==0):
return 1
else:
return a*fact(a-1)
print(fact(a))

20) Check whether the year is leap year or not.


num =int(input("enter the year:"))

if(num%400 == 0 or (num%4 == 0 and num%100 != 0)):


print("this is a leap year")
else:
print("this is not a leap year")
MATPLOTLIB
1)PLOT X AND Y POINTS.
import sys
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 8])


ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

2)PLOT X AND Y POINTS WITH ‘:’ LINE


import sys
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linestyle = 'dotted')


plt.show()

plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

3)USING SCATTER
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])

plt.scatter(x, y)
plt.show()

plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

4)USING GRID
import sys
import matplotlib
matplotlib.use('Agg')

import numpy as np
import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x, y)
plt.grid()
plt.show()
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

5)USING BARS
import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x,y)
plt.show()
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

6)USING HISTOGRAM
import sys
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt


import numpy as np

x = np.random.normal(170, 10, 250)

plt.hist(x)
plt.show()
plt.savefig(sys.stdout.buffer)
sys.stdout.flush()

SNAKES AND LADDERS


1)COUNT NO. OF SNAKES AND LADDERS
INPUT=6:14,11:28,17:74,22:7,38:59,49:12,57:76,61:54
OUTPUT=(SNAKES) (LADDERS)
s= input().split(',')
d={}
snake=0
ladder=0
for i in s:
t=i.split(':')
d[int(t[0])]=int(t[1])
for key in d:
if d[value]>key:
ladder+=1
else:
snake+=1
print(str(snake)+''+string(ladder),end="")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy