Pythonmypython

Ace your homework & exams now with Quizwiz!

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

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

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

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"

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

class MyClass: x = 5

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

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

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

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

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

#This is a comment

How do you start writing a for loop in Python? for x in y: for each x in y: for x > y:

How do you start writing a for loop in Python? for x in y: Correct answer for each x in y: for x > y:

Which operator can be used to compare two values?

==

Which collection is ordered, changeable, and allows duplicate members?

DICTIONARY LIST Correct answer TUPLE SET

How do you start writing a while loop in Python? x > y while { while x > y: while (x > y) while x > y {

How do you start writing a while loop in Python? x > y while { while x > y: Correct answer while (x > y) while x > y {

How do you start writing an if statement in Python? if (x > y) if x > y then: if x > y:

How do you start writing an if statement in Python? if (x > y) if x > y then: if x > y: Correct answer

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():

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)

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

print(10 < 9) False

Which collection does not allow duplicate members? SET LIST TUPLE

Which collection does not allow duplicate members? SET Correct answer LIST TUPLE

Which of these collections defines a DICTIONARY? ("apple", "banana", "cherry") ["apple", "banana", "cherry"] {"name": "apple", "color": "green"} {"apple", "banana", "cherry"}

Which of these collections defines a DICTIONARY? ("apple", "banana", "cherry") ["apple", "banana", "cherry"] {"name": "apple", "color": "green"} Correct answer {"apple", "banana", "cherry"}

Which of these collections defines a SET? ["apple", "banana", "cherry"] ("apple", "banana", "cherry") {"name": "apple", "color": "green"} {"apple", "banana", "cherry"}

Which of these collections defines a SET? ["apple", "banana", "cherry"] ("apple", "banana", "cherry") {"name": "apple", "color": "green"} {"apple", "banana", "cherry"} Correct answer

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

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

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"

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

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

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"

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

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

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 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!")

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

Which method can be used to remove any whitespace from both the beginning and the end of a string?

strip()

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

What is a correct syntax to return the first character in a string?

x = "Hello"[0]

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 = 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 = {"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)

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

x=lambda a : a

Which of these collections defines a TUPLE? {"name": "apple", "color": "green"} ("apple", "banana", "cherry") ["apple", "banana", "cherry"] {"apple", "banana", "cherry"}

Which of these collections defines a TUPLE? {"name": "apple", "color": "green"} ("apple", "banana", "cherry") Correct answer ["apple", "banana", "cherry"] {"apple", "banana", "cherry"}

Which statement is used to stop a loop? return break exit stop

Which statement is used to stop a loop? return break Correct answer exit stop

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

X = 50

Which method can be used to return a string in upper case letters?

upper()

Which method can be used to replace parts of a string?

replace()

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 = ["apple", "banana", "cherry"] print(type(x))

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

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"

Which of these collections defines a LIST? {"apple", "banana", "cherry"} {"name": "apple", "color": "green"} ["apple", "banana", "cherry"] C ("apple", "banana", "cherry")

{"apple", "banana", "cherry"} {"name": "apple", "color": "green"} ["apple", "banana", "cherry"] Correct answer ("apple", "banana", "cherry")

What is the correct file extension for Python files?

.py

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

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

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"""

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

print("Hello World")

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 negative indexing to print the last item in the list. fruits = ["apple", "banana", "cherry"] print( ? )

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

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!")

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)

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

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

def my_function(x): return x + 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])

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)

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

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

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

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)

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

print(type(x))

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

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 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 an object of MyClass called p1: class MyClass: x = 5 ? =?

class MyClass: x = 5 p1 = MyClass()

How do you insert COMMENTS in Python code?

#This is a comment

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"])

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

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)

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)

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)

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

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

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

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

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

print(10 == 9) False

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]

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 = True print(type(x))

x = True print(type(x)) bool


Related study sets

hair, skin & nails health assessment

View Set

Essentials of Statistics for the Behavioral Sciences Ch.3

View Set

Normal Balance: Debit or Credit?

View Set

Introductory Microeconomics Chapter 7 Quiz

View Set

Psych Midterm Evolve Quizes (2-6 (Not as important))

View Set

COMM 101 FINAL Q #13 What is facework in communication studies? According to Dr. Ishani Mukherjee and Dr. Maggie Griffith Williams, how do the Hmong view forgiveness and facework? Describe 1 of Mukherjee and Williams' examples of forgiveness and facework

View Set

Anatomy & Physiology Chapter 7: Bones

View Set