PYTHON CODING Flashcards

(9 cards)

1
Q

how to generate random number within a range

A

import random
x = random.randint(1,100)
print(x)

outputs random number from 1-100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to make string all uppercase, lowercase and find length of string

A

length = len(x)
uppercase = x.upper()
lowercase = x.lower()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how to repeat code a specific number of times

A

for i in range(100):
print(“hello”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how to repeat code while condition is met/not met

A

x = 0
while x < 10:
print(x)
x = x + 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how to do functions

A

def add10(x):
for i in range(10):
x = x + 1
print(x)
return(x)

add10(10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how to make an array and how to find the item in an array and how to change item in an array

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to find specific character in a string

A

a = “hello world”
print(a[1:4])

should print out “ell”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how do you open a file and create a new file

A

file = open(“file name.txt”)
newFile(“myFile.txt”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how do you read and write to a file and then close the file

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly