Python_Basics_1
Python_Basics_1
January 2, 2025
[ ]: a = int(input('enter a number:'))
print(type(a))
enter a number:3
<class 'int'>
[1]: print('Hello')
Hello
[ ]: a = True
b = 'hello'
print('a=',a,'hello',b)
print(b)
b
[ ]: 'hello'
[ ]: type(a)
[ ]: bool
[ ]: a =4
print(type(a))
<class 'int'>
Hello world
Good$morning$hello$world
1
[ ]: # WAP to input radius and print area of a circle
radius = float(input('Enter the radius:'))
area = round(3.14 * (radius ** 2),2)
print('Area=',area)
Enter a number:34563789
34563789 is odd
Enter a number:4
Not Divisible
2
else:
print(n1, n3, n2)
else:
if n1 < n2:
print(n3, n1, n2)
else:
print(n3, n2, n1)
[ ]: # WAP to check whether three given lengths of three sides form a right triangle.
''' Print yes if the given sides forms a right triangle, otherwise no '''
n1 = int(input('Enter the first length:'))
n2 = int(input('Enter the second length:'))
n3 = int(input('Enter the third length:'))
n1_s = n1 ** 2
n2_s = n2 ** 2
n3_s = n3 ** 2
if ( n1_s == n2_s + n3_s or n2_s == n1_s + n3_s or n3_s == n1_s + n2_s):
print('yes')
else:
print('no')
[ ]: # WAP to get the third side of the right angled triangle from two given sides
hyp = float(input('Enter the length of hypothness:'))
adj = float(input('Enter the length of adjacent side:'))
opp = float(input('Enter the length of opposite side:'))
if hyp == 0:
length = (adj ** 2 + opp ** 2) ** 0.5
elif adj == 0:
length = (hyp ** 2 - opp ** 2) ** 0.5
3
else:
length = (hyp ** 2 - adj ** 2) ** 0.5
print(length)
20
18
16
14
12
1
2
3
4
5
6
7
8
9
10
4
[ ]: password = 'random'
guess = input('Enter your guess:')
while(password != guess):
print('Wrong Password')
guess = input('Enter your guess:')
print('Correct')
[ ]: # WAP to input a number and print its sum of digits, E.g 123 = 1 + 2 + 3 = 6
sum_d = 0
n1 = int(input('Enter a number:'))
while(n1 > 0):
rem = n1 % 10
sum_d = sum_d + rem
n1 = n1 // 10
print(sum_d)
Enter a number:512
8
Enter a number:7
7 is prime number
5
if num == 1:
print(num, 'is not a prime number')
else:
while(i <= num/2):
if num % i == 0:
print(num, 'is not a prime number')
break
i = i + 1
else:
print(num, 'is a prime number')
Enter a number:19
19 is a prime number
[ ]: # Nested loop
for i in range(1,11):
for j in range(1,11):
print(i,j)
print()
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
3 1
3 2
3 3
3 4
3 5
3 6
6
3 7
3 8
3 9
3 10
4 1
4 2
4 3
4 4
4 5
4 6
4 7
4 8
4 9
4 10
5 1
5 2
5 3
5 4
5 5
5 6
5 7
5 8
5 9
5 10
6 1
6 2
6 3
6 4
6 5
6 6
6 7
6 8
6 9
6 10
7 1
7 2
7 3
7 4
7 5
7 6
7 7
7 8
7 9
7 10
7
8 1
8 2
8 3
8 4
8 5
8 6
8 7
8 8
8 9
8 10
9 1
9 2
9 3
9 4
9 5
9 6
9 7
9 8
9 9
9 10
10 1
10 2
10 3
10 4
10 5
10 6
10 7
10 8
10 9
10 10
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
8
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
9
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
17
19
23
29
[ ]: # String Operations
string = 'hello World'
print(string[1:4:2])
el
dlroW olleh
[ ]: # String Functions
print(len(string))
print(string.capitalize())
print(string.isalpha())
print(string.isdigit())
print(string.isspace())
print(string.isalnum())
print(string.lower())
print(string.upper())
print(string.islower())
print(string.isupper())
print(string.find('World'))
print(string.find('world'))
11
print(string.find('World',6,11))
11
Hello world
False
False
False
False
hello world
HELLO WORLD
False
False
6
-1
6
[ ]: # WAP to count the number of letters, numbers and special characters in a string
count1 = count2 = count3 = 0
string = 'hello123*'
for i in string:
if i.isalpha():
count1 = count1 + 1
elif i.isdigit():
count2 = count2 + 1
else:
count3 = count3 + 1
print(count1, count2, count3)
5 3 1
[ ]: # WAP to convert uppercase letters into lowercase and lowercase letters into␣
↪uppercase and then display the complete string
hELLO wORLD
[ ]: ''' WAP to input a string like this --> 32,478.90. You have to replace the␣
↪comma with the dot ang the dot with the coma, and
12
string = '32,478.90.'
str2 = ''
for i in string:
if i == ',':
str2 = str2 + '.'
elif i == '.':
str2 = str2 + ','
else:
str2 = str2 + i
print(str2)
32.478,90,
[ ]: # list
lst = [ 12, 'a', 12.3, True, 3+4j ]
lst = list((12, 'a', 12.3, True, 3+4j))
print(lst)
[ ]: for i in lst:
print(i)
12
a
12.3
True
(3+4j)
[ ]: lst1 =[1, 2, 3, 4, 5]
lst2 =[ i ** 2 for i in lst1 ]
print(lst2)
13
print(lst1)
[ ]: # List slicing
lst = [1,2,3,4,5]
lst[0:3] = 'a'
print(lst)
['a', 4, 5]
[ ]: # list operations
lst = [ 1, 2, 3, 4]
lst1 = [ 'a', 'b', 'c']
print(lst+lst1)
[ ]: print(lst * 3)
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
True
False
[ ]: # List functions
print(len(lst1))
lst1.append(56)
[ ]: del lst1[2]
[ ]: print(lst1)
[ ]: lst1.pop(2)
14
[ ]: 56
[ ]: # WAP to create a list having some elements, filter only unique elements in a␣
↪list to a new list
[ ]: string = 'hello'
list(string)
[ ]: lst = [ 1, 2, 2, 4, 3, 4]
lst.count(2)
lst.index(1)
lst.insert(3,'a')
lst.remove('a')
lst.sort(reverse=True)
# l = sorted(lst, reverse=True)
[ ]: lst.sort()
lst
[ ]: [1, 2, 2, 3, 4, 4]
[ ]: l
[ ]: [1, 2, 2, 3, 4, 4]
[ ]: # Tuples
t = (1,2,3,'a',True,3+4j)
print(t)
[ ]: tu = tuple((12,45,4.6,'a',True))
print(tu)
[ ]: for i in t:
print(i)
15
1
2
3
a
True
(3+4j)
[ ]: # Tuple Unpacking
a,b,c,d,e = (1,2,3,4,5)
print(a,b,c,d,e)
1 2 3 4 5
[ ]: # Dictonaries
d = {'a':1,'b':2,'c':3,(1,2):[3,4],45:'b'}
print(d)
[ ]: d.keys()
[ ]: d.values()
[ ]: d.items()
[ ]: dict_items([('a', 1), ('b', 2), ('c', 3), ((1, 2), [3, 4]), (45, 'b')])
[ ]: for i in d:
print(i,'->',d[i])
a -> 1
b -> 2
c -> 3
(1, 2) -> [3, 4]
45 -> b
16
[ ]: d = {}
key = input('Enter the key:')
val = input('Enter the value:')
d[key] = val
print(d)
[ ]: ''' WAP to search for a value in dictionary. if the values exists then display␣
↪it along with its key otherwise display value
[ ]: # Nested Dictonary
emp = {'john':{'age':24,'salary':12000},'rohan':{'age':25,'salary':34000}}
# print(emp['john']['salary'])
for i in emp:
print(i,'has',end=' ')
for j,k in emp[i].items():
print(j,k,end=' ')
john has age 24 salary 12000 rohan has age 25 salary 34000
[ ]: # Functions
def demo():
print('Hello World')
17
[ ]: demo()
Hello World
[ ]: def demo(name):
print(name)
[ ]: demo('Hello')
Hello
[ ]: def demo(name):
return name
[ ]: result = demo('naimu')
print(result)
naimu
[ ]: # WAP to create a function having 3 values in the argument and return the␣
↪highest value among them
def func1(x,y,z):
return max(x,y,z)
[ ]: res = func1(4,2,5)
print(res)
[ ]: # WAP to create a function having a list as an argument and print the unique␣
↪elements from it
def func2(l1):
l2 = []
for i in l1:
if i not in l2:
l2.append(i)
return l2
[ ]: l = [4,5,6,4,2,1,5,2,7,8,2,8,3,6,4]
res = func2(l)
print(res)
[4, 5, 6, 2, 1, 7, 8, 3]
18
[ ]: demo()
1
arun
[ ]: name = 'arun'
[ ]: def demo():
global name
name = 'Hello' + name
print(name)
[ ]: demo()
Helloarun
def details(name='arun'):
print(name)
[ ]: details('aman')
aman
[ ]: def demo(*args):
print(*args)
[ ]: demo(1,2,3,4,5,'hello')
1 2 3 4 5 hello
[ ]: # Module:
import random
random.random()
[ ]: 0.7354155581000072
[ ]: random.randint(2,6)
[ ]: 2
[ ]: random.random()*(30-15)+15
[ ]: 26.625588408902836
19
[ ]: # WAP to print 10 random floating-point numbers from 20 to 30
import random
for i in range(10):
print(random.random()*(30-20)+20)
27.06573738265071
27.169379844196968
27.013183698272403
27.877626150860042
20.65026150622552
21.358073131479355
21.282691997252158
23.966268490397585
20.966318985027975
28.856511665913104
8
7
[ ]: print(math.sqrt(7))
print(math.pow(2,3))
2.6457513110645907
8.0
[38]: l = [3,4,5]
[42]: list((map(even_odd,l)))
[40]: list((filter(even_odd,l)))
[40]: [4]
[ ]:
20