Python Programming for Cybersecurity

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Use a multiline string to make the a multi line comment: -This is a comment written in more that just one line-

"""This is a comment written in more that just one line"""

Comments in Python are written with a special character, which one?

#This is a comment

How do you insert COMMENTS in Python code?

#This is a comment

What is the correct file extension for Python files?

.py

What is the correct syntax to execute the printname method of the object x? class Person: def __init__(self, fname): self.firstname = fname def printname(self): print(self.firstname) class Student(Person): pass x = Student("Mike") ?

class Person: def __init__(self, fname): self.firstname = fname def printname(self): print(self.firstname) class Student(Person): pass x =Student("Mike") x.printname()

What is the correct syntax to assign a "init" function to a class? class Person: def ?(self, name, age): self.name = name self.age = age

class Person: def -init- (self, name, age): self.name = name self.age = age

What is the correct syntax to create a class named Student that will inherit properties and methods from a class named Person? class ?:

classStudent(Person):

What is the correct way to create a function in Python?

def myFunction():

Create a function named my_function. ? print("Hello from a function")

def my_function(): print("Hello from a function")

Execute a function named my_function. def my_function(): print("Hello from a function") ?

def my_function(): print("Hello from a function") my_function()

If you do not know the number of keyword arguments that will be passed into your function, there is a prefix you can add in the function definition, which prefix? def my_function(?kid): print("His last name is " + kid["lname"])

def my_function(**kid): print("His last name is " + kid["lname"])

If you do not know the number of arguments that will be passed into your function, there is a prefix you can add in the function definition, which prefix? def my_function(?kids): print("The youngest child is " + kids[2])

def my_function(*kids): print("The youngest child is " + kids[2])

Inside a function with two parameters, print the first parameter. def my_function(fname, lname): print()

def my_function(fname, lname): print(fname)

Let the function return the x parameter + 5. def my_function(x): ????

def my_function(x): return x + 5

Insert the correct keyword to make the variable x belong to the global scope. def myfunc(): ? x x = "fantastic"

def myfunc(): global x x = "fantastic"

Print "Hello" if a is equal to b, or if c is equal to d. f a == b ? c == d: print("Hello")

f a == b or c == d: print("Hello")

Use the range function to loop through a code set 6 times. for x in ?: print(x)

for x in range(6) : print(x)

Use negative indexing to print the last item in the tuple. fruits = ("apple", "banana", "cherry") print(?)

fruits = ("apple", "banana", "cherry") print(fruits[-1])

Use the correct syntax to print the first item in the fruits tuple. fruits = ("apple", "banana", "cherry") print(?)

fruits = ("apple", "banana", "cherry") print(fruits[0])

Use the correct syntax to print the number of items in the fruits tuple. fruits = ("apple", "banana", "cherry") print()

fruits = ("apple", "banana", "cherry") print(len(fruits))

Use a range of indexes to print the third, fourth, and fifth item in the list. fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") print(fruits[?])

fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango") print(fruits[2:5])

Use a range of indexes to print the third, fourth, and fifth item in the list. fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(fruits[?])

fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(fruits[2:5])

Exit the loop when x is "banana". fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": ? print(x)

fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": break print(x)

In the loop, when the item value is "banana", jump directly to the next item. fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": ? print(x)

fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": continue print(x)

Use negative indexing to print the last item in the list. fruits = ["apple", "banana", "cherry"] print( ? )

fruits = ["apple", "banana", "cherry"] print(fruits[-1])

Print the second item in the fruits list. fruits = ["apple", "banana", "cherry"] print( ? )

fruits = ["apple", "banana", "cherry"] print(fruits[1])

Use the correct syntax to print the number of items in the list. fruits = ["apple", "banana", "cherry"] print( ?)

fruits = ["apple", "banana", "cherry"] print(len(fruits))

Use the append method to add "orange" to the fruits list. fruits = ["apple", "banana", "cherry"] ?

fruits = ["apple", "banana", "cherry"] fruits.append("orange")

Use the remove method to remove "banana" from the fruits list. fruits = ["apple", "banana", "cherry"] ?

fruits = ["apple", "banana", "cherry"] fruits.remove("banana")

Loop through the items in the fruits list. fruits = ["apple", "banana", "cherry"] ? x ? fruits? print(x)

fruits = ["apple", "banana", "cherry"] for x in fruits : print(x)

Change the value from "apple" to "kiwi", in the fruits list. fruits = ["apple", "banana", "cherry"] ? =?

fruits = ["apple", "banana", "cherry"] fruits[0]="kiwi"

Use the insert method to add "lemon" as the second item in the fruits list. fruits = ["apple", "banana", "cherry"] fruits.insert(1,"lemon")

fruits = ["apple", "banana", "cherry"] fruits.insert(1,"lemon")

Use the correct membership operator to check if "apple" is present in the fruits object. fruits = ["apple", "banana"] if "apple" ? fruits: print("Yes, apple is a fruit!")

fruits = ["apple", "banana"] if "apple" in fruits: print("Yes, apple is a fruit!")

Check if "apple" is present in the fruits set. fruits = {"apple", "banana", "cherry"} if "apple" ? fruits: print("Yes, apple is a fruit!")

fruits = {"apple", "banana", "cherry"} if "apple" in fruits: print("Yes, apple is a fruit!")

Use the add method to add "orange" to the fruits set. fruits = {"apple", "banana", "cherry"} ?

fruits = {"apple", "banana", "cherry"} fruits.add("orange")

Use the discard method to remove "banana" from the fruits set. fruits = {"apple", "banana", "cherry"} ?

fruits = {"apple", "banana", "cherry"} fruits.discard("banana")

Use the remove method to remove "banana" from the fruits set. fruits = {"apple", "banana", "cherry"} ?

fruits = {"apple", "banana", "cherry"} fruits.remove("banana")

Use the correct method to add multiple items (more_fruits) to the fruits set. fruits = {"apple", "banana", "cherry"} more_fruits = ["orange", "mango", "grapes"] ?

fruits = {"apple", "banana", "cherry"} more_fruits = ["orange", "mango", "grapes"] fruits.update(more_fruits)

In the loop, when i is 3, jump directly to the next iteration. i = 0 while i < 6: i += 1 if i == 3: ? print(i)

i = 0 while i < 6: i += 1 if i == 3: continue print(i)

Stop the loop if i is 3. i = 1 while i < 6: i == 3: ? i += 1

i = 1 while i < 6: f i == 3: break i += 1

Print i as long as i is less than 6. i = 1 ? i < 6 ? print(i) i += 1

i = 1 while i < 6: print(i) i += 1

Print a message once the condition is false. i = 1 while i < 6: print(i) i += 1 ? print("i is no longer less than 6")

i = 1 while i < 6: print(i) i += 1 else: print("i is no longer less than 6")

Use the correct comparison operator to check if 5 is not equal to 10. if 5 ?10: print("5 and 10 is not equal")

if 5 != 10: print("5 and 10 is not equal")

Use the correct logical operator to check if at least one of two statements is True. if 5 == 10 ? 4 == 4: print("At least one of the statements is true")

if 5 == 10 or 4 == 4: print("At least one of the statements is true")

Insert the missing indentation to make the code correct: if 5 > 2: print("Five is greater than two!")

if 5 > 2: print("Five is greater than two!")

Use the correct short hand syntax to put the following statement on one line: if 5 > 2: print("Five is greater than two!")

if 5 > 2: print("Five is greater than two!")

Print "Hello" if a is equal to b, and c is equal to d. if a == b ? c == d: print("Hello")

if a == b and c == d: print("Hello")

What is the correct syntax to import a module named "mymodule"? ?mymodule

import mymodule

What is the correct syntax for creating an alias for a module? import mymodule ? mx

import mymodule as mx

What is the correct syntax of printing all variables and function names of the "mymodule" module? import mymodule print( ?)

import mymodule print(dir(mymodule)

Remove the illegal characters in the variable name: 2my-first_name = "John"

myfirst_name = "John"

Insert the missing part of the code below to output____ "Hello World"

print("Hello World")

What is a correct syntax to output "Hello World" in Python?

print("Hello World")

Use the correct short hand syntax to put the following statement on one line: if 5 > 2: print("Yes") else print("No")

print("Yes") if 5 > 2 else print("No")

The statement below would print a Boolean value, which one? print(10 < 9) ?

print(10 < 9) False

The statement below would print a Boolean value, which one? print(10 == 9) ?

print(10 == 9) False

The statement below would print a Boolean value, which one? print(10 > 9) ?

print(10 > 9) True

Multiply 10 with 5, and print the result. print(10 ? 5)

print(10* 5)

Divide 10 by 2, and print the result. print(10 ? 2)

print(10/ 2)

The statement below would print a Boolean value, which one? print(bool("abc")) ?

print(bool("abc")) True

The statement below would print a Boolean value, which one? print(bool(0)) ?

print(bool(0)) False

What is the correct syntax to output the type of a variable or object in Python?

print(type(x))

Return the string without any whitespace at the beginning or the end. txt = " Hello World " x =?

txt = " Hello World " x =txt.strip()

Replace the character H with a J. txt = "Hello World" txt = txt.(, )

txt = "Hello World" txt = txt.replace("H", "J")

Convert the value of txt to lower case. txt = "Hello World" txt =txt.lower()

txt = "Hello World" txt =txt.lower()

Convert the value of txt to upper case. txt = "Hello World" txt =?

txt = "Hello World" txt =txt.upper()

Get the first character of the string txt. txt = "Hello World" x =?

txt = "Hello World" x =txt[0]

Get the characters from position 2 to position 5 (not included). txt = "Hello World" x =?

txt = "Hello World" x =txt[2:5]

Use the len method to print the length of the string. x = "Hello World" print(?)

x = "Hello World" print(len(x))

The following code example would print the data type of x, what data type would that be? x = "Hello World" print(type(x))

x = "Hello World" print(type(x)) str

The following code example would print the data type of x, what data type would that be? x = ("apple", "banana", "cherry") print(type(x))

x = ("apple", "banana", "cherry") print(type(x)) tuple

How do you create a variable with the floating number 2.8?

x = 2.8 or x = float(2.8)

The following code example would print the data type of x, what data type would that be? x = 20.5 print(type(x))

x = 20.5 print(type(x)) float

Create a variable called z, assign x + y to it, and display the result. x = 5 y = 10 ?=x + y print(?)

x = 5 y = 10 z = x + y print(z)

Insert the correct syntax to convert x into a complex number. x = 5 x =? (x)

x = 5 x =complex (x)

Insert the correct syntax to convert x into a decimal number. x = 5 x =?(x)

x = 5 x =float (x)

The following code example would print the data type of x, what data type would that be? x = 5 print(type(x))

x = 5 print(type(x)) int

Insert the correct syntax to convert x into a integer. x = 5.5 x =? (x)

x = 5.5 x = int(x)

The following code example would print the data type of x, what data type would that be? x = True print(type(x))

x = True print(type(x)) bool

The following code example would print the data type of x, what data type would that be? x = ["apple", "banana", "cherry"] print(type(x))

x = ["apple", "banana", "cherry"] print(type(x)) list

The following code example would print the data type of x, what data type would that be? x = {"name" : "John", "age" : 36} print(type(x))

x = {"name" : "John", "age" : 36} print(type(x)) dict

How do you create a variable with the numeric value 5?

x =5 or x = int(5)

Insert the correct syntax to assign the same value to all three variables in one code line. x ? y ? z ? "Orange"

x= y= z ="Orange"

Create a lambda function that takes one parameter (a) and returns it. x = ????

x=lambda a : a

Display the sum of 5 + 10, using two variables: x and y.

X = 5 y = 10 print(x+y)

Create a variable named x and assign the value 50 to it.

X = 50

Print "Hello World" if a is not equal to b. a = 50 b = 10 ?a? b? print("Hello World")

a = 50 b = 10 if a != b : print("Hello World")

Print "1" if a is equal to b, print "2" if a is greater than b, otherwise print "3". a = 50 b = 10 ? a ?b ? print("1") ? a ?b? print("2")

a = 50 b = 10 if a ==b: print("1") elif a > b: Print("2")

Print "Yes" if a is equal to b, otherwise print "No". a = 50 b = 10 ?a ?b? print("Yes") ? print("No")

a = 50 b = 10 if a == b :print("Yes") else: print("No")

Print "Hello World" if a is greater than b a = 50 b = 10 ?a ?b? print("Hello World")

a = 50 b = 10 if a> b print("Hello World")

Insert the correct syntax to add a placeholder for the age parameter. age = 36 txt = "My name is John, and I am ? " print(txt.format(age))

age = 36 txt = "My name is John, and I am {} " print(txt.format(age))

Use the clear method to empty th e car dictionary. car = { "brand": "Ford", "model": "Mustang", "year": 1964 } ?

car = { "brand": "Ford", "model": "Mustang", ""year": 1964 car.clear()

Use the pop method to remove "model" from the car dictionary. car = { "brand": "Ford", "model": "Mustang", "year": 1964 } ?

car = { "brand": "Ford", "model": "Mustang", "year": 1964 } car.pop("model")

Add the key/value pair "color" : "red" to the car dictionary. car = { "brand": "Ford", "model": "Mustang", "year": 1964 } ? ? ?

car = { "brand": "Ford", "model": "Mustang", "year": 1964 } car["color"]="red"

Change the "year" value from 1964 to 2020. car = { " brand": "Ford", "model": "Mustang", "year": 1964 } ?=?

car = { "brand": "Ford", "model": "Mustang", "year": 1964 } car["year"]=2020

Use the get method to print the value of the "model" key of the car dictionary. car = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(? )

car = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(car.get("model"))

Create a variable named carname and assign the value Volvo to it.

carname ="Volvo"

Use the p1 object to print the value of x: class MyClass: x = 5 p1 = MyClass() print( ?)

class MyClass: x = 5 p1 = MyClass() print(p1.x)

Create a class named MyClass: ? MyClass: x = 5

class MyClass: x = 5

Create an object of MyClass called p1: class MyClass: x = 5 ? =?

class MyClass: x = 5 p1 = MyClass()


Kaugnay na mga set ng pag-aaral

Organizational Behavior: Ch 4 Emotions & Moods, Quiz 4, MGMT Test 1, BUAD309 Ch. 4, OB - Chapter 4, Ch3 Attitudes and job Satisfaction, Quiz 3, OB Ch. 3, Chapter 3: Attitudes & Job Satisfaction, MGMT 3750 Chapter 3: Job Attitudes and Job Satisfaction...

View Set

SIE CHAPTER 9 Securities Industry Regulations

View Set