Thursday, 20 September 2018

Python Lab - Day 1

Python Labs  : 1st Day

1. Print Hello World
2. Variables in Python
3. Arithmetic Operation in Python
4. Conditional Statement in Python
5. Loops in Python. While, for
6. Jumping Statement in python
7. Datastructure in python,  python contains four  types of collection datatype  List, Tuple, Dictionary , set
8. Formatting the String  function for print
9. Function demo in Python
10. Implementing inbuilt function of python
11. Importing math functions in python

 Lab 1 : Print Hello World print("hello world") a = 'hello world' print(a)

 Lab 2 Variables in Python a = "my first variable" print(a) b =  100 print(b) c = 10.50 print(c)

 Lab 3 Arithmetic Operation in Python # getting the numbers a = 1 b = 2 c = a + b print(c) print("Answer is ", c) # printing the values into mutiple time demo = "Demo \n" print( demo * 5) #getting the square values square = 2 ** 3 input("get the value") print(square) # getting the divisions d = 8/3 print(d) dd = 8//3 print("division vaues",dd) # input values in the Python num1 = input("Enter the value of 1  num :: ") num2 = input("Enter the value of 2 num :: ") print("Addition of two values " ,int(num1) + int(num2)) ##   pre  conversion num3 =  int(input("Enter the value of 3  num :: ")) num4 = int (input("Enter the value of 4 num :: ")) print("Addition of two values "  ,num3 + num4)

 Lab 4 Conditional Statement in Python # conditional statement #print( 8 & 1) ''' muti line Commented values ''' ''' #Using if if and else num  = int(input("Provide the number : ")) if(num & 1) == 0: #indentation     print("Even Number") else :     print("Odd Number") ''' # if else ladder a, b, c = 2, 1, -8 if a> b:     if a > c:         print("a is bigger")     else :         print("c is bigger") else:     if b > c:         print("b is bigger")     else:         print("c is bigger") # make it same line of condition print("_" * 20) if a> b and a > c:     print("a is bigger") elif b > c:     print("b is bigger") else:     print("c is bigger") # programme to calculate leap year lp  = int(input("Provide the year : ")) if (lp % 4) == 0:     print(lp, " is Leap Year") else:     print(lp, " is not Leap Year")

 Lab 5 Loops in Python. While, for, # Loops ''' i = 1 while i < 100 :     print (i)     i += 1 ''' # print  even number till 100 j = 1 while j < 100:     if (j % 2) == 0:         print (j)     j +=1 x =0 for x in range(0, 3):     print(x) name = "This is the required document" list3 = ["A", "E", "I", "O","U"] count = 0 for i in name :     print(i)     if  i.upper() == 'A' or i.upper() =='E' or i.upper() == 'I' or i.upper() == 'O' or i.upper() == 'U' :       count = count + 1 print("No of Vowel =", count)

 Lab 6 Jumping Statement in python # Jumping statetemnt ''' Break Continue Pass ''' ''' num =40 if num >1:     pass while num >1 :     num =  num - 1     print(num)     if num == 38:         break     else:         continue ''' a = [0, 1, 2] element = 0 for element in a:     if not element:         pass         print(element) for element in a:     if not element:         continue         print(element)

 Lab 7 Datastructure in python,  python contains four  types of collection datatype  List, Tuple, Dictionary , set ''' List : collection of element contains homogenues and hetrogeneus, denoted by [] tuple : denoted by () dictionary : denoted by {} set ''' ## List a =[] b = [1,2,3,4,5,6,7,8,9] print(b) b.reverse() print(b) b.pop() print(b) b.append(8) print(b) b.insert(3,6) print(b) b.sort() print(b) print(b.count(8)) a.append("Hyderbad") print(a) a.append("Delhi") print(a) c = [1,2,3] c.extend(a) print(c) print(len(c)) d = c.copy() d.append("dest") e= c e.append("New") print(c) print(d) print(e) # slicing in data structure print(b[:2]) print(b[2:6]) print(b[1:6]) print(b[5:2]) m = [1,2,3,4,5,6,7,8,9] print(m[3:6]) # 7 - 4, 7 - 1 print(m[-4:-1]) print(m[::2]) print(m[::5]) print(m[::1]) print(m[::-1]) print(m[::-2]) print(m[1::-5]) b = "Hyderbad" print(b[::-1]) a = 12345 ar = str(a) print(int(ar[::-1]))

 Lab 8 Formatting the String  function for print # format function using the print command a,b,c,d = 1,2,3,4 print("a = ",a, "b =", b, "c = ",c ,"d =", d) print("a = {}, b = {}, c = {}, d = {}".format(a,b,c,d)) print("a = {0:.1f}, b = {1:d}, c = {2:b}, d = {3:^5}".format(a,b,c,d)) print("a = {0}, b = {1}, c = {2}, d = {d}".format(a,b,c,d = "satya").capitalize()) print("a = {0}, b = {1}, c = {2}, d = {d}".format(a,b,c,d = "SATYA").upper())

 Lab 9 Function demo in Python # using of function def myevenodd(val = 26) :     if val == 26:         print(val, "default function call")     elif val % 2:         print(val, "It is even number")     else :         print(val, "It is odd number") ''' val = int(input("Enter the number : ")) if myevenodd(val) == True:     print(val, "It is even number") elif myevenodd(val) == False :     print(val, "It is odd number") else :     print(val,  "default function call") v = 1 print("Even value") for  v in range(1,101):     myevenodd(v) '''

 Lab 10 Implementing inbuilt function of python a,b,c,d = 10,20,30,40 # Inbuilt Min and max function xx= max(a,b,c,d) xm= min(a,b,c,d) print(xx) print(xm) print(a,b,c, d) # power functions pp = pow print(pp(3,3)) p = pow(2,3) print(p)

 Lab 11 Importing math functions in python import math def reverse_number(val):     res = 0     while val > 0 :         rem = val % 10         res = (res* 10) + rem         if val > 0 :            val = val // 10     return res def adon_number(num):     #print("num", num)     square =  num ** 2     #print("square", square)     rev1 = reverse_number(square)     #print("rev1", rev1)     mysqrt = int(math.sqrt(rev1))    # print("mysqrt", mysqrt)     rev2 = reverse_number(mysqrt)     #print("Adon number", rev2)     return rev2 x = 1 for x in range(1,1000):     op = adon_number(x)     if op == x:         print(x, " is adon number")