0% found this document useful (0 votes)
11 views6 pages

Divyanshiexp 2 Py

Uploaded by

janvijain996
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)
11 views6 pages

Divyanshiexp 2 Py

Uploaded by

janvijain996
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/ 6

2/7/24, 1:34 PM Python_Experiment_3 - Jupyter Notebook

In [1]: # WAP to print the sum of the first n natural numbers using for loop
n=int(input("Enter the value of n:"))
s = 0
for i in range(n+1):
s+=i
print("Sum of",n,"natural numbers is:",s)

Enter the value of n:23


Sum of 23 natural numbers is: 276

In [3]: def coevenodd(P, Q):


ec = 0
oc = 0

for num in range(P, Q + 1):
if num % 2 == 0:
ec += 1
else:
oc+= 1

return ec, oc
P = int(input("Enter the value of P: "))
Q = int(input("Enter the value of Q: "))

even, odd = coevenodd(P, Q)

print("Number of even numbers:",even)
print("Number of odd numbers:",odd)

Enter the value of P: 23


Enter the value of Q: 43
Number of even numbers: 10
Number of odd numbers: 11


localhost:8888/notebooks/Desktop/python practice/Python_Experiment_3.ipynb# 1/6


2/7/24, 1:35 PM Python_Experiment_3 - Jupyter Notebook

In [13]: # Write a program to check entered number is prime or not (make use of break).
def prime(n):
if n < 2:
return False

for i in range(2, n):
if n % i == 0:
return False
break

return True


n= int(input("Enter a number to check if it's prime: "))


if prime(n):
print(n,"is a prime number.")
else:
print(n,"is not a prime number.")

Enter a number to check if it's prime: 7854


7854 is not a prime number.

In [4]: # Display the Fibonacci sequence up to nth term where n is provided by the use
n = int(input("Enter the number of terms:"))

n1, n2 = 0, 1
count = 0

if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence upto",n,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < n:
print(n1,sep=" ",end=" ")
nth = n1 + n2
n1 = n2
n2 = nth
count += 1

Enter the number of terms:34


Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 1
7711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 35
24578

localhost:8888/notebooks/Desktop/python practice/Python_Experiment_3.ipynb# 2/6


2/7/24, 1:35 PM Python_Experiment_3 - Jupyter Notebook

In [5]: """ Print the following pattern using nested loop.


*
* * *
* * * * * """
r=int(input("Enter the number of rows:"))
for i in range(1, r+1 ):
for j in range(1,r-i+1): # loop for spaces
print(" ",end="")
for k in range(1,2*i):
print("*",end="")
print()

Enter the number of rows:5


*
***
*****
*******
*********

In [6]: # WAP to traverse string using for loop


s=str(input("Enter a string: "))
for elements in s:
print(elements)

Enter a string: 34
3
4

In [7]: # WAP to traverse string using while loop


s=str(input("Enter a string: "))
i = 0
while i < len(s):
print(s[i])
i = i + 1

Enter a string: hello world


h
e
l
l
o

w
o
r
l
d

localhost:8888/notebooks/Desktop/python practice/Python_Experiment_3.ipynb# 3/6


2/7/24, 1:35 PM Python_Experiment_3 - Jupyter Notebook

In [8]: # Demonstrate 5 string operations using string function


s1 = input("Enter the first string: ")
s2 = input("Enter the second string: ")
# Compare Two Strings
if s1 == s2:
print("Both strings are equal.")
else:
print("Strings are not equal.")

# Join Two or More Strings
r=s1+s2
print(r)

# Iterate Through a Python String
for i in s1:
print(i,end=" ")

print()
for i in s2:
print(i,end=" ")

print()
# Python String Length
print("Length of first string is:",len(s1))
print("Length of second string is:",len(s2))

# String Membership Test
print("a" in s1)
print("p" not in s2)

# Methods of Python String
print(s1.upper())
print(s2.lower())
print(s1.partition('e'))
print(s2.replace('i','O'))

Enter the first string: hello


Enter the second string: world
Strings are not equal.
helloworld
h e l l o
w o r l d
Length of first string is: 5
Length of second string is: 5
False
True
HELLO
world
('h', 'e', 'llo')
world

localhost:8888/notebooks/Desktop/python practice/Python_Experiment_3.ipynb# 4/6


2/7/24, 1:35 PM Python_Experiment_3 - Jupyter Notebook

In [27]: # Demonstrate using continue statement in loop


n=input("Enter a string:")
for i in n:
if(i=='e' or i=='a' or i=='o' or i=='u' or i=='i'):
continue
print(i,end='')

Enter a string:python is fun


pythn s fn

In [9]: # Demonstrate use of Pass in loop and else with loop


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

for i in range(1,n+1):
if i==5:
pass
print(i)

else:
print("No items are left")

Enter a number:23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
No items are left

In [ ]: ​

localhost:8888/notebooks/Desktop/python practice/Python_Experiment_3.ipynb# 5/6


2/7/24, 1:35 PM Python_Experiment_3 - Jupyter Notebook

localhost:8888/notebooks/Desktop/python practice/Python_Experiment_3.ipynb# 6/6

You might also like