Python Cheat Sheet
Python Cheat Sheet
PYTHON RANDOM
# PYTHON RANDOM
import random
print(a)
print(b)
PYTHON ARRAY
# PYTHON ARRAY
print(numbers[1])
PYTHON 2D ARRAY
# PYTHON 2D ARRAY
ARRAY METHODS
arr = [1, 1, 2, 3, 4, 5]
# append = Adds an element at the end of the list
arr.append(20)
print(arr[5])
# copy = Returns a copy of the list
arr2 = arr.copy()
print(arr2[0])
# count = Returns the number of elements with the specified value
print(arr.count(1))
# index = Returns the index of the first element with the specified value
print(arr.index(5))
# insert = Adds an element at the specified position
arr.insert(2, 10)
print(arr[2])
# pop = Removes the element at the specified position
arr.pop()
# remove = Removes the first item with the specified value
arr.remove(1)
# reverse = Reverses the order of the list
arr.reverse()
print(arr[0])
# sort = Sorts the list, increasing order
arr.sort()
# clear = Removes all the elements from the list
arr.clear()
PYTHON RANDOM EXERCISE [guessing game]
winning_number = random.randint(1-5)
guess = int(input('Guess a number between 1-5:'))
if guess == winning_number:
print('Guess you won!')
else:
print('Better luck next time')
print(average)
PYTHON 2D ARRAY EXERCISE [IGN generator using 2d array]