0% found this document useful (0 votes)
17 views

Notes

Python notes

Uploaded by

Aakanksha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Notes

Python notes

Uploaded by

Aakanksha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python allows to create files, read from files, write content to file and append content to existing

content through inbuilt functions!

Method Description
This method is used to open the file for the specified operation. The
open(file_path,operation)
operation can either be r,w,a for read, write and append.
close() This method is used to close a file which is already open.
This method is used to write a string to a file, if file is present. If not,
write()
it creates the file and writes the string into it.
read() This method is used to read all the contents of a file into a string.

Sometimes, we may encounter exceptions when... by Unknown UserAshutosh9:36 AM

Sometimes, we may encounter exceptions when dealing with files.


In this example, we are trying to write to a file which is opened in read-only mode. This will
result in an exception.
In such a case, let’s see how we can use try except block to catch this exception.

try: flight_file=open("flight.txt","r") text=flight_file.read() print


(text) flight_file.write(",Good
Morning") flight_file.close()except: print("Error occurred") if
flight_file.closed: print("File is closed") else: print("File
is open")

When we want to write a code that will run in all situations, we can put it in a finally block.
Since closing a file is necessary we can do it in the finally block instead of in the try block.

try: flight_file=open("flight.txt","r") text=flight_file.read() print


(text) flight_file.write(",Good Morning")except: print("Error
occurred")finally: print("File is being
closed") flight_file.close() if flight_file.closed: print("File
is closed") else: print("File is open")
Firstly, copy " Code 1 " to TryOut.py in Ec... by Unknown UserAshutosh9:38 AM

Firstly, copy "Code 1" to TryOut.py in Eclipse Plug-in. Execute and observe the results.

Now, copy "Code 2" to execute and observe the results.

Code 1: Code 2:
try: hello_file=open("flight.txt"," try: hello_file=open("flight.txt","
w") text="Hello everyone! w") text="Hello everyone!
Welcome" hello_file.write(text)exce Welcome" hello_file.write(text)exce
pt: print("Error occurred, not able
to write to pt: print("Error occurred, not able
file")finally: hello_file.close()tr to write to
y: hello_file=open("flights.txt","r file")finally: hello_file.close()tr
") text_from_file=hello_file.read() y: hello_file=open("flight.txt","r"
print(text_from_file)except: pri ) text_from_file=hello_file.read()
nt("Error Occurred, not able to read print(text_from_file)except: prin
from t("Error Occurred, not able to read
file")finally: hello_file.close() from
file")finally: hello_file.close()

Q1 of 6outlined_flag

What will be the value of the variables total and game_points_history, after executing the below
code?
def check_loss(game_history,game_points,total):
if game_history[1] == 0:
game_points[1] -= 1
else:
loss_points = game_history[1] * 2
game_points[1] -= loss_points

total = game_points[0] + game_points[1] + game_points[2]


game_history = [4,0,2]
game_points = [12,-4,2]
total = 0
check_loss(game_history, game_points, total)
game_history = [5,2,2]
check_loss(game_history, game_points, total)
print(total,game_points)

0, [12, -4, 2]

9, [12, -5, 2]

0, [12, -9, 2]

5, [12, -9, 2]

Q2 of 6outlined_flag

Predict the output of the following code snippet.


result=0
def find_sum(num1,num2):
if(num1!=num2):
result=num1+num2
else:
result=2*(num1+num2)
find_sum(3,4)
print(result)
find_sum(5,5)
print(result)
a. 0
0
b. 14
10
c. 7
20
d. Error:cannot modify global variable in a program

Q3 of 6outlined_flag

What is the output of the following code snippet?


def find_avg(list_num):
result_sum=0
for num in list_num:
result_sum+=num
result_avg=result_sum/len(list_num)
find_avg([5,8,5])
print(result_avg)

None

0.0

6.0

Error: 'result_avg' is not defined

Q4 of 6outlined_flag

What is the output of the following code snippet?


def func(word, char="A"):
if(char=="A"):
return len(word[1:])
elif(char=="B"):
return len(word[2:])
else:
return len(word)

print(func("Apple","A"),end=" ")
print(func("Apple","B"),end=" ")
print(func("Apple"),end=" ")
print(func("Apple","C"),end=" ")

4345

3235

4 3 Error: missing 1 required positional argument: 'char'

Error: "A" cannot be passed as an argument

Q5 of 6outlined_flag

Choose the statements which are CORRECT with respect to the below code

1. arg2 is a default argument


2. We cannot have a positional argument after arg2 in func1
3. arg4 is a variable length argument
4. We can have any number of default arguments after arg4 in func2
5. arg5 and arg6 are positional arguments
6. arg1 and arg3 are positional arguments
7. *argument_name indicates default argument

def func1(arg1,arg2 = 5):


if(arg1 > arg2):
return arg1
return arg2
def func2(arg3,*arg4):
for i in arg4:
if(arg3 == i):
return i
return 0
def func3(arg5,arg6):
if(arg5 == arg6):
return True
return False
res1 = func1(1)
res2 = func2(res1,1,1,2,5,7,8)
print(func3(arg6 = 5,arg5 = res2))
1,2,4

1,2,3,5

4,5,7

1,2,3,6

Q6 of 6outlined_flag

Predict the output of the following code snippet.


def func(sample, res, key,val):
index =- 1
if(key in sample):
res = True
index = sample.index(key)
values[index] = val
else:
res = False
return index

res = None
sample = ["A","B","C","D"]
values = [1,1,3,4,5]
index = func(sample,res,"B",2)
print(values[index], res)

1 None

2 None

1 True

2 True

has context menu

You might also like