Python Flashcards

1
Q

What does this Python command do?
print()

A

Outputs text to the screen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a variable?

A

A variable is a storage space in memory, which can be changed by the program as it runs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you ask the user for an input and then store the user’s input in a variable?

A
varname =  input("question to ask user")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a string in Python?

A

a string of characters, surrounded by single or double quotation marks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an integer in Python?

A

a positive or negative whole number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an float in Python?

A

a positive or negative number containing one or more decimals

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a list in Python?

A

Multiple items in a single variable. Lists are ordered, allow duplicates and can be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a tuple in Python?

A

also allow multiple items in a single variable (like a list). They are also ordered but cannot be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a range in Python?

A

a sequence of numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a boolean in Python?

A

only has one of two values, true or false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

List 2 different ways of formatting output using placeholders.

A
  • The format() method
  • using F-Strings
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

The format() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

F-strings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

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}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What do the if, elif and else statements do?

A
  • 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 the if statement.
    * The structure of an elif statement is just the same as the original if statement, but it cannot be used alone.
    * It must be part of an if..elif..else statement.
    * You can have as many 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is used to enclose a list?

A

Square brackets

[ ]

17
Q

What seperates each value in a list?

18
Q

What command do you use to print one item from a list?

A

print (varname [number of the item])

Remember: Lists always start with item 0

19
Q

How do you add an item to the end of the list?

A

listvarname.append (item to add to list enclosed in brackets)

20
Q

How do you print the length of the list?

A

print (len(listvarname))

21
Q

How do you remove an element from a list?

A

listvarname.remove (item to remove from list enclosed in brackets)

22
Q

How do you sort a list?

A

myList.sort()

23
Q

What is the difference between a list and a tuple?

A

Lists can be edited, but tuples can’t be edited.

24
Q

What is a loop ➰ in programming?

A

A loop in programming code will repeat (iterate) until a specific end result is achieved.

25
Q

Name the two types of loops in Python.

26
Q

What is a

for
loop in Python and what does the term “iterable object” mean?
A
  • 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.
27
Q

What does the

range()
functon do in Python and how does it work?
A

The

range
function allows us to use a
for
loop 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.
28
Q

What is a

while
loop ➰ in Python?
A

A

while
loop 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.