Python_Basics_1
January 2, 2025
0.0.1 Python Basics
[ ]: 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
a= True hello hello
hello
[ ]: 'hello'
[ ]: type(a)
[ ]: bool
[ ]: a =4
print(type(a))
<class 'int'>
[ ]: print('Hello', end=' ')
print('world')
Hello world
[ ]: print('Good', 'morning', 'hello', 'world', sep='$')
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 the radius:4.3
Area= 58.06
[ ]: #WAP to input 3 marks and print their average
n1 = float(input('Enter the first mark:'))
n2 = float(input('Enter the second mark:'))
n3 = float(input('Enter the third mark:'))
average = (n1 + n2 + n3) / 3
print(average)
Enter the first mark:45
Enter the second mark:81
Enter the third mark:69
65.0
[ ]: # WAP to print the input number is even or odd
num = int(input('Enter a number:'))
if (num % 2 == 0):
print(num, 'is even')
else:
print(num, 'is odd')
Enter a number:34563789
34563789 is odd
[ ]: # WAP a program to check whether a number is divisible by 5 as well as 3
num = int(input('Enter a number:'))
if (num % 3 == 0 and num % 5 == 0):
print('It is divisible by 3 and 5')
else:
print('Not Divisible')
Enter a number:4
Not Divisible
[ ]: # WAP to input 3 numbers and arrange them into ascending order
n1 = int(input('Enter the First Number:'))
n2 = int(input('Enter the second Number:'))
n3 = int(input('Enter the third Number:'))
if n1 < n2 and n1 < n3:
if n2 < n3:
print(n1, n2, n3)
2
else:
print(n1, n3, n2)
elif n2 < n1 and n2 < n3:
if n1 < n3:
print(n2, n1, n3)
else:
print(n2, n3, n1)
else:
if n1 < n2:
print(n3, n1, n2)
else:
print(n3, n2, n1)
Enter the First Number:1
Enter the second Number:9
Enter the third Number:4
1 4 9
[ ]: # 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')
Enter the first length:3
Enter the second length:4
Enter the third length:5
yes
[ ]: # 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)
Enter the length of hypothness:0
Enter the length of adjacent side:3
Enter the length of opposite side:4
5.0
[6]: # for loop
for i in range(20,10,-2):
print(i)
20
18
16
14
12
[7]: # WAP to print even and odd numbers in the range of 10 to 20
sum_e = sum_o = 0
for i in range(10,21):
if i % 2 == 0:
sum_e += i
else:
sum_o = sum_o + i
print('Sum of even number:', sum_e)
print('Sum of odd number:', sum_o)
Sum of even number: 90
Sum of odd number: 75
[1]: # While loop
i = 1
while(i <= 10):
print(i)
i = i + 1
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')
Enter your guess:Random
Wrong Password
Enter your guess:genius
Wrong Password
Enter your guess:random
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
[ ]: # WAP to input a number and check if it is prime or not
num = int(input('Enter a number:'))
flag = 0
i = 2
if num == 1:
print(num, 'is not a prime number')
else:
while(i <= num/2):
if num % i == 0:
flag = 1
break
i = i + 1
if flag == 0:
print(num, 'is a prime number')
else:
print(num, 'is not a prime number')
Enter a number:7
7 is prime number
[ ]: # WAP to input a number and check if it is prime or not
num = int(input('Enter a number:'))
i = 2
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
[ ]: # WAP to print the table of all the numbers from 1 to 10
for i in range(1,11):
for j in range(1,11):
print(i, '*', j, '=', i * j)
print()
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
[ ]: # WAP to print the prime numbers from 15 to 30
flag = 0
for i in range(15,31):
for j in range(2, i):
if i % j == 0:
flag = 1
break
else:
flag = 0
if flag == 0:
print(i)
17
19
23
29
[ ]: # String Operations
string = 'hello World'
print(string[1:4:2])
el
[ ]: # Reverse the string using slicing
print(string[::-1])
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
string = 'Hello World'
str2 =''
for i in string:
if i.islower():
str2 = str2 + i.upper()
elif i.isupper():
str2 = str2 + i.lower()
else:
str2 = str2 + i
print(str2)
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
display the complete string '''
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)
[12, 'a', 12.3, True, (3+4j)]
[ ]: for i in lst:
print(i)
12
a
12.3
True
(3+4j)
[ ]: # WAP to print the square of all numbers in a list
lst = [1, 2, 3, 4, 5]
lst1= []
for i in lst:
lst1 = lst1 + [ i ** 2 ]
print(lst1)
[1, 4, 9, 16, 25]
[ ]: lst1 =[1, 2, 3, 4, 5]
lst2 =[ i ** 2 for i in lst1 ]
print(lst2)
[1, 4, 9, 16, 25]
[ ]: # WAP to filter only integers from a list
lst = [ 12, 'a', 12.3, True, 3+4j, 43, 67 ]
lst1 = []
for i in lst:
if type(i) == int:
lst1 = lst1 + [ i ]
13
print(lst1)
[12, 43, 67]
[ ]: # 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)
[1, 2, 3, 4, 'a', 'b', 'c']
[ ]: print(lst * 3)
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
[ ]: lst1 = [4, 6, 7, 12]
lst2 = [4, 6, 7, 9]
print( lst1 > lst2)
True
[ ]: lst1 = ['apple', 'bannana', 'mango']
lst2 = ['Apple', 'kiwi', 'orange']
print(lst1 < lst2)
False
[ ]: # List functions
print(len(lst1))
lst1.append(56)
[ ]: lst1.extend([ 20, 21, 'hello', True])
print(lst1)
['apple', 'bannana', 'mango', 56, 20, 21, 'hello', True]
[ ]: del lst1[2]
[ ]: print(lst1)
['apple', 'bannana', 56, 20, 21, 'hello', True]
[ ]: lst1.pop(2)
14
[ ]: 56
[ ]: # WAP to create a list having some elements, filter only unique elements in a␣
↪list to a new list
lst = [ 12, 'a', True, 12, 'b', 'c', 'a', True]
new_lst= []
for i in lst:
if i not in new_lst:
new_lst.append(i)
print(new_lst)
[12, 'a', True, 'b', 'c']
[ ]: string = 'hello'
list(string)
[ ]: ['h', 'e', 'l', 'l', 'o']
[ ]: 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)
(1, 2, 3, 'a', True, (3+4j))
[ ]: tu = tuple((12,45,4.6,'a',True))
print(tu)
(12, 45, 4.6, 'a', True)
[ ]: 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
[ ]: # Delete an element from a tuple
t1 = ()
t1 = t[0:3]
t2 = t[4:]
t3 = t1 + t2
print(t3)
(1, 2, 3, True, (3+4j))
[ ]: # Dictonaries
d = {'a':1,'b':2,'c':3,(1,2):[3,4],45:'b'}
print(d)
{'a': 1, 'b': 2, 'c': 3, (1, 2): [3, 4], 45: 'b'}
[ ]: d.keys()
[ ]: dict_keys(['a', 'b', 'c', (1, 2), 45])
[ ]: d.values()
[ ]: dict_values([1, 2, 3, [3, 4], 'b'])
[ ]: 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)
Enter the key:4
Enter the value:6
{'4': '6'}
[ ]: ''' WAP to search for a value in dictionary. if the values exists then display␣
↪it along with its key otherwise display value
does not exists '''
num = int(input('Enter the Value to be searched:'))
for i in d:
if d[i] == num:
print(i,'->',d[i])
break
else:
print('Element not found')
Enter the Value to be searched:441
21 -> 441
[ ]: # 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
[ ]: # WAP to replace dictionary values with their average
import numpy as np
d1 = {'a':1,'b':2,'c':3}
d2 = list(d1.values())
avg = np.mean(d2)
for i in d1:
d1[i] = avg
print(d1)
{'a': 2.0, 'b': 2.0, 'c': 2.0}
[ ]: # 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]
[ ]: # Local and Global Variable
def demo():
a = 1 # local variable
print(a)
print(name) # Global variable
18
[ ]: demo()
1
arun
[ ]: name = 'arun'
[ ]: def demo():
global name
name = 'Hello' + name
print(name)
[ ]: demo()
Helloarun
[ ]: # Types of Function argument
'''' 1.Default Argument
2.Keyworg Argument '''
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
[3]: import math
print(math.ceil(7.8))
print(math.floor(7.8))
8
7
[ ]: print(math.sqrt(7))
print(math.pow(2,3))
2.6457513110645907
8.0
[36]: def even_odd(x):
if x % 2 == 0:
return True
else:
return False
[38]: l = [3,4,5]
[42]: list((map(even_odd,l)))
[42]: [False, True, False]
[40]: list((filter(even_odd,l)))
[40]: [4]
[ ]:
20