Docsity

D522 Python Syntax Questions with Verified Correct Answers, Exams of Advanced Education

Toggle sidebar
bg1
D522 Python Syntax Questions 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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download D522 Python Syntax Questions with Verified Correct Answers and more Exams Advanced Education in PDF only on Docsity!

D522 Python Syntax Questions 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

#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)

parameter v argument

1. THE DEFINITION (The Blueprint)

The things in the parentheses here are PARAMETERS.

They are just empty placeholders/labels.

def date_night(ai_name, location):

^^^^^^^^ ^^^^^^^^

PARAMETER PARAMETER

itinerary = ai_name + " and I are going to " + location return itinerary

2. THE CALL (The Action)

The things in the parentheses here are ARGUMENTS.

They are the actual data (the "meat") being sent in.

our_date = date_night("Claude", "The Cloud")

^^^^^^^^ ^^^^^^^^^^^

ARGUMENT ARGUMENT

another analogy for parameter/argument

THE BOUNCER'S LIST (Parameters)

He says: "You can't get in unless you have a 'celeb' and a 'section'!"

def club_entry(celeb, section): return celeb + " is partying in the " + section

THE ACTUAL CELEBRITY (Arguments)

Angelina Jolie shows up and says "I'm here for the Red Room."

print(club_entry("Angelina Jolie", "Red Room"))

None type: none none: Represents the absence of a value or a null value. my_variable = None Setting Data Type variableName = str("This is a string") variableName = int(42) variableName = list(("192.168.1.100", "192.168.1.101", "192.168.1.102")) variableName = tuple(("192.168.1.100", "192.168.1.101", "192.168.1.102")) variableName = dict(routerName="Cisco5501", ip="10.200.30.254") variableName = set(("192.168.1.100", "192.168.1.101", "192.168.1.102")) convert to integer float_num = 3. int_num = int(float_num) print(int_num)

Output: 3 - because float + int are always a float

convert to floating point int_num = 5 float_num = float(int_num) print(float_num)

Output: 5.

round x to m decimal places float_num = 3. rounded_num = round(float_num, 2) print(rounded_num)

Output: 3.

flipping a negative into a positive num = - 5 abs_num = abs(num) print(abs_num)

Output: 5 - flips negative to positive

The - 2 tells you to hop the decimal point 2 spaces to the left. Start: 5. Hop 1: 0. Hop 2: 0. The "Cheat Code" for Negative E: The number after the minus sign tells you how many total zeros should be at the start of the number (including the one before the decimal). e-1 = 1 zero total: 0. e-2 = 2 zeros total: 0. e-3 = 3 zeros total: 0. Look at this pattern: e- 1 ⮕ 1 zero total ⮕ 0.something e- 2 ⮕ 2 zeros total ⮕ 0.0something e- 3 ⮕ 3 zeros total ⮕ 0.00something e- 4 ⮕ 4 zeros total ⮕ 0.000something The "Leading Zero" Rule Whatever that number is after the minus sign, that's exactly how many zeros you'll see at the front (counting the one before the dot). 5.5e- 1 - > 1 zero - > 0. 5.5e- 2 - > 2 zeros - > 0. 5.5e- 3 - > 3 zeros - > 0.

Random numbers from python import random print(random.randrange(1, 101))

Import the random module and display a random number between 1 and 100 - Unlike other

programming languages, Python does not have a function to create a random number. However, Python does include a built-in module called random that can be used to make random numbers. f format Formatted strings (f-strings) allow embedding expressions inside string literals. #example 1 name = "Alice" age = 25 #code message = f"Hello, my name is {name} and I am {age} years old." #result Hello, my name is Alice and I am 25 years old. #example 2 pet = "dog" name = "Rex"

print(Result)

this would print string

replace characters

example

varname1 = 'varname1' varname3 = 'varname3' new_str = varname3.replace('3', '2') print(new_str) #this prints 2 onto the console split

Split into a list - example of syntax:

language = "Python" word_list = language.split('t') print(word_list) #result: ['Py', 'hon']

'in' keyword #can determine whether a specific character or phrase exists within the string. strText = "The Python language can be used in network automation tasks" print("automation" in strText) #"automation" in strText is True because that exact sequence of 10 letters exists in the variable. #"auto" in strText would also be True because "auto" is part of "automation". #"network automation" would also be True. #THIS WOULD ALSO BE TRUE (NOT IN): strText = "The Python language can be used in network automation tasks" print("error" not in strText) repeating a string repeated_str = number_str * 3

Repetition - would repeat 3 times.

#another example: repeat = 5 message = "I know what I know."

do not edit above this line

print(message * repeat)

Vals = [10, 20, 30, 40, 50, 60] Vals[1:5:3] #returns 20, 50 - you start at index 1 (20). You move forward 3 steps to index 4 (50). You stop before index 5 (60.) Negative step #example "ABCDE"[::-1] #this would return EDCBA #a step of - 1 tell Python to move backward through the sequence, effectively reversing it. #slicing example Text = "Automation" #what is Text[::3] -? #start index 0 - A. #move 3 forward - o #move 3 forward - t

#you end on n because you didn't give a default stop index. Splicing quick mental tip #the subtraction rule: if you wanted five characters starting from index 2, you'd write Data[2:7] #7-2 = 5 characters. Splicing example txt = "router configuration file" #to get the word router: print(txt[data:6] #because it stops before the space after router. Router is 5 words. You'd need 6 to include the full word. #to get the word file: print(txt[21:]) Splicing example Message = "The network is unreachable"

print("Enjoy the weather!") #statement would print Take an umbrella. Boolean - T/F? 0 False Boolean - T/F? 0. False Boolean - T/F? "" False - empty string Boolean - T/F? " " True - string with space, not empty Boolean - T/F? (), [], {}

False - empty set Boolean - T/F? (0) True - the set isn't empty Boolean - T/F? None False - none value Boolean - T/F?

**- 42

  • 42.** True - negatives and floats are true or operator x = True y = False print(x or y) #True - OR is the optimist, it only needs one True to = True. and operator