What does this Python command do?print()
Outputs text to the screen.
What is a variable?
A variable is a storage space in memory, which can be changed by the program as it runs.
How would you ask the user for an input and then store the user’s input in a variable?
varname = input("question to ask user")What is a string in Python?
a string of characters, surrounded by single or double quotation marks.
What is an integer in Python?
a positive or negative whole number
What is an float in Python?
a positive or negative number containing one or more decimals
What is a list in Python?
Multiple items in a single variable. Lists are ordered, allow duplicates and can be changed.
What is a tuple in Python?
also allow multiple items in a single variable (like a list). They are also ordered but cannot be changed.
What is a range in Python?
a sequence of numbers
What is a boolean in Python?
only has one of two values, true or false.
List 2 different ways of formatting output using placeholders.
What method of formatting output using placeholders is the following code using?
first_name = input ('Please enter your first name')
last_name = input('Please enter your last name')
print('Hello {0} {1} pleased to meet you.'.format(first_name,last_name))The format() method
What method of formatting output using placeholders is the following code using?
first_name = input ("Please enter the client's name: ")
height = float(input("Please enter the height: "))
txt = f"{first_name} is {height:.2f}m tall"
print(txt)F-strings
Explain the following code:
first_name = input ('Please enter your first name')
last_name = input('Please enter your last name')
print('Hello {0} {1} pleased to meet you.'.format(first_name,last_name))The program will start by asking the user for their first name. It will then store it in a variable called first_name. Then it will do the same with the last name, but instead will store it in a variable called last_name. Then it will print “Hello (first name) (last name). Pleased to meet you.” It does this by using the placeholder method. It puts the tags {0} and {1} into the print command’s string. Then after the string is closed, but still in the brackets of the print command, it has .format(first_name,last_name) Which will assign first_name to {0} and last_name to {1}.
What do the if, elif and else statements do?
elif is not requiredif is used to compare one thing to another::elif is similar to the if statement.elif statement is just the same as the original if statement, but it cannot be used alone.if..elif..else statement.elif choices as you need before your final else statement.Note: The indents after each of the if, elif and else statements show that these instructions will only be executed if selected by the preceding if, elif or else.
What is used to enclose a list?
Square brackets
[ ]
What seperates each value in a list?
commas
,
What command do you use to print one item from a list?
print (varname [number of the item])
Remember: Lists always start with item 0
How do you add an item to the end of the list?
listvarname.append (item to add to list enclosed in brackets)
How do you print the length of the list?
print (len(listvarname))
How do you remove an element from a list?
listvarname.remove (item to remove from list enclosed in brackets)
How do you sort a list?
myList.sort()
What is the difference between a list and a tuple?
Lists can be edited, but tuples can’t be edited.
What is a loop β° in programming?
A loop in programming code will repeat (iterate) until a specific end result is achieved.