Python Flashcards
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.
- The format() method
- using F-Strings
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?
- All 3 go together but
elif
is not required -
if
is used to compare one thing to another
* must end with a colon:
* Any instructions you want to run if statement is true must be indented. How much is not important, but all the lines must be indented by the same amount. - The else statement tells the computer what instructions to follow if the if statement returns a value of false.
* must end with a colon:
- The
elif
is similar to theif
statement.
* The structure of anelif
statement is just the same as the originalif
statement, but it cannot be used alone.
* It must be part of anif
..elif
..else
statement.
* You can have as manyelif
choices as you need before your finalelse
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.
Name the two types of loops in Python.
for
while
What is a
forloop in Python and what does the term “iterable object” mean?
-
for
loops are used when you have a block of code which you want to repeat a fixed number of times. - An iterable object is any Python object capable of returning its members one at a time, permitting it to be stepped through one member at a time.
What does the
range()functon do in Python and how does it work?
The
rangefunction allows us to use a
forloop to repeat a block of code a set number of times. The range function returns a series of numbers, starting and zero and ending with the number specified.
What is a
whileloop ➰ in Python?
A
whileloop is used to execute a block of statements repeatedly until a given condition is no longer true. When the condition becomes false, the line immediately after the loop in the program is executed. The block of code in the loop is identified by being indented.