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

PythonLiveClass.ipynb - Colab

The document is a Python notebook containing various code snippets demonstrating basic programming concepts such as conditionals, loops, functions, and data structures. It includes examples of checking for even/odd numbers, calculating factorials, identifying prime numbers, and working with lists, tuples, and sets. Additionally, it showcases exception handling and generating random integers using NumPy.

Uploaded by

kolawaranushree
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)
7 views6 pages

PythonLiveClass.ipynb - Colab

The document is a Python notebook containing various code snippets demonstrating basic programming concepts such as conditionals, loops, functions, and data structures. It includes examples of checking for even/odd numbers, calculating factorials, identifying prime numbers, and working with lists, tuples, and sets. Additionally, it showcases exception handling and generating random integers using NumPy.

Uploaded by

kolawaranushree
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

5/14/25, 10:08 PM PythonLiveClass.

ipynb - Colab

num = 10
# this is my if block
if(num<10):
print("num is less than 10")

num1 = int(input("Enter the value of num1"))


print(num1)

Enter the value of num120


20

num = int(input("Enter the value of num"))


if(num%2==0):
print("num is even number")
elif(num==0):
print("num is euqals to 0")
else:
print("num is an odd number")

Enter the value of num0


num is even number

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


if(num<0):
print("Factorials does not exists for negative number")
elif(num==0):
print("Factorial of 0 is 1")
else:
factorial = 1
for i in range(1,num+1):
factorial = factorial*i
print(f"The factorial of {num} is {factorial}")

Enter the number5


The factorial of 5 is 120

num = int(input("Enter the value of num"))


for i in range(1,num+1):
if(i%2!=0):
print(i)

Enter the value of num20


1
3
5
7
9
11
13
15
17
19

flag = True
num = int(input("Enter the number"))
for i in range(2,num):
if(num%i==0):
flag = False
https://colab.research.google.com/drive/1xJluAxLnEhzmmTe07txc0RhV-u_a69FY#printMode=true 1/6
5/14/25, 10:09 PM PythonLiveClass.ipynb - Colab
break
if(flag):
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")

Enter the number17


17 is a prime number

n = int(input("Enter the value of n"))


#loop for range 1 to n
for i in range(1,n+1):
flag = True
#loop to check prime number
for j in range(2,i):
if(i%j==0):
flag = False
break
if(flag):
print(i)

Enter the value of n20


1
2
3
5
7
11
13
17
19

def palincheck(userstring):
if(userstring == userstring[::-1]):
print(f"{userstring} is a palindrome")
else:
print(f"{userstring} is not a palindrome")
userinput = input("Enter the string")
userinput.lower()
palincheck(userinput)

Enter the stringmam


mam is a palindrome

num = int(input("Enter the value of num"))


temp_num = num
sum = 0
while num>0:
digit = num%10
sum = sum + digit
num = num//10
print(f"Sum of digit of {temp_num} is {sum} ")

Enter the value of num123


Sum of digit of 123 is 6

num = int(input("Enter the value of num"))


a = 0
b = 1
for i in range(num):
print(a)
https://colab.research.google.com/drive/1xJluAxLnEhzmmTe07txc0RhV-u_a69FY#printMode=true 2/6
5/14/25, 10:09 PM PythonLiveClass.ipynb - Colab
c = a+b
a = b
b = c

Enter the value of num8


0
1
1
2
3
5
8
13

mylist = [10,20,30,40,50]
print("Length of my list :",len(mylist))
for i in mylist:
print(i)

Length of my list : 5
10
20
30
40
50

mylist = [10,20,30,40,50]
print("Largest element : ",max(mylist))
print("Smallest element : ",min(mylist))

Largest element : 50
Smallest element : 10

n = int(input("Enter the number of elements :"))


mylist =[]
for i in range(n):
element = input(f"Enter {i+1} element")
mylist.append(element)
mylist.sort()
print(mylist)

Enter the number of elements :5


Enter 1 element10
Enter 2 element60
Enter 3 element0
Enter 4 element80
Enter 5 element70
['0', '10', '60', '70', '80']

num = list(map(int,input("Enter numbers separated by space").split()))


print("Largest element : ",max(mylist))
print("Smallest element : ",min(mylist))

Enter numbers separated by space10 20 30 40 50


Largest element : 50
Smallest element : 10

mytuple1 = (10,20,30,40,50)
print(mytuple1)
print("Largest element : ",max(mytuple1))
print("Smallest element : ",min(mytuple1))

https://colab.research.google.com/drive/1xJluAxLnEhzmmTe07txc0RhV-u_a69FY#printMode=true 3/6
5/14/25, 10:09 PM PythonLiveClass.ipynb - Colab

print(mytuple1)

del mytuple1

print(mytuple1)

(10, 20, 30, 40, 50)


Largest element : 50
Smallest element : 10
(10, 20, 30, 40, 50)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-44-3512bf41e09a> in <cell line: 0>()
8 del mytuple1
9
---> 10 print(mytuple1)

NameError: name 'mytuple1' is not defined

mytuple1 = (10,20,30)
mylist = list(mytuple1)
mylist.append(40)

print(mylist)

[10, 20, 30, 40]

tuple1 = tuple(input("Enter the elements of first tuple separated by space").split())


tuple2 = tuple(input("Enter the elements of second tuple separated by space").split())

print("Before swapping :")


print("Tuple 1 :",tuple1)
print("Tuple 2 :",tuple2)

# swapping of values

tuple1,tuple2 = tuple2,tuple1
print("After swapping :")
print("Tuple 1 :",tuple1)
print("Tuple 2 :",tuple2)

Enter the elements of first tuple separated by space10 20 30 40 50


Enter the elements of second tuple separated by space60 70 80 90 100
Before swapping :
Tuple 1 : ('10', '20', '30', '40', '50')
Tuple 2 : ('60', '70', '80', '90', '100')
After swapping :
Tuple 1 : ('60', '70', '80', '90', '100')
Tuple 2 : ('10', '20', '30', '40', '50')

myset1 = set([10,20,30,40,50])
myset2 = set([50,60,70,80,90])
print("Union")
resultset = myset1 | myset2
print(resultset)
print("Intersection")
resultset = myset1 & myset2
print(resultset)
print("Difference")
resultset = myset1.difference(myset2)
print(resultset)
https://colab.research.google.com/drive/1xJluAxLnEhzmmTe07txc0RhV-u_a69FY#printMode=true 4/6
5/14/25, 10:09 PM PythonLiveClass.ipynb - Colab
print("Symmetric Difference")
resultset = myset1.symmetric_difference(myset2)
print(resultset)

Union
{70, 40, 10, 80, 50, 20, 90, 60, 30}
Intersection
{50}
Difference
{40, 10, 20, 30}
Symmetric Difference
{70, 10, 80, 20, 90, 30, 40, 60}

class student:
def __init__(self):
self.name=""
self.roll=""
self.dept=""
self.mob=""
def read_info(self):
self.name = input("Enter the name")
self.roll = input("Enter the roll")
self.dept = input("Enter the dept")
self.mob = input("Enter the mobile")
def display_info(self):
print("Student information - ")
print("Name : ",self.name)
print("Roll : ",self.roll);
print("Dept : ",self.dept);
print("Mobile : ",self.mob);

obj = student()
obj.read_info()
obj.display_info()

Enter the nameprince


Enter the roll23
Enter the deptco
Enter the mobile29912922
Student information -
Name : prince
Roll : 23
Dept : co
Mobile : 29912922

num1 = int(input("Enter the first number"))


num2 = int(input("Enter the second number"))
try:
result = num1/num2
except ZeroDivisionError:
print("Cannot divided by zero")
else:
print("Result = ",result)

Enter the first number10


Enter the second number2
Result = 5.0

class myexception(Exception):
pass
https://colab.research.google.com/drive/1xJluAxLnEhzmmTe07txc0RhV-u_a69FY#printMode=true 5/6
5/14/25, 10:09 PM PythonLiveClass.ipynb - Colab

def checkage(userage):
if userage<18:
raise myexception("Age should be less than 18")
else:
print("Welcome")
try:
age = int(input("Enter the age"))
checkage(age)
except myexception as e:
print("User defined error",e)

---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-61-009e485564aa> in <cell line: 0>()
8 print("Welcome")
9 try:
---> 10 age = int(input("Enter the age"))
11 checkage(age)
12 except myexception as e:

1 frames
/usr/local/lib/python3.11/dist-packages/ipykernel/kernelbase.py in _input_request(self, prompt,
ident, parent, password)
1217 except KeyboardInterrupt:
1218 # re-raise KeyboardInterrupt, to truncate traceback
-> 1219 raise KeyboardInterrupt("Interrupted by user") from None
1220 except Exception:
1221 self.log.warning("Invalid Message:", exc_info=True)

KeyboardInterrupt: Interrupted by user

import numpy as np
random_integers = np.random.randint(10,51,15)
print("Random Integers between 10 and 50",random_integers)

Random Integers between 10 and 50 [34 45 33 12 38 12 42 39 47 30 18 43 21 21 25]

https://colab.research.google.com/drive/1xJluAxLnEhzmmTe07txc0RhV-u_a69FY#printMode=true 6/6

You might also like