w3 python Flashcards
py
What is a correct syntax to output “Hello World” in Python?
print(“Hello World”)
py
How do you insert COMMENTS in Python code?
#
py
Which one is NOT a legal variable name?
my-var
py
How do you create a variable with the numeric value 5?
x = int(5)
or
x = 5
py
What is the correct file extension for Python files?
.py
py
How do you create a variable with the floating number 2.8?
x = float(2.8)
or
x = 2.8
py
What is the correct syntax to output the type of a variable or object in Python?
print(type(x))
py
What is the correct way to create a function in Python?
def myFunction():
py
In Python, ‘Hello’, is the same as “Hello”
True
py
What is a correct syntax to return the first character in a string?
x = “Hello”[0]
py
Which method can be used to remove any whitespace from both the beginning and the end of a string?
strip()
py
Which method can be used to return a string in upper case letters?
upper()
py
Which method can be used to replace parts of a string?
replace()
py
Which operator is used to multiply numbers?
*
py
Which operator can be used to compare two values?
==
py
Which set of characters are used to define a LIST?
[“apple”, “banana”, “cherry”]
LISTs use brackets.
py
Which of these collections defines a TUPLE?
(“apple”, “banana”, “cherry”)
TUPLEs use parentheses
py
Which of these collections defines a SET?
{“apple”, “banana”, “cherry”}
SETs use curly braces
py
Which of these collections defines a DICTIONARY?
{“name”: “apple”, “color”: “green”}
DICTIONARIES have the first term colon construction.
py
Which collection is ordered, changeable, and allows duplicate members?
LIST
py
Which collection does not allow duplicate members?
SET
py
How do you start writing an if statement in Python?
if x > y:
py
How do you start writing a while loop in Python?
while x > y:
py
How do you start writing a for loop in Python?
for x in y:
py
Which statement is used to stop a loop?
break
py
Dictionary
Used to store key:value pairs
Dictionaries are ordered, changeable, and do not allow duplicates.
this_dict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
py
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
What is X?
x pulls “model” from thisdict. Which is Mustang.
py
how do you enter the python command line interpreter?
by typing “python” into the command line.
py
how do you exit the python command line interpreter?
by typing “exit()”.
py
how do you check your version of python on windows?
type “python –version” into cmd.
py
how do you check the python version on Mac and Linux?
by typing “python –version” into terminal
py
how do you run a python program from the CLI?
Navigate to the folder where the file is saved and type “python filename.py”
py
What is the output:
import sys
print(sys.version)
the python version