0% found this document useful (0 votes)
12 views4 pages

Discussion Assignment Unit2

The document outlines a discussion assignment for a Unit 2 Discussion Forum focused on Python programming. Participants are required to create original code examples demonstrating various programming concepts, including function arguments, local variables, and scope. Each example must be explained technically, and the assignment concludes with a question to stimulate further discussion among peers.

Uploaded by

SAYILE JAMES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Discussion Assignment Unit2

The document outlines a discussion assignment for a Unit 2 Discussion Forum focused on Python programming. Participants are required to create original code examples demonstrating various programming concepts, including function arguments, local variables, and scope. Each example must be explained technically, and the assignment concludes with a question to stimulate further discussion among peers.

Uploaded by

SAYILE JAMES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Discussion Assignment

Welcome to the Unit 2 Discussion Forum.

Write your own Python code examples that demonstrate each of the following. Do not copy
examples from the book or any other source. Try to be creative with your examples to
demonstrate that you invented them yourself.

Example 1: Define a function that takes an argument. Call the function. Identify what code
is the argument and what code is the parameter.

Example 2: Call your function from Example 1 three times with different kinds of arguments:
a value, a variable, and an expression. Identify which kind of argument is which.

Example 3: Construct a function with a local variable. Show what happens when you try to
use that variable outside the function. Explain the results.

Example 4: Construct a function that takes an argument. Give the function parameter a
unique name. Show what happens when you try to use that parameter name outside the
function. Explain the results.

Example 5: Show what happens when a variable defined outside a function has the same
name as a local variable inside a function. Explain what happens to the value of each
variable as the program runs.

The code and its output must be explained technically whenever asked. The explanation
can be provided before or after the code, or in the form of code comments within the code.
For any descriptive type question, Your answer must be at least 150 words.

End your discussion post with one question related to programming fundamentals learned
in this unit from which your colleagues can formulate a response or generate further
discussion. Remember to post your initial response as early as possible, preferably by
Sunday evening, to allow time for you and your classmates to have a discussion.
Example 1

# Function that takes argument name Sayile and prints it


name = "Sayile"
def argument(name):
print (name)

argument(name)

In this example, we have a function called argument that takes a parameter named name. The name
parameter represents the argument that will be passed to the function when called, and is passed by
the result statement returning the function argument.

The code ‘name’ is the parameter, it allows us to use the value provided during the function call.

The code ‘Sayile’ is the argument, this is the value that we want to pass into the parameter ‘name’,
which is then passed into the function argument.

…………

Example 2

// Value Argument

# Function that takes argument name Sayile and prints it


def argument(name):
print (name)

#Value argument
argument('This is a value argument')

In the value Argument, we directly pass the value ‘This is a value argument’ as the argument when
calling the function.

// Variable argument

# Function that takes argument name Sayile and prints it


def argument(name):
print (name)

#Variable argument
Test = 'This is a variable argument'
argument(Test)

When it comes to variable arguments, I assigned the value ‘This is a variable argument’ to the variable
‘Test’. Then pass the variable as an argument when calling the function.
// Expression argument

# Function that takes argument name Sayile and prints it


def argument(name):
print (name)

#Expression argument
Test = "This" + " is an " + "Expression argument."
argument(Test)

An expression argument in Python refers to an argument that is an expression, rather than a specific
value or variable.

In Python, an expression is a combination of values, variables, operators, and function calls that are
evaluated to produce a result. It can involve mathematical operations, logical operations, string
concatenation, or any other valid Python expression.

When a function takes an expression as an argument, it means that the function expects the result of
evaluating that expression to be passed in. The expression can be inline within the function call or
assigned to a variable beforehand.

From the above code, we define a variable ‘Test’ and assign the value ‘this’ + ‘ is an ‘ + “ Expression
argument’ to it.

……………

Example 3

# Function with a local variable


def argument(name):
Test = "This" + " is an " + "Expression argument."
print (name)

#Function with a local variable


argument(name)
print(Test)
The error code: “ NameError: name ‘name’ is not defined” is received. This error occurs because the
variable `Test` is defined within the scope of the `argument` function and is inaccessible outside of that
scope. Local variables are limited to the scope in which they are defined, which means they can only be
accessed and used within that specific function or block of code.

……………….

Example 4

def junub(x):
print(x)

junub(10)

#trying to print the parameter name outside the function


print(x)

In this example, the function `junub()` takes an argument named `x`. When the function is called, the
argument is passed to the function and the value of the argument is printed.However, when you try to
use the name `x` outside the function, you get an error 'NameError:' because the name is not
defined.This is because the name `x` is only valid inside the function `junub()`.

Example 5

#using the same variable name in function and outside the function
x = 10

def junub():
x = 20
print(x)

junub()
print(x)

In this example, the variable `x` is first defined outside the function `junub()` with the value 10. Then,
inside the function `junub()`, a new variable `x` is defined with the value 20. When the function `junub()`
is called, the value of the local variable `x` is printed, which is 20. Then, the value of the global variable
`x` is printed, which is still 10.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy