Python Slip 8

9c2b9860b23142968aa6a46454745fa1

Q1. Write a python program to calculate the cube of all numbers from 1 to n.

num = int (input("Enter an any number: "))
for i in range(1,num+1):
    cb = i**3
    print("Cube of {0} is {1} ".format(i, cb))
Enter an any number: 6
Cube of 1 is 1 
Cube of 2 is 8 
Cube of 3 is 27 
Cube of 4 is 64 
Cube of 5 is 125 
Cube of 6 is 216 

Q2. Write a python program to display all prime numbers within given range.

min = int(input("Enter the min of range : "))
max = int(input("Enter the max of range: "))

for n in range(min,max + 1):
   if n > 1:
       for i in range(2,n):
           if (n % i) == 0:
               break
       else:
           print(n)
Enter the min of range : 10
Enter the max of range: 50
11
13
17
19
23
29
31
37
41
43
47

Q2. Write a python program to list only files from a directory and print files count

import os

# Directory path
dir_path = r'C:\RS\MSC(CA)'
print("Files and directories in '", dir_path, "' :")
count = 0
for path in os.listdir(dir_path):
    if os.path.isfile(os.path.join(dir_path, path)):
        print(path)
        count += 1
print('\n\nFile count:', count)
Files and directories in ' C:\RS\MSC(CA) ' :
Python Assignment.docx
Python Lab.ppt
Python Lab.ppt.pptx
Python Programming Question Bank -Mid Term.docx
Unit I Introduction to Python Scripting.ppt
Unit II Python strings.ppt
Unit III Python tuples, sets, Dictionary.ppt
Unit IV FUnction New.ppt
Unit IV Functions.ppt
Unit V Directories.ppt
Unit V Files and Directories.ppt
Unit VI Python Classes and Objects.ppt


File count: 12

0 Comments:

Post a Comment