Python 3 Flashcards
How to comment in Python?
#
How to print in Python?
print(text here)
Float number
Number with a decimal
How to do exponents?
**
Example: For 62, you would write 6**2
How to make a multiline string?
””” or ‘’’
How to assign a variable to user input?
input(text here)
Boolean operators in Python
and, or, not
Else if statement
elif
Syntax Error
Error caused by not following the proper syntax of the language
Name Error
Error caused when an unknown variable is used
Type Error
Error caused when you do an operation on an incorrect type
how to concatanate Strings with ints or doubles
use a comma instead of a plus sign
How to create a list
variable = [stuff here, separated by commas]
True or False: Lists can contain any data type
True
How to add something to the end of a list?
list.append(“”)
list concatination
use the + symbol and store it in a new list
How to access the last element of a list
use -1 (-2 would access second to last, etc.)
How to add an element to a specific index in a list?
.insert(index, element)
How to remove an element by index
.pop(index)
you can also do .pop() with no parameters and it will remove the last index
what does range() do
range(10)
will create a range object with numbers 0 -> 9
range(2, 9)
will create a range object with numbers 2 -> 8
range(2, 9, 2)
will create a range object where each number is 2 greater than the previous
[2, 4, 6, 8]
note: because it is a range object, you need to convert it into a list using list(my_range)
how to find the number of items in a list
len(my_list)
how to slice a list
list_name[first index:last index]
note: it goes up to but not including last index
what does list[:n] do?
selects the first n objects in a list (also works for negative, list[-n:] will select that last n objects in a list
.count() method
will count the amount of times the parameter appears in a list (can also be used with 2d lists)