Python Programming Assignment: Functions, Loops, Strings, and Files

Verified

Added on  2019/09/16

|10
|781
|200
Homework Assignment
AI Summary
This document presents Python code solutions for several programming concepts. The solutions cover functions, loops, strings, file reading and writing, and lists. The function examples include output predictions and explanations. The loops section demonstrates a while loop. The string section provides a function to swap characters. The file handling section shows how to read and write to files. The list section includes examples and explanations for functions such as MysteryFunc1 and MysteryFunc2, including their outputs and logic. The document is a useful resource for students learning Python programming, offering practical examples and explanations to aid in understanding and completing assignments.
Document Page
[School]
[Course title]
[Document title]
[Document subtitle]
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Table of Contents
1. Functions......................................................................................................................................2
2. Loops...........................................................................................................................................3
3. Strings..........................................................................................................................................4
4. File Reading and Writing.............................................................................................................5
Document Page
1. Functions
Output:
['Bane', 'Zannah', 'Cognus'] Gordon Freman 54 3.14
['Bane', 'Zannah', 'Cognus', 'Vader'] Gordon 108 3.14
['Bane', 'Zannah', 'Cognus', 'Vader'] Gordon 108 2
['Bane', 'Zannah', 'Cognus', 'Vader'] Gordon Freman 54 3.14
Document Page
2. Loops
a. Output
25
20
15
10
5
While Loop
val = 25
while (val>0):
print (val)
val = val – 5
b. Output
the list index of myList is out of range hence there is runtime error
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
3. Strings
def SwapFunction(string):
temp=''
if len(string)%2==0:
for i in range(0,len(string),2):
temp += string[i + 1] + string[i]
string=temp
else:
c=string[-1]
for i in range(0,len(string)-1,2):
temp += string[i + 1] + string[i]
temp=temp+string[-1]
string=temp
return temp
string=input('Enter a string: ')
n=SwapFunction(string)
print (n)
Document Page
4. File Reading and Writing
with open("mydata.txt") as Temp:
LineR = Temp.readlines()
StringC = ""
FileWriter = open('newdata.txt', 'w')
if len(LineR) % 2 == 0:
for i in range(1,len(LineR)+1):
StringC += LineR[i-1].strip()
if i % 2 == 0:
print (StringC)
FileWriter.write(StringC+"\n")
StringC = ""
else:
for i in range(1,len(LineR)+1):
StringC += LineR[i-1].StringCip()
if i % 2 == 0:
print (StringC)
FileWriter.write(StringC+"\n")
StringC = ""
print(LineR[len(LineR)-1])
FileWriter.close()
Document Page
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
5. Lists
a)
The function MysteryFucn1 is called in the program given:
def MysteryFunc1(mysteryObject):
t=1
for item in mysteryObject:
t *= item
return t
print(MysteryFunc1([2,3,2,3]))
print(MysteryFunc1([]))
when input of 2,3,2,3 is fed to the function it sets the value of t=1
then every element in the list is multiplied by t and result is saved as value t hence
t *=2, t =2
t *=3, t = 2*3
t *=2, t =2*3*2
t *=3, t = 2*3*3*2
hence result is 36
now in the next case there are no arguments hence nothing is passed hence the loop doesn’t
work and we get the value of t as 1 which was initialized
Document Page
b)
def MysteryFunc2(mysteryObject):
mys1 = mysteryObject[0]
for el in mysteryObject[1:]:
if mys1 < el:
mys1 = el
return mys1
print(MysteryFunc2([20,3,90,3]))
print(MysteryFunc2([1]))
The MysteryFunc2 reads all the values passed to the function
The first value of the array passed is assigned to the mys1 = 20
Now loop works for each element el
We check all the elements and traverse till we find
mysl (20) < 3 no
mysl(20)<90 yes mys1 = 90
mysl(90) < 3 no
return the mysl hence we get the output 90
for next we only pass 1 as value so 1 is returned.
c)
in this we are counting the number which is passed an argument as el and its occurrence in the
passed array.
Document Page
Hence in first case we are searching for number of 3 in passed array [20, 3, 90, 3] we get output
as 2
In second case we are searching for number of 5 in passed array [20, 3, 90, 3] we get output as 0
chevron_up_icon
1 out of 10
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]