[go: up one dir, main page]

0% found this document useful (0 votes)
234 views12 pages

Practical Set-1: The Result Is 600 The Result Is 70

Here are a few ways to check if a file is empty or not in Python: 1. Check the file size: ```python import os file_path = 'file.txt' if os.stat(file_path).st_size == 0: print('File is empty') else: print('File is not empty') ``` 2. Open the file and check if it is empty: ```python file = open('file.txt') if file.read() == '': print('File is empty') else: print('File is not empty') file.close() ``` 3. Use os.path.

Uploaded by

nency ahir
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)
234 views12 pages

Practical Set-1: The Result Is 600 The Result Is 70

Here are a few ways to check if a file is empty or not in Python: 1. Check the file size: ```python import os file_path = 'file.txt' if os.stat(file_path).st_size == 0: print('File is empty') else: print('File is not empty') ``` 2. Open the file and check if it is empty: ```python file = open('file.txt') if file.read() == '': print('File is empty') else: print('File is not empty') file.close() ``` 3. Use os.path.

Uploaded by

nency ahir
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/ 12

PDS 190170107147

Practical Set-1

1. Given a two integer numbers return their product and if the


product is greater than 1000, then return their sum

def multiplication_or_sum(num1, num2):


# calculate product of two number
product = num1 * num2
# check if product is less then 1000
if product <= 1000:
return product
else:
# product is greater than 1000 calculate sum
return num1 + num2

# first condition
result = multiplication_or_sum(20, 30)
print("The result is", result)

# Second condition
result = multiplication_or_sum(40, 30)
print("The result is", result

Output
The result is 600

The result is 70

Executed in: 0.029 sec(s)

Memory: 4684 kilobyte(s)

2.Given a range of first 10 numbers, iterate from start number to


the end number and print the sum of the current number and
previous number
def sumNum(num):
previousNum = 0

1
PDS 190170107147

for i in range(num):
sum = previousNum + i
print("Current Number", i, "Previous Number ", previousNum," Sum: ", sum)
previousNum = i

print("Printing current and previous number sum in a given range(10)")


sumNum(10)

Output
Printing current and previous number sum in a given range(10)

Current Number 0 Previous Number 0 Sum: 0

Current Number 1 Previous Number 0 Sum: 1

Current Number 2 Previous Number 1 Sum: 3

Current Number 3 Previous Number 2 Sum: 5

Current Number 4 Previous Number 3 Sum: 7

Current Number 5 Previous Number 4 Sum: 9

Current Number 6 Previous Number 5 Sum: 11

Current Number 7 Previous Number 6 Sum: 13

Current Number 8 Previous Number 7 Sum: 15

Current Number 9 Previous Number 8 Sum: 17

Executed in: 0.027 sec(s)

Memory: 4352 kilobyte(s)

3.Given a string, display only those characters which are present


at an even index number.
def printEveIndexChar(str):
for i in range(0, len(str)-1, 2):
print("index[",i,"]", str[i] )

inputStr = "pynative"

2
PDS 190170107147

print("Orginal String is ", inputStr)

print("Printing only even index chars")


printEveIndexChar(inputStr)

Output
Orginal String is pynative

Printing only even index chars

index[ 0 ] p

index[ 2 ] n

index[ 4 ] t

index[ 6 ] v

Executed in: 0.034 sec(s)

Memory: 4236 kilobyte(s)

4.Given a stirng and an integer number n , remove characters


from a string starting from zero up to n and return a new string
def removeChars(str, n):
return str[n:]

print("Removing n number of chars")


print(removeChars("pynative", 4))

Output
Removing n number of chars

tive

Executed in: 0.03 sec(s)

Memory: 4312 kilobyte(s)

3
PDS 190170107147

5.Given a list of numbers, return True if first and last number of


a list is same
def isFirst_And_Last_Same(numberList):
print("Given list is ", numberList)
firstElement = numberList[0]
lastElement = numberList[-1]
if (firstElement == lastElement):
return True
else:
return False

numList = [10, 20, 30, 40, 10]


print("result is", isFirst_And_Last_Same(numList))

Output
Given list is [10, 20, 30, 40, 10]

result is True

Executed in: 0.027 sec(s)

Memory: 4600 kilobyte(s)

6.Given a list of numbers, Iterate it and print only those numbers


which are divisible of 5
def findDivisible(numberList):
print("Given list is ", numberList)
print("Divisible of 5 in a list")
for num in numberList:
if (num % 5 == 0):
print(num)

numList = [10, 20, 33, 46, 55]


findDivisible(numList)

4
PDS 190170107147

Output
Given list is [10, 20, 33, 46, 55]

Divisible of 5 in a list

10

20

55

Executed in: 0.034 sec(s)

Memory: 9344 kilobyte(s)

7.Return the total count of string “Emma” appears in the given


string

sampleStr = "Emma is good developer. Emma is a writer"


# use count method of a str class
cnt = sampleStr.count("Emma")
print(cnt)

Output
2

Executed in: 0.03 sec(s)

Memory: 4336 kilobyte(s)

8.Reverse a given number and return true if it is the same as the


original number

5
PDS 190170107147

def reverseCheck(number):
print("original number", number)
originalNum = number
reverseNum = 0
while (number > 0):
reminder = number % 10
reverseNum = (reverseNum * 10) + reminder
number = number // 10
if (originalNum == reverseNum):
return True
else:
return False

print("The original and reverse number is the same:", reverseCheck(121))

Output
original number 121

The original and reverse number is the same: True

Executed in: 0.033 sec(s)

Memory: 4236 kilobyte(s)

9.Given a two list of numbers create a new list such that new list
should contain only odd numbers from the first list and even
numbers from the second list

def mergeList(listOne, listTwo):

print("First List ", listOne)

print("Second List ", listTwo)

thirdList = []

for num in listOne:

6
PDS 190170107147

if (num % 2 != 0):

thirdList.append(num)

for num in listTwo:

if (num % 2 == 0):

thirdList.append(num)

return thirdList

listOne = [10, 20, 23, 11, 17]

listTwo = [13, 43, 24, 36, 12]

print("result List is", mergeList(listOne, listTwo))

Output
First List [10, 20, 23, 11, 17]

Second List [13, 43, 24, 36, 12]

result List is [23, 11, 17, 24, 36, 12]

Executed in: 0.031 sec(s)

Memory: 4340 kilobyte(s)

10.Accept two numbers from the user and calculate multiplication


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

num2 = int(input("Enter second number "))

res = num1 + num2

print("Sum is", res)

Output
67

7
PDS 190170107147

7
Enter first number Enter second number Sum is 74

Executed in: 0.029 sec(s)

Memory: 4232 kilobyte(s)

11.Display “My Name Is James” as “My**Name**Is**James”


using output formatting of a print() function
print('My', 'Name', 'Is', 'James', sep='**')

Output
My**Name**Is**James

Executed in: 0.029 sec(s)

Memory: 4356 kilobyte(s)

12.Convert decimal number to octal using print() output


formatting
print('%o,' % (8))

Output
10,

Executed in: 0.027 sec(s)

Memory: 4320 kilobyte(s)

13.Display float number with 2 decimal places using print()


print('%.2f' % 458.541315)
Output

8
PDS 190170107147

458.54

Executed in: 0.028 sec(s)

Memory: 4320 kilobyte(s)

14.Accept list of 5 float numbers as a input from user


floatNumbers = []

n = int(input("Enter the list size : "))

for i in range(0, n):

print("Enter number at location", i, ":")

item = float(input())

floatNumbers.append(item)

print("User List is ", floatNumbers)

Output
45.6

67.8

43.6

34.7

5.7

Executed in: 0.028 sec(s)

Memory: 4320 kilobyte(s)

9
PDS 190170107147

15.write all file content into new file by skiping line 5 from
following file
count = 0

with open("test.txt", "r") as fp:

lines = fp.readlines()

count = len(lines)

with open("newFile.txt", "w") as fp:

for line in lines:

if (count == 3):

count -= 1

continue

else:

fp.write(line)

count-=1
Given test.txt file:

line1

line2

line3

line4

line5

line6

line7

Output:

line1

10
PDS 190170107147

line2

line3

line4

line6

line7

16.Accept any three string from one input() call

str1, str2, str3 = input("Enter three string").split()

print(str1, str2, str3)

Output
Enter three string nency ahir vaghamshi

Executed in: 0.028 sec(s)

Memory: 4356 kilobyte(s)

17.You have the following data. totalMoney = 1000 ,quantity =


3, price = 450

display above data using string.format() method

quantity = 3

totalMoney = 1000

price = 450

statement1 = "I have {1} dollars so I can buy {0} football for {2:.2f} dollars."

11
PDS 190170107147

print(statement1.format(quantity, totalMoney, price))

Output
I have 1000 dollars so I can buy 3 football for 450.00 dollars.

Executed in: 0.025 sec(s)

Memory: 4408 kilobyte(s)

18.How to check file is empty or not


import os

print(os.stat("test.txt").st_size == 0)

19.Read line number 4 from the given file

with open("test.txt", "r") as fp:


lines = fp.readlines()
print(lines[3])

test.txt file:

line1

line2

line3

line4

line5

line6

line7

12

You might also like