can you wite the in pyth ,please? There's a sample out put

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter6: User-defined Functions
Section: Chapter Questions
Problem 1TF: Mark the following statements as true or false: a. To use a predefined function in a program, you...
icon
Related questions
Question

can you wite the in pyth ,please? There's a sample out put

Part One - Using a main function, Docstrings
Write a function called hello. The function has no arguments and no return value. It simply prints the text "Hello World". Include a docstring that describes the function.
Write a main function, as described by the Python main function reading.
Call main, as described by the Python main function reading.
From main, call hello and then print hello's docstring.
Part Two - Error Handling
Write a function called printListElement. The function has two arguments and no return value. The first argument is a list, and the second argument is a list index. The function will print an element from the list as determined by the list index. If the list index is invalid, print an error message.
We could accomplish this with a logic test, but instead, we will manage this with error handling.
Write a try block that attempts to print the list element. Catch any errors with an except block, print an error message.
From main, create a myList list with elements 0, 1, 2 by using the list and range commands.
Then, call printListElement with your list and a list index value of 3.
Part Three - How Python function arguments are treated
There can be some confusion as to how Python functions treat their arguments - is it by reference or by value? Explore this for yourself.
From main, create a myInt variable and give it the value 3. Also create a myList list with elements 0, 1, 2.
Print the IDs of myInt and myList. Also print the ID of the last element of myList.
Now create a function called byVal which has one argument. In the function, add 7 to the argument. Print the ID of the argument before and after the change.
Create a second function called byRef which has one argument. In the function, add 7 to the last element in the list. Print the ID of the argument and the ID of the last element of the argument before and after the change.
Now call by Val with myInt and then call byRef with myList. Next, again print the IDs of MyInt, myList, and the last element of myList. Finally, print myInt and MyList from main. Can you explain the results?
Here are two sites that do a good job of explaining how Python functions work:
Is Python call by reference or call by value
Parameters and Arguments
Transcribed Image Text:Part One - Using a main function, Docstrings Write a function called hello. The function has no arguments and no return value. It simply prints the text "Hello World". Include a docstring that describes the function. Write a main function, as described by the Python main function reading. Call main, as described by the Python main function reading. From main, call hello and then print hello's docstring. Part Two - Error Handling Write a function called printListElement. The function has two arguments and no return value. The first argument is a list, and the second argument is a list index. The function will print an element from the list as determined by the list index. If the list index is invalid, print an error message. We could accomplish this with a logic test, but instead, we will manage this with error handling. Write a try block that attempts to print the list element. Catch any errors with an except block, print an error message. From main, create a myList list with elements 0, 1, 2 by using the list and range commands. Then, call printListElement with your list and a list index value of 3. Part Three - How Python function arguments are treated There can be some confusion as to how Python functions treat their arguments - is it by reference or by value? Explore this for yourself. From main, create a myInt variable and give it the value 3. Also create a myList list with elements 0, 1, 2. Print the IDs of myInt and myList. Also print the ID of the last element of myList. Now create a function called byVal which has one argument. In the function, add 7 to the argument. Print the ID of the argument before and after the change. Create a second function called byRef which has one argument. In the function, add 7 to the last element in the list. Print the ID of the argument and the ID of the last element of the argument before and after the change. Now call by Val with myInt and then call byRef with myList. Next, again print the IDs of MyInt, myList, and the last element of myList. Finally, print myInt and MyList from main. Can you explain the results? Here are two sites that do a good job of explaining how Python functions work: Is Python call by reference or call by value Parameters and Arguments
Sample Execution Results:
Hello world
Help on function hello in module __main__:
hello()
This function prints Hello World
Error: bad index number.
Original ID of myInt in main is 1406102608
Original ID of myList in main is 2884791210120
Original ID of myList's last element in main is 1406102576
Original ID of parameter in byVal 1406102608
ID of parameter in byVal after change 1406102832
Original ID of parameter in byRef 2884791210120
Original ID of parameter's last element in byRef 1406102576
ID of parameter in byRef after change 2884791210120
ID of parameter's last element in byRef after change 1406102800
ID of my Int in main after call to byVal is 1406102608
ID of myList in main after call to byRef is 2884791210120
ID of myList's last element in main after call to byRef is 1406102800
myInt is now: 3
myList is now: [0, 1, 9]
Add the following at the end of the script to show your results:
Execution results:
paste execution results here
Transcribed Image Text:Sample Execution Results: Hello world Help on function hello in module __main__: hello() This function prints Hello World Error: bad index number. Original ID of myInt in main is 1406102608 Original ID of myList in main is 2884791210120 Original ID of myList's last element in main is 1406102576 Original ID of parameter in byVal 1406102608 ID of parameter in byVal after change 1406102832 Original ID of parameter in byRef 2884791210120 Original ID of parameter's last element in byRef 1406102576 ID of parameter in byRef after change 2884791210120 ID of parameter's last element in byRef after change 1406102800 ID of my Int in main after call to byVal is 1406102608 ID of myList in main after call to byRef is 2884791210120 ID of myList's last element in main after call to byRef is 1406102800 myInt is now: 3 myList is now: [0, 1, 9] Add the following at the end of the script to show your results: Execution results: paste execution results here
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Greatest Common Divisor
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr