This assignment content provides a brief overview of various programming concepts and techniques. It includes topics such as functions, loops, strings, file reading and writing, and lists. The content also includes examples of Python code for each topic.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Table of Contents 1. Functions......................................................................................................................................2 2. Loops...........................................................................................................................................3 3. Strings..........................................................................................................................................4 4. File Reading and Writing.............................................................................................................5
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
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
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
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)
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()
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
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
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.
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