0% found this document useful (0 votes)
37 views

Python Cheat Sheet

The document discusses Python escape sequences, random number generation, arrays, 2D arrays, and array methods in Python. It provides examples of using escape sequences like \n and \t, generating random numbers with the random module, declaring and accessing elements of 1D and 2D arrays, and using common array methods like append(), pop(), sort(). It also includes exercises using random numbers, calculating averages from arrays, and generating IGNs from user input and a 2D array.

Uploaded by

Mir AI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Python Cheat Sheet

The document discusses Python escape sequences, random number generation, arrays, 2D arrays, and array methods in Python. It provides examples of using escape sequences like \n and \t, generating random numbers with the random module, declaring and accessing elements of 1D and 2D arrays, and using common array methods like append(), pop(), sort(). It also includes exercises using random numbers, calculating averages from arrays, and generating IGNs from user input and a 2D array.

Uploaded by

Mir AI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PYTHON ESCAPE SEQUENCE (some)

# PYTHON ESCAPE SEQUENCE

# \n = creates a new line


print('this should create a new line \ndid it work?')
# \t = inserts a tab
print('\tthis should be indented')
# \' = inserts a single quote
print('It\'s working I guess')
# \" = inserts a double quote
print('he called me an \"idiot\"')
# \\ = inserts a backslash
print("it is a \\ or a backslash")

PYTHON RANDOM

# PYTHON RANDOM
import random

# same as generating random decimal number between 0 and 1


a = random.random()

# save random in a variable


ran = random
# generating number within the bounds (lowest, highest)
b = ran.randint(1, 5)

print(a)
print(b)

PYTHON ARRAY

# PYTHON ARRAY

# declare the number of element and its value


numbers = [1, 2, 3]

# edit array content during runtime


numbers[1] = 4

print(numbers[1])
PYTHON 2D ARRAY

# PYTHON 2D ARRAY

# declare the number of element in array and its value


matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# edit content of array during runtime
matrix[1][1] = 15
print(matrix[1][1])

ARRAY METHODS

# PYTHON 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]

# PYTHON RANDOM EXERCISE


import random

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

PYTHON ARRAY EXERCISE [get the average of int in an array]

# PYTHON ARRAY EXERCISE

numbers = [3, 12, 4]


total = numbers[0] + numbers[1] + numbers[2]
average = total / len(numbers)

print(average)
PYTHON 2D ARRAY EXERCISE [IGN generator using 2d array]

# PYTHON ARRAY EXERCISE

# declare ign variables in form of array


name = [
['cat', 'dog'],
['1', '2']
]
# get user input
choice1 = int(input('cat or dog?: 0 cat, 1 dog: '))
choice2 = int(input('one or two? 0 one, 1 two: '))
# declare ign variables
ign1 = ''
ign2 = ''
# set ign conditon
if choice1 == 0:
ign1 = name[0][0]
elif choice1 == 1:
ign1 = name[0][1]
if choice2 == 0:
ign2 = name[1][0]
elif choice2 == 1:
ign2 = name[1][1]
# print ign conditions
print(f'your ign is: {ign1}_{ign2}')

You might also like