Python Basics Flashcards
Create function with default argument
def student(firstname, lastname =’Mark’):
pass
Loop through elements in an array
for element in array:
pass
Check if a variable is an array
type(array) is list
Decode and parse a URL
from urllib.parse import urlparse, parseqs
URL=’https://someurl.com/with/query_string?i=main&enc=+Hello’
parsed_url = urlparse(URL)
parse_qs(parsed_url.query)
Increment a variable number by 1
n += 1 (note, n++ is not valid Python syntax)
Return minimum value in array
min(1, 2, 3)
Loop through index and value of elements in an array
for idx, value in enumerate(arr):
pass
Add element to end of list
myList.append(value)
Round a float number to an integer
round(n)
How can you write a number in python using a thousands place delimeter
num = 16_000
How do you indicate that a number is binary?
Prefix with 0b, example 0b0101
What number is this binary value, 10101
The 4 digits represent 16-8-4-2-1
Therefore this is calculated as 16 + 0 + 4 + 0 + 1 = 21
How do you negate / reverse a result?
not True # use the ‘not’ keyword
Write a full if statement with different conditions
if a > b:
pass
elif b > a:
pass
else:
pass
Print numbers 1 through 100
for i in range(1, 101):
print(i)
Print numbers from 100 to 1
for i in range(100, 0, -1):
print(i)
How do you determine the size of a list?
len(myList)
Add a new value to the end of a list
myList.append(‘new value’)
Remove the last element of a list
myList.pop()
Loop through values in a list
for value in [1, 2, 3, 4]:
print(value)
Set variables to the max and min numbers possible
negative_infinity = float(‘-inf’)
positive_infinity = float(‘inf’)
Return every other element from a list
myList[::2]
Return the last 2 elements from a list
myList[-2:]
Combine to lists
[1, 2, 3] + [4, 5, 6]