D522 Python Syntax Question with Verified
Correct Answers
Assigning multiple variables in a single line
x, y, z = "router", "switch", "firewall"
print(x) print(y) print(z)
Assigning same value to multiple variables in a single line
switch1 = switch2 = switch3 = "Juniper"
Unpacking collections
extracting elements from iterable objects like lists or tuples and assigning them to individual
variables. It simplifies code by enabling simultaneous assignment of values, enhancing
readability and conciseness in assignments and function returns.
ip_addresses = ["172.20.200.10", "172.20.200.11", "172.20.200.12"]
ip1, ip2, ip3 = ip_addresses
print(ip1) print(ip2) print(ip3)
The existing list "device_information" contains a device name, IP address, and subnet
mask, respectively. Assign each value of "device_information" to its own variable using
a single line of code by unpacking. The new variable names should match the existing
print statements.
,device_information = ["name", "ip_address", "subnet_mask"
"laptop1",
"10.0.0.1",
"255.255.255.0"
]
# create variables from device_information
name = "laptop1"
ip_address = "10.0.0.1"
subnet_mask = "255.255.255.0"
# do not edit below this line
print(f"Device Name: {name}")
print(f"IP Address: {ip_address}")
print(f"Subnet Mask: {subnet_mask}")
output print to console with different variable types
x = 15 y = "server"
print(x, y)
- separate the variables with a comma
,local variable
A variable defined inside a function. A local variable can only be used inside its function.
Parameters of a function are also a special kind of local variable.
def my_function():
x = "I am local"
print(x)
my_function()
print(x) <-- This would cause an error!
global variable
x = "I am global"
def my_function():
print(x)
# The function can see it
my_function()
print(x)
# The rest of the code can see it too
new function w/no parameters
# the function will do the same thing every time (with no parameters)
, #syntax
def name():
#example syntax
def say_hi():
print("Hi!")
new function w/parameters
def name(parameters):
#The function needs specific info to do its job.
#example syntax
def say_hi(name):
print("Hi " + name)
return function
return does not output anything to the console, it give a value to the computer/code.
What happens after return? The function stops immediately. Anything written below the
return line inside that function is ignored.
Do I need a variable? Only if you want to save that "answer" to use later.
def add(a, b):
return a + b # Send the sum back
# The line below is like saying: total = 5 + 10
total = add(5, 10)
Correct Answers
Assigning multiple variables in a single line
x, y, z = "router", "switch", "firewall"
print(x) print(y) print(z)
Assigning same value to multiple variables in a single line
switch1 = switch2 = switch3 = "Juniper"
Unpacking collections
extracting elements from iterable objects like lists or tuples and assigning them to individual
variables. It simplifies code by enabling simultaneous assignment of values, enhancing
readability and conciseness in assignments and function returns.
ip_addresses = ["172.20.200.10", "172.20.200.11", "172.20.200.12"]
ip1, ip2, ip3 = ip_addresses
print(ip1) print(ip2) print(ip3)
The existing list "device_information" contains a device name, IP address, and subnet
mask, respectively. Assign each value of "device_information" to its own variable using
a single line of code by unpacking. The new variable names should match the existing
print statements.
,device_information = ["name", "ip_address", "subnet_mask"
"laptop1",
"10.0.0.1",
"255.255.255.0"
]
# create variables from device_information
name = "laptop1"
ip_address = "10.0.0.1"
subnet_mask = "255.255.255.0"
# do not edit below this line
print(f"Device Name: {name}")
print(f"IP Address: {ip_address}")
print(f"Subnet Mask: {subnet_mask}")
output print to console with different variable types
x = 15 y = "server"
print(x, y)
- separate the variables with a comma
,local variable
A variable defined inside a function. A local variable can only be used inside its function.
Parameters of a function are also a special kind of local variable.
def my_function():
x = "I am local"
print(x)
my_function()
print(x) <-- This would cause an error!
global variable
x = "I am global"
def my_function():
print(x)
# The function can see it
my_function()
print(x)
# The rest of the code can see it too
new function w/no parameters
# the function will do the same thing every time (with no parameters)
, #syntax
def name():
#example syntax
def say_hi():
print("Hi!")
new function w/parameters
def name(parameters):
#The function needs specific info to do its job.
#example syntax
def say_hi(name):
print("Hi " + name)
return function
return does not output anything to the console, it give a value to the computer/code.
What happens after return? The function stops immediately. Anything written below the
return line inside that function is ignored.
Do I need a variable? Only if you want to save that "answer" to use later.
def add(a, b):
return a + b # Send the sum back
# The line below is like saying: total = 5 + 10
total = add(5, 10)