python coding notes Flashcards

1
Q

how to find where a character is

A

.index(character)

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

how to remove something from a list

A

list.pop(position of char)

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

different ways to show range

A
  • for i in range
  • num for num
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

different ways to do loops

A
  • for i in
  • char for char
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how to find the number of time something occurs in a list

A

.occur()

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

how to sort list

A
  • sorted(list)
  • list.sort()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to find length of something

A

len()

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

how to insert an element into a specific index of a list

A

list.insert()

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

how to skip dumbest when doing range

A
  • add a third input, eg range(2,9,2) would output 2,4,6,8
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how to slice lists

A

list[start:end]

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

how to see how many times an element occurs in a string or list

A

.count

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

how to remove duplicates in a list

A

use list(set())

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

what is a tuple

A

a data structure that allows us to store multiple pieces of data inside of it
- the elements, the order of the elements and how many elements there are cannot be changed
- they are immutable, can’t be edited

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

what does zip() do

A

takes two or more list as u outs and returns an object that contains a list of pairs. each pair contains one element from each of the inputs

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

what is indefinite iteration

A

where the number of times the loop is executed depends on how many times a condition is met

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

what is definite iteration

A

where the number of times the loop will be executed is defined in advance

17
Q

what does a while loop do

A

performs a set of instructions as long as a given condition is true

18
Q

what does break do

A

stops an iteration

19
Q

how to carry on an iteration

A

continue

20
Q

how to iterate using a list

A

[(what u wanna append to the list) for i in list]

21
Q

string in built functions

A

.strip() - removes characters from string
- .find() - finds the position of a character in a string

22
Q

how to reverse a list

A
  • list.reverse()
  • Reverse = True
  • [::-1]
23
Q

what does enumerate do()

A

generates tuples which represent the indices and values of the elements in the list

24
Q

what does while True do

A

loops forever

25
Q

what does try do

A

lets you test a block of code for errors.

26
Q

how to check if something is a digit

A

.isdigit()

27
Q

how to repeat a loop, for example in a game

A

use While True

28
Q

difference between pop and remove

A

remove removes using values
pop removes using indexes

29
Q

if string.split() what does this mean

A

checks if the input string is not empty after removing leading and trailing whitespace

30
Q

other ways to remove white space in sentence

A
  • “”.join(sentence.split())
  • sentence.replace(“ “, “”)
31
Q

what else does list()

A
  • converts a string into a list, where each character in the string is a separate element in the list
  • eg hello is [“h”, “e”, “l”, “l”,”o”]
32
Q

what does zip do

A

used to combine two or more iterables, eg an integer and string, or string and a list

  • eg - for i,j in zip(str(n)), range(1,len(str(n))+1) , this combines a string and an integer
33
Q

how to do square root

A

math.sqrt() but import math before u do this

34
Q

how to check if a number is whole

A

mod it by 1

35
Q

how to make a number whole

A

round()