PYTHON CODING Flashcards
(9 cards)
how to generate random number within a range
import random
x = random.randint(1,100)
print(x)
outputs random number from 1-100
how to make string all uppercase, lowercase and find length of string
length = len(x)
uppercase = x.upper()
lowercase = x.lower()
how to repeat code a specific number of times
for i in range(100):
print(“hello”)
how to repeat code while condition is met/not met
x = 0
while x < 10:
print(x)
x = x + 1
how to do functions
def add10(x):
for i in range(10):
x = x + 1
print(x)
return(x)
add10(10)
how to make an array and how to find the item in an array and how to change item in an array
a = [1,2,3,4,5]
print(a[2])
a[2] = 5
print(a)
line:
1 = how to make an array
3 = how to find item in an array
4 = how to change item in an array
how to find specific character in a string
a = “hello world”
print(a[1:4])
should print out “ell”
how do you open a file and create a new file
file = open(“file name.txt”)
newFile(“myFile.txt”)
how do you read and write to a file and then close the file
filename_variable.writeLine(“hello world”) writes “hello world” to file
filename_variable.readLine() read line 1
filename_variable.readLine() reads line 2
filename_variable.close()